How to delete the loaded gltf and babylon files?

For example, if I use gltf loaded by BABYLON.AssetsManager, how can I delete it completely when I don’t need it? And the memory can be freed clean,
This is a memory snapshot I deleted several times and it keeps growing.
My delete method is like this:
public destory(task: BABYLON.MeshAssetTask) {
let meshes = task.loadedMeshes;
let skeletons = task.loadedSkeletons;
let animationGroups = task.loadedAnimationGroups;
let particleSystems = task.loadedParticleSystems;
// meshes
for (let i = 0; i < meshes.length; i++) {
if (meshes[i].material) {
if (meshes[i].material.getActiveTextures()) {
let ts = meshes[i].material.getActiveTextures();
for (let j = 0; j < ts.length; j++) {
if (meshes[i].material.getAlphaTestTexture()) {
let t = meshes[i].material.getAlphaTestTexture();
// skeletons
for (let i = 0; i < skeletons.length; i++) {
// animationGroups
for (let i = 0; i < animationGroups.length; i++) {
// particleSystems
for (let i = 0; i < particleSystems.length; i++) {
But it can’t completely delete material,

To get rid of a material you should just have to call material.dispose(). it must take care of everything for you

same for mesh.dispose()

Can you repro the leak in the playground?

Thank you for your reply, the playground is here :, click the “Add” button to create a resource and display it, click the “Sub” button to delete the resource, but through several rounds of creation and deletion, I found that the memory has been growing. I used Google Chrome’s memory heap snapshot to view the memory.

By the way, ask a few questions, 1. If I want to display two identical 3D models, do I need to load it twice? Because loading is too time consuming, is there a way to load two 3D models at once? 2. How to delete the 3D model correctly so that there is no memory leak? Looking forward to your answer.

Ok using the Inspector, you can see what is left:

To dispose the textures when disposing the material, you can call material.dispose(true, true);

To dispose the skeletons you need to call mesh.skeleton.dispose() (they are not automatically disposed as they could be shared)

  1. No you don’t need to load twice: Use an AssetContainer - Babylon.js Documentation
  2. See my previous message

Thank you for your help, I will try your method, sincere thanks.

1 Like