# Thin Instance vertex buffer memory leak (thinInstanceSetBuffer)

**URL:** https://forum.babylonjs.com/t/thin-instance-vertex-buffer-memory-leak-thininstancesetbuffer/59351
**Category:** Bugs
**Created:** [July 3, 2025, 7:26am UTC](https://forum.babylonjs.com/t/thin-instance-vertex-buffer-memory-leak-thininstancesetbuffer/59351 "2025-07-03T07:26:48Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![rapid.tore.levenstam](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/rapid.tore.levenstam/32/43255_2.png) [@rapid.tore.levenstam](https://forum.babylonjs.com/u/rapid.tore.levenstam)
#### Post date: [July 3, 2025, 7:26am UTC](https://forum.babylonjs.com/t/thin-instance-vertex-buffer-memory-leak-thininstancesetbuffer/59351/1 "2025-07-03T07:26:48Z")

</div>

Hello 🙂

Played around with thin instances yesterday and saw that I was continuously getting more and more WebGL buffers in my context. Seems like there is a memory leak, see repro (comments and console logs):

> **[Babylon.js Playground](https://playground.babylonjs.com/#SQ1N4M)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL and WebGPU 3D scenes

Wasn’t able to track down the exact cause, and I’ve only tested the “specialized” matrix buffer. I’m fairly confident there is a memory leak here, but the underlying mechanics I can mostly speculate about, so don’t take my details here as a source of truth, it’s my guess of what’s going on.

Due to suspecting the issue being in 2 parts, I actually don’t think this is currently an issue for single-attribute buffers (as opposed to 4 in the matrix case) due to the fact that there seems to be an error in the reference counting causing it to “cancel out”. However, if this potential error in reference counting is fixed, the issue should show up for single-attribute buffers as well.

I hope the repro shows it well, but will write a summary here as well. I haven’t dug very deep so this is just **my guess** of what’s happening under the surface from what I could gather:

Allocation of new buffer:

- Buffer is created with `thinInstanceSetBuffer` and reference count is _not_ increased (guessing).
- Inside `ThinInstanceMesh::_thinInstanceCreateMatrixBuffer`, `setVerticesBuffer` is called 4 times (one for each row). Each call increases the reference count to the underlying buffer, which is now at 4.

De-allocation of old buffer (happens at the same time):

- Buffer is “disposed” by `thinInstanceSetBuffer`, decreasing the reference counter. I think this is a reference count error, as I suspect the original allocation did not increase the reference count. Either way, I believe we’re now left with 3 on the reference counter.
- The aforementioned `setVerticesBuffer` do not seem to release their references, and the reference count is left at 3, and thus the underlying buffer is never disposed.

My thinking is that the flow should rather be that after `thinInstanceSetBuffer` we should have a reference count of 5 (or 2 in the single attribute cases), and the old/existing buffer should be fully disposed. The first reference being the allocation and storing inside `_thinInstanceDataStorage` (released when creating a new buffer), and the other 4 (or 1) being the vertex attribute references (which should also be released when replaced).

Cheers,  
-Tore

---

<div class="post-metadata">

### Author: ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)
#### Post date: [July 3, 2025, 9:20am UTC](https://forum.babylonjs.com/t/thin-instance-vertex-buffer-memory-leak-thininstancesetbuffer/59351/2 "2025-07-03T09:20:05Z")

</div>

cc @sebavan

---

<div class="post-metadata">

### Author: ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)
#### Post date: [July 3, 2025, 4:42pm UTC](https://forum.babylonjs.com/t/thin-instance-vertex-buffer-memory-leak-thininstancesetbuffer/59351/3 "2025-07-03T16:42:15Z")

</div>



---

<div class="post-metadata">

### Author: ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)
#### Post date: [July 3, 2025, 10:37pm UTC](https://forum.babylonjs.com/t/thin-instance-vertex-buffer-memory-leak-thininstancesetbuffer/59351/4 "2025-07-03T22:37:53Z")

</div>

Thanks a ton for the amazing repro, this is definitely a bug in Babylon. I ll address it ASAP as I want to be sure to fix it correctly 🙂

---

<div class="post-metadata">

### Author: ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)
#### Post date: [July 3, 2025, 11:35pm UTC](https://forum.babylonjs.com/t/thin-instance-vertex-buffer-memory-leak-thininstancesetbuffer/59351/5 "2025-07-03T23:35:32Z")

</div>

I did a first pass at a fix [Fix Thin Instances Memory leak by sebavan · Pull Request #16851 · BabylonJS/Babylon.js · GitHub](https://github.com/BabylonJS/Babylon.js/pull/16851) if it creates issues I ll use a more targeted approach for the fix. Thanks a lot for the report.

---

<div class="post-metadata">

### Author: ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)
#### Post date: [July 4, 2025, 10:14am UTC](https://forum.babylonjs.com/t/thin-instance-vertex-buffer-memory-leak-thininstancesetbuffer/59351/6 "2025-07-04T10:14:49Z")

</div>


