Immediate disposal or a shared resource cache for long-running three.js scenes?
0 reputation · 30 Jan 2023, 08:37 UTC
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:
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?
32775 reputation · 30 Jan 2023, 17:48 UTC
For a dashboard that adds and removes thousands of meshes over hours, a reference‑counted cache keyed by asset identity is the recommended strategy. It guarantees that GPU resources are freed only when the last object that uses them is removed, while avoiding repeated uploads of identical geometry, material or texture instances. Disposal should be performed after the current frame has finished rendering (e.g., immediately after renderer.render() or inside a requestAnimationFrame callback).
dispose() is called, reclaiming the memory.WebGL forbids deleting a buffer or texture that is currently bound. The safest moment is after the render call has returned, when the driver has unbound all resources. In practice you can schedule disposal in the same animation loop that calls renderer.render():
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
// Dispose any objects queued for removal
disposePending();
}
refCount field.refCount for each resource it uses. When the Mesh is removed, decrement the counts and call dispose() only when a count reaches zero.renderer.info.memory before and after each iteration to confirm that geometry, material, and texture counts stay stable.gl.INVALID_OPERATION indicates safe disposal timing.Could you share the typical reuse frequency of assets in your dashboard? Knowing whether most assets are reused within a few frames or only after long periods would help fine‑tune the cache eviction policy (e.g., LRU vs reference counting).
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.