How to free heap memory - best practice

Hi there,
in my application i need to create and destroy some objects depending by user interaction with the scene.
I’ve found out that every time i destroy and then re-create the object the heap memory increase: it seems that some resources are not released properly. So the question is, what’s the best practice to do that?

This is a PG similar to my applicaton:
Click on the ground to toggle the creation/delete of the object (at least 10 times to see some significant change in memory)

What it seems to increase are some array object as you can se from these snapshots taken form Chrome DevTools:

Memory at start

Memory after some toggle job

Where do i mistake?

You don’t dispose neither materials nor textures.
So every click they are adding to the scene again and again…


I’ve disposed also all material and the (the only one) texture…it seems much better:

So, generally speaking, it is not enough to dispose only the mesh, i must always dispose each material and texture of that mesh, is ti right?
Thank you very much, now i try also in my application.


You can also pass true as the second parameter to dispose to have Babylon dispose the material and textures for you. :slightly_smiling_face:

1 Like

Still Inspector shows more and more textures with each click.

1 Like

Thing is it depends cause materials and textures may be shared so you need to manage their lifetime. You can nevertheless use the dispose parameters of the mesh to handle it from the mesh


1 Like

The function display creates a temp DynamicTexture. Calling temp.dispose() at the end of the function resolves this second issue. :slightly_smiling_face: :beers:


Here is the small utility which may help a lot with leakages :slight_smile: - Babylon Scene Debugging Tool


Will def try that out, overlay with pertinent debug info is right up my alley. :grin: