How to delete assets from the memory

Hi guys,

I got a problem of deleting things from memory. I loaded a character with meshes and other assets such as textures, animations, skeletons and etc from a glft file by using SceneLoader.LoadAssetContainerAsync function.

When I used dispose function to remove everything that can be seen in the scene explorer, they did disappear from the scene, but the memory did not decrease much (observed by windows task manager). My question are,

  1. Am I missing something that does not show in scene explorer?
  2. Does dispose function really remove the object or it just remove objects from the scene but leave them on that window?
  3. Is there any approach to completely release all the memory took by the import things?

Garbage collection does not happen instantaneously. dispose marks the asset for release but does not trigger automatic memory freeing. Reference: Memory Management - JavaScript | MDN

As of 2019, it is not possible to explicitly or programmatically trigger garbage collection in JavaScript.

Hope it helps. :slight_smile:


Wanted to add (on top of the wonderful answer @phaselock provided) that if you still reference the object you disposed somewhere, it will not be garbage collected at all.
Babylon itself removes all references to the object, but if your scene still holds the variable (i.e. it is not deleted or set to null/undefined) GC will not trigger.

Also - checking windows task manager for memory management in your browser is wrong. The browser decides the memory allocation and it does not correspond directly to the current JS memory consumption. Check the profiler tab in the dev tools for a better understanding of memory usage of your scene.

1 Like

Thanks for your reply. As you mentioned, some reference may protected the objects from been collected. In this cases, A mesh may reference the materials, while the texture may be referenced by materials. Does it mean I am supposed to dispose the texture, next materials and finally the mesh to dispose everything? According to this topic,

dispose(true,true) can force delete the objects.

I also tried to use a third party tool to check the memory but got a similar result as task manager…mean task manager could be correct? When I keep loading the gltf and disposing them on the scene, it became OOM… so far the only way is to close window to release the memory.

I would say dispose(false,true); would work better (also disposing children and their materials/textures), but yes - this should be the right way to dispose an object.
Want to share a playground where you think the objects are not disposed? maybe we can understand the situation better

Appreciate for your time.
Here is my simple test of dispose function.

Correct me if I did some thing stupid.

From my side, I observed there is nothing changed after while loop executed.

PS:Without the dispose part, if I reload the page, the memory will keep increasing.

We can’t control when the browser runs garbage collection.

If you wait long enough, the browser will clear the memory:

(this is after a minute of profiling).

Just wanted to add that I am using this scene to test (though your scene will probably result in very similar results):

Babylon.js Playground (

I am making sure here that array iteration works correctly and that any reference to any object that was added to the scene is being cleared