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,
Am I missing something that does not show in scene explorer?
Does dispose function really remove the object or it just remove objects from the scene but leave them on that window?
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.
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.
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