How can I remove meshes from three.js scene completely without causing memory leaks. I could find that loading the same models again and again causes the browser to crash, so it seems like the memory is not being deallocated.
How can I remove meshes from three.js scene completely without causing memory leaks. I could find that loading the same models again and again causes the browser to crash, so it seems like the memory is not being deallocated.
Share Improve this question edited Nov 20, 2016 at 6:53 the12 2,4156 gold badges20 silver badges39 bronze badges asked Nov 19, 2016 at 15:07 sreesreenusreesreenu 1,0572 gold badges12 silver badges22 bronze badges1 Answer
Reset to default 17Use the dispose
method on the geometry and material. Also, ensure that nothing is holding references to these objects, as that will prevent garbage collection.
var myMesh = new THREE.Mesh(geo, mat);
scene.add(myMesh);
//...
scene.remove(myMesh);
myMesh.geometry.dispose();
myMesh.material.dispose();
myMesh = undefined;