问题
I'm doing a project using webGl and Three.js. My problem is only one:
I need to deallocate memory because, during the game, i have to create a lot of object so i don't want to allocate an huge size of memory. I tried a lot of "code", but nothing happened..
For now I use this:
function deallocazioneScena(){
for ( var i = scene.children.length - 1; i >= 0 ; i -- ) {
deallocazioneObj(scene.children[i]);
}
//camera.position.set(0,50,70);
}
function deallocazioneObj(obj){
scene.remove(obj);
if (obj.geometry) {
obj.geometry.dispose();
}
if (obj.dispose) {
obj.dispose();
}
}
How i could do for make this work?
回答1:
The browser will clean up your objects for you. When you've dereferenced the objects, the browser will perform a cleanup of garbage, and all of that will be removed for you.
To test how and when this happens, you can use the Timeline tab in Chrome. After you've removed a large object, you should eventually see a "Garbage Collection" event. This indicates that the browser performed a cleanup.
As far as making that explicit, there's no way to force the browser to free a particular piece of memory. Leaning on the garbage collector is generally fine, though, as it is quite fast in modern browsers. I would be more concerned about the number of objects that you allocate, though. Especially in 3D applications, large numbers of object allocations (even for small objects) can result in quite a lot of heap churn, which could very negatively impact your rendering performance.
来源:https://stackoverflow.com/questions/22696253/deallocate-object-three-js