在 Three.js 中释放内存 [英] freeing memory in three.js

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

问题描述

我的应用程序加载了很多网格.为了摆脱旧网格,我尝试处理它们.但内存永远不会被释放.

my application loads a lot of meshes. to get rid of old meshes i try to dispose them. but the memory is never being freed.

我错过了什么吗?

我的简单复制示例:

  1. 加载 100 个大二元网格
  2. 再次处理所有这些
  3. chrome 任务管理器显示使用了 250mb 内存,与没有步骤 2 时完全相同

  1. load 100 of big binary meshes
  2. dispose all of them again
  3. chrome task manager says 250mb memory used, its exactly the same as without step 2

内存测试

var scene = new THREE.Scene();
var mymesh=Array();

// 1. load a lot of geometry/meshes...

for(var i=0;i<100;i++)
{
    var bloader;
    bloader = new THREE.BinaryLoader();

    bloader.load( "objekte/presto_6.js" , function( geometry ) 
    {
        mymesh.push(new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( {color:0xffffff } ) ));
        scene.add(mymesh.length-1);
    });
}

// 2. try to dispose objects and free memory...

for(var j=0;j<mymesh.length;j++)
{
    mymesh[j].geometry.dispose();
    mymesh[j].material.dispose();
    screne.remove(mymesh[j]);
}

mymesh=Array();

</script>

推荐答案

可能是拼写错误,但如果不是:screne.remove(mymesh[j]); 应该是 scene.remove(mymesh[j]);

Probably a typo, but if it isn't: screne.remove(mymesh[j]); should be scene.remove(mymesh[j]);

除此之外:记住(或找出)JS 如何管理内存.它的垃圾收集器是一个清扫型 GC.它标记没有在任何地方被引用的对象,然后在下次 GC 启动时清除它们:

Other than that: rember (or find out) how JS manages the memory. Its garbage collector is a sweep-and-clean GC. It flags objects that aren't being referenced anywhere and then cleans them next time the GC kicks in:

for(var j=0;j<mymesh.length;j++)
{
    mymesh[j].geometry.dispose();
    mymesh[j].material.dispose();
    scene.remove(mymesh[j]);
}

mymesh 数组仍然包含对您要释放的网格对象的引用.GC 看到此引用,因此不会标记这些对象.重新分配、删除或删除您不再需要的特定键的整个数组:

The mymesh array still contains references to the mesh objects you are attemting to free. The GC sees this referenec, and therefore refrains from flagging these objects. Reassign, delete, either the entire array of those specific keys you no longer need:

for(var j=0;j<mymesh.length;j++)
{
    mymesh[j].geometry.dispose();
    mymesh[j].material.dispose();//don't know if you need these, even
    scene.remove(mymesh[j]);
    mymesh[j] = undefined;//or
    delete(mymesh[j]);
}
//or simply:
mymesh = undefined;//or some other value

这允许释放内存,除非另一个变量也保留在引用部分或所有这些对象的范围内.
顺便说一句:

That allows the memory to be freed, unless another variable remains in scope that references some or all of these objects, too.
As an asside:

mymesh=Array();

在很多层面上都是糟糕的代码.以大写开头的 JS 函数是构造函数,应该使用 new 关键字来调用,尽管大多数构造函数(尤其是原生对象)应该尽可能少地调用.
他们的行为可能是不可预测的,而且编写代码的方法通常更短:

Is bad code on many levels. JS functions that begin with an UpperCase are constructors, and should be called usign the new keyword, though most constructors (especially the native objects) shoul be called as little as possibe.
Their behaviour can be unpredictable, and there's often a shorter way to write the code:

mymesh = [];//an array literal
//example of werird behaviour:
mymesh = new Array(10);//[undefined, undefined, undefined....]
mymesh = [10];
mymesh = new Array('10');//['10']
mymesh = new Array(1.2);//RangeError
var o = new Object(123);//returns new Nuber
o = new Object('something');//new String
o = new Object(false);//new Boolean
o = new Object('foo', 'bar');//new String('foo')!!!
o = {foo: 'bar'};//<-- this is soooo much easier

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

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