NASA-AMMOS / 3DTilesRendererJS

Renderer for 3D Tiles in Javascript using three.js
https://nasa-ammos.github.io/3DTilesRendererJS/example/bundle/mars.html
Apache License 2.0
1.47k stars 266 forks source link

disposeTile is sometimes unable to clean up tile resources #510

Closed Lafeu-p closed 3 months ago

Lafeu-p commented 3 months ago

LRUCache starts to dispose the tiles

  for ( let i = 0, l = geometry.length; i < l; i ++ ) {

      geometry[ i ].dispose();

  }

  for ( let i = 0, l = materials.length; i < l; i ++ ) {

      materials[ i ].dispose();

  }

  for ( let i = 0, l = textures.length; i < l; i ++ ) {

      const texture = textures[ i ];

      if ( texture.image instanceof ImageBitmap ) {

          texture.image.close();

      }

      texture.dispose();

  }

Cleaning up geometry, material, and texture with the above code looks good, but I found that I was loading large 3dtiles with memory that kept increasing and LRUCache's caching strategy seemed to fail.

image

geometry.dispose () will call onGeometryDispose to clean the geometry contents, but this function will not be registered on the geometry if the mesh is not added (or was added) to the scene,because it traverses the object below the scene, gets the geometry of the object and then binds onGeometryDispose. Many tiles will not register onGeometryDispose after loading because the camera position has moved, and nothing will happen when the dispose function is called

1

gkjohnson commented 3 months ago

The three.js onDispose function is only added once a tile has been rendered and there are WebGL resources to dispose of. It is not a problem if it has not been added.

You'll have provide a more concrete example of a memory leak or show where dangling references are left once a tile has been disposed. Javascript is a garbage collected language so it's expected that resource usage goes up until the GC runs.