Three js memory management

陌路散爱 提交于 2019-12-31 13:08:12

问题


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:

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.

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

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.


回答1:


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

three.js r.60




回答2:


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.

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

this.requestId = requestAnimationFrame(render);


来源:https://stackoverflow.com/questions/13914959/three-js-memory-management

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