释放ThreeJS应用程序的内存

Hel*_*ium 9 javascript memory three.js

我目前在使用ThreeJS应用程序时释放内存时遇到问题.

我知道有关于这个问题的几个问题:

用我做了以下打字稿函数处理我的对象:

function dispose( object3D: THREE.Object3D ): void
{
    // Dispose children first
    for ( let childIndex = 0; childIndex < object3D.children.length; ++childIndex )
    {
        this.dispose( object3D.children[childIndex] );
    }

    object3D.children = [];

    if ( object3D instanceof THREE.Mesh )
    {
        // Geometry
        object3D.geometry.dispose();

        // Material(s)
        if ( object3D.material instanceof THREE.MultiMaterial )
        {
            for ( let matIndex = 0; matIndex < object3D.material.materials.length; ++matIndex )
            {
                object3D.material.materials[matIndex].dispose();
                object3D.material.materials[matIndex] = null;
            }
            object3D.material.materials = [];
        }

        if ( object3D.material.dispose )
        {
            object3D.material.dispose();
            object3D.material = null;
        }
    }

    // Remove from parent
    if ( object3D.parent )
        object3D.parent.remove( object3D );

    object3D = null;
}
Run Code Online (Sandbox Code Playgroud)

但是,当我使用Chrome Dev Tools堆积快照时,我仍然有大量的吨:

  • 数组
  • Vector2(uvs in __directGeometry,...)
  • Vector3(顶点in geometry,normal in faces,vertexColors in faces,...)
  • Face3(面对geometry)
  • 颜色(颜色__directGeometry,...)
  • JSArrayBufferData(彩色,标准,在attributesgeometry...)

由于内存中的所有数据,我的应用程序被iOS上的Jetsam杀死,请参阅:Jetsam在iOS上杀死WebGL应用程序

我怀疑当我要求它时,库中的一些数据没有被释放.

Ser*_*Pie 0

尝试处理掉所有东西。我使用这个片段有一段时间了。它处理材质、纹理、3D 对象。它迭代数组和普通对象。

let dispose = function(o) {
    try {
        if (o && typeof o === 'object') {
            if (Array.isArray(o)) {
                o.forEach(dispose);
            } else
            if (o instanceof THREE.Object3D) {
                dispose(o.geometry);
                dispose(o.material);
                if (o.parent) {
                    o.parent.remove(o);
                }
                dispose(o.children);
            } else
            if (o instanceof THREE.Geometry) {
                o.dispose();
            } else
            if (o instanceof THREE.Material) {
                o.dispose();
                dispose(o.materials);
                dispose(o.map);
                dispose(o.lightMap);
                dispose(o.bumpMap);
                dispose(o.normalMap);
                dispose(o.specularMap);
                dispose(o.envMap);
            } else
            if (typeof o.dispose === 'function') {
                o.dispose();
            } else {
                Object.values(o).forEach(dispose);
            }
        }
    } catch (error) {
        console.log(error);
    }
};
Run Code Online (Sandbox Code Playgroud)