Correct way to dispose of a mesh

I’m posting this because I couldn’t find any relevant information after searching through the documentation and forums.

When disposing of a mesh, do I need to explicitly remove it from scene.meshes and ShadowMap.renderList as well? If I don’t, will it cause memory leaks or performance degradation?

Or does calling the mesh’s dispose() method automatically handle its removal from those lists?

Thanks!

When you call mesh.dispose(), the Babylon.js engine does automatically handle the cleanup for you. You never need to manually remove a mesh from core engine lists like scene.meshes or shadowMap.renderList.

There are 2 parameters: dispose(doNotRecurse?: boolean, disposeMaterialAndTextures?: boolean)so one can define them for even better tuning.

For example, dispose(false, true) to dispose the parent and all its children and all related materials and textures.

If there is a lot of disposing operations one may try

myMesh.dispose(); myMesh = null; // Helps garbage collection if myMesh is a long-lived reference

but the effect actually is quite subtle and may depend on other factors too.

Thank you for the answer!

From what I understand, calling mesh.dispose() is usually all you need. Babylon.js automatically removes the mesh from scene.meshes and cleans up its resources, so there’s no need to remove it manually. This generally won’t cause memory leaks or performance problems. The only exception is if you’re storing the mesh in your own custom arrays or lists. In that case, it’s a good idea to remove those references as well so the memory can be fully released.

I just want to note that mesh.dispose() does not automatically remove mesh from renderList of RenderTarget or its subclasses like MirrorTexture or RefractionTexture

For example: Babylon.js Playground

The automatic cleanup works when you use standard ShadowGenerator with the default RenderTargetTexture as its shadow map. If you’re using a custom RenderTargetTexture (e.g., for advanced effects), you’re still responsible for managing its renderList yourself (just like with MirrorTexture and RefractionTexture).

Example - Babylon.js Playground