0

I have a large number of objects in a scene, but I can not release the memory with following codes:

for (var i in all3DObj) {
    console.log("disposing");
    scene.remove(all3DObj[i]);
    all3DObj[i].geometry.dispose();
    if (all3DObj[i].material instanceof Array) {
        for (var j = 0; j < all3DObj[i].material.length; j++) {
            all3DObj[i].material[j].dispose();
            if (all3DObj[i].material[j].map) all3DObj[i].material[j].map.dispose();
        }
    } else {
        all3DObj[i].material.dispose();
        if (all3DObj[i].material.map) {
            all3DObj[i].material.map.dispose();
        }
    }
    all3DObj[i] = null;
}

How to release the memory effectively?

Tim Diekmann
  • 7,755
  • 11
  • 41
  • 69
  • That looks pretty much alright, only addition: If you're using other textures (normalMap etc) you might need to dispose them manually as well. Are you 100% sure that there is not a reference to any of these objects anywhere else? Did you wait until the next full GC-run before measuring free'd memory? How do you measure the memory used? – Martin Schuhfuß Jul 03 '18 at 13:12
  • There is still an open issue in `WebGLRenderer` that could cause your problem, see https://github.com/mrdoob/three.js/pull/12464. Try to call `renderer.renderLists.dispose()` after your `for` loop. – Mugen87 Jul 03 '18 at 14:11
  • you might want to look at https://stackoverflow.com/a/33199591/1980846 – gaitat Jul 03 '18 at 14:28
  • OK, thank you very much! I will try it as soon as possible. – HytionKing Jul 04 '18 at 10:03

0 Answers0