释放对象三js [英] Deallocate Object Three js

查看:92
本文介绍了释放对象三js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用webGl和Three.js做一个项目.我的问题只有一个:

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..

现在我使用这个:

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?

推荐答案

浏览器将为您清理对象.取消引用对象后,浏览器将清除垃圾,所有垃圾都将为您删除.

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.

要测试这种情况的发生方式和时间,可以使用Chrome中的时间轴"标签.删除大对象后,最终应该看到垃圾回收"事件.这表明浏览器已执行清理.

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.

就这一点而言,没有办法强迫浏览器释放特定​​的内存.不过,依靠垃圾收集器通常没问题,因为在现代浏览器中,垃圾收集器的速度非常快.不过,我将更加关注您分配的对象数量.特别是在3D应用程序中,大量的对象分配(甚至是小的对象)也会导致大量的堆混乱,这可能会对渲染性能产生负面影响.

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.

这篇关于释放对象三js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆