Immediate disposal or a shared resource cache for long-running three.js scenes?
0 reputation · 30 Jan 2023, 08:37 UTC
I'm designing a long-running three.js dashboard (assume a recent three.js release, r150+ era) where meshes are continuously added and removed from the scene graph over hours. My understanding is that removing a Mesh from the scene does not free its GPU resources, so I need an explicit strategy for BufferGeometry.dispose(), Material.dispose(), and texture disposal.
Two documented approaches seem viable, and I can't settle the trade-off:
- Immediate disposal: dispose geometry, material, and textures as soon as an object leaves the scene. Simple, but re-creating identical resources later means repeated GPU uploads and CPU overhead.
- Shared cache / pooling: keep a central cache keyed by asset identity to avoid redundant uploads. This risks stale references keeping VRAM allocated, and calling
dispose()on a cached material still in use elsewhere would break other objects.
I plan to verify behavior with renderer.info counts and a create/remove loop of ~1,000 meshes while watching heap and VRAM growth.
Which approach better fits a scene where the same assets recur unpredictably? Is reference counting on the cache the standard way to avoid both stale entries and premature disposal? And is there a safe point in the frame lifecycle to dispose without hitting mid-render WebGL errors?