javascript 三种js内存管理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13914959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:10:06  来源:igfitidea点击:

Three js memory management

javascriptmemory-managementthree.js

提问by user974250

I have a large scene with a lot of Mesh and MorphAnimMesh. I want to free memory when the meshes are removed. If i know right this is the best way to do:

我有一个包含很多 Mesh 和 MorphAnimMesh 的大场景。我想在删除网格时释放内存。如果我知道正确,这是最好的方法:

for ( var i = scene.children.length - 1; i >= 0 ; i -- ) {
  var obj = scene.children[i];
  scene.remove(obj);
  obj.deallocate(); 
  obj.geometry.deallocate();
  obj.material.deallocate();
  obj.material.map.deallocate();
}

if i check the memory usage at task manager after this, nothing changes. ( tried to wait a few min for GC but nothing. ) Google Chrome memory snapshot shows the objects still there. morphTargets in THREE.Geometry @1862203 etc.

如果我在此之后检查任务管理器的内存使用情况,则没有任何变化。(试图等待几分钟的 GC 但什么也没有。)谷歌 Chrome 内存快照显示对象仍然存在。THREE.Geometry @1862203 中的 morphTargets 等。

Tried to set the obj to null, but still no memory decrease.

试图将 obj 设置为 null,但仍然没有减少内存。

Any idea what am i doing wrong?

知道我做错了什么吗?

Its a game with levels and the player can change from one to another. After a few change memory usage increases to really high. Thats why i want to remove all object from memory before the level change.

它是一个有层次的游戏,玩家可以从一个到另一个。几次更改后,内存使用量增加到非常高。这就是为什么我想在级别更改之前从内存中删除所有对象。

回答by WestLangley

Most likely, you need to add some, or all, of the following:

很可能,您需要添加以下部分或全部内容:

geometry.dispose();
material.dispose();
texture.dispose();

Check out these examples:

查看这些示例:

http://mrdoob.github.com/three.js/examples/webgl_test_memory.html

http://mrdoob.github.com/three.js/examples/webgl_test_memory.html

http://mrdoob.github.com/three.js/examples/webgl_test_memory2.html

http://mrdoob.github.com/three.js/examples/webgl_test_memory2.html

three.js r.60

三.js r.60

回答by Rajesh Panda

I did try all the dispose and deallocate methods but nothing worked.

我确实尝试了所有的处置和解除分配方法,但没有任何效果。

Then I did the following for my ionic application which is using webgl renderer to render a 360 image.

然后我为我的 ionic 应用程序执行了以下操作,该应用程序使用 webgl 渲染器来渲染 360 度图像。

this.renderer = new THREE.WebGLRenderer({ antialias: true });
RicohView.prototype.stopRendering = function () {
    this.canRender = false;
    this.renderer.forceContextLoss();
    this.renderer.dispose();
    console.log('renderer disposed');
    cancelAnimationFrame(this.requestId);
}

requestId is something which can be captured from

requestId 是可以从中捕获的东西

this.requestId = requestAnimationFrame(render);