Deallocate Object Three js

时光总嘲笑我的痴心妄想 提交于 2019-12-11 03:44:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!