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: https://playground.babylonjs.com/#TESQZM#18
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…

5 Likes

I’ve disposed also all material and the (the only one) texture…it seems much better: https://playground.babylonjs.com/#TESQZM#20

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.

2 Likes

You can also pass true as the second parameter to dispose to have Babylon dispose the material and textures for you. :slightly_smiling_face:
https://playground.babylonjs.com/#TESQZM#22

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

image

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:

2 Likes

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

3 Likes

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