Is there a correct way to remove mesh from a scene without causing memory leaks?
removable_items = [];
box = new THREE.Object3D();
scene.add(box);
function add() {
var mesh = new THREE.Mesh( new THREE.IcosahedronGeometry( 10, 5 ), new THREE.MeshPhongMaterial( {color: 0xFFFFFF}) );
box.add( mesh );
removable_items.push(mesh);
//clean(); ///// when is integrated in function memory is cleaned properly
}
function clean() {
if( removable_items.length > 0 ) {
removable_items.forEach(function(v,i) {
v.parent.remove(v);
});
removable_items = null;
removable_items = [];
}
}
function makeExperiment(r) {
var i = 0;
while (i < r) {
add();
i++;
if( r === i ) console.log(r+' finnished ');
}
}
makeExperiment(50);
/// after that i mannualy set clean();
After removing meshes from the scene, they are no longer visible, but memory still shows signs of leaking. Is THREE.js creating additional references?
THREE.js R73
EDIT: When clean();
is integrated within a function (currently commented out in the code), memory is properly cleaned. However, setting clean();
manually after makeExperiment();
does not free up memory correctly.