I'm facing challenges with memory management in my ThreeJS application.
I know there are already a few queries regarding this issue:
- freeing memory in three.js
- Freeing memory with threejs
- Three.js Collada - What's the proper way to dispose() and release memory (garbage collection)?
I do release my objects using this Typescript function I've created :
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;
}
However, even after using heap snapshots in Chrome Dev Tools, I'm still seeing :
- Arrays
- Vector2 (uvs in
__directGeometry
, ...) - Vector3 (vertices in
geometry
, normal infaces
, vertexColors infaces
, ...) - Face3 (faces in
geometry
) - Color (colors in
__directGeometry
, ...) - JSArrayBufferData (color, normal, in
attributes
ofgeometry
, ...)
Due to the large amount of data in memory, my app is being terminated by Jetsam on iOS, as mentioned here : Jetsam kills WebGL application on iOS
I suspect that some data within the library is not being properly released when requested.