跪拜 Guibai
← All articles
Three.js · Frontend · AI Programming

AI-Generated Three.js Dashboards Die in Minutes: Memory Leaks and Scene-Graph Growth Behind 'Crashed After 3 Days'

By Flynt ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Without AI-aware cleanup, a Three.js dashboard can hit V8's 4GB heap cap in under an hour and degrade to regular jank within minutes. The reproduced curves give frontend teams a concrete checklist to audit any AI-generated or long-lived Three.js scene for the same three leaks.

Summary

AI-generated Three.js dashboards leak memory at a steady 1.38MB/s when the code rebuilds BufferGeometry, materials, and Meshes every frame without ever calling remove. A Node reproduction that simulates 60fps for 60 seconds shows 75.6MB of unreclaimable memory in 55 seconds, matching the theoretical 24KB per object times 60 frames. The leaked Float32Arrays hang on BufferAttribute, Mesh, and scene.children, so GC cannot touch them while the scene stays alive.

A second problem runs on the CPU side: scene.children keeps growing because nothing removes nodes, and three.js traverses the whole scene graph every render call. Measuring scales shows traversal time climbing from 0.058ms at 1000 children to 3.084ms at 16000, with a superlinear jump at the top. Combined with frustum culling, sorting, and draw call submission, the cost lands on the 16.7ms frame budget and produces the jank users see long before a crash.

A dispose() test in Node shows why the call alone is often useless: in three.js dispose() only broadcasts an event; actual GPU resource release happens inside WebGLRenderer's listener, and only for objects that were actually rendered. remove() alone lets the JS heap reclaim an object but leaves GPU resources allocated. The fixes are simple and verified on 0.187: reuse geometry with attribute.needsUpdate, remove then dispose in pairs inside a release function, use InstancedMesh for thousands of similar objects, and diagnose by checking scene.children.length and performance.memory before looking at GPU memory.

Takeaways
— Per-frame creation of BufferGeometry, MeshBasicMaterial, and Mesh without remove leaks about 1.38MB/s; 3600 frames produce 75.6MB of unreclaimable memory.
— V8's heapUsed metric does not include TypedArray backing data; track arrayBuffers instead to see real leaks.
— Reusing a single geometry and updating attribute.needsUpdate keeps arrayBuffers at 0 and heapUsed flat at 16.4MB across 3600 frames.
— Scene graph traversal cost rises from 0.058ms at 1000 children to 3.084ms at 16000, with a superlinear jump at the top.
— The first performance sample is unreliable due to JIT warm-up; rerun and take medians.
— three.js dispose() only dispatches an event; WebGLRenderer's listener frees GPU resources, and only if the object was actually rendered.
— remove() without dispose reclaims JS heap but leaves GPU resources allocated.
— For many similar objects, InstancedMesh with instanceMatrix.needsUpdate = true gives one draw call; it shares material, so independent per-instance style is limited.
— Diagnose lag by checking scene.children.length first, then performance.memory slope, then Performance Monitor's GPU memory.
Conclusions

AI code generators optimize for a runnable demo, not a long-lived deployment, so they inherit tutorial habits that never dispose because demo pages are torn down wholesale.

The heapUsed measurement pitfall suggests many quick checks of AI-generated code may underestimate leaks by ignoring TypedArray storage, leading to false confidence.

All three pitfalls are basic API usage, not obscure knowledge; the failure is not missing expertise but missing operational context in training data.

The diagnosis order (JS heap before GPU) points out that most 'mysterious' lag in three.js dashboards is CPU-side scene graph growth, not a GPU bottleneck.

Concepts & terms
arrayBuffers (V8 memory metric)
In Node/V8, TypedArray backing data is stored separately from the regular JS heap; heapUsed excludes it, so memory leaks from Float32Array positions show up in the arrayBuffers field instead.
dispose() in three.js
Broadcasts a 'dispose' event; WebGLRenderer listens and frees GPU-side resources (VBOs, shader programs) for objects that were rendered. Calling it alone does not reclaim JS heap.
InstancedMesh
A Three.js mesh that renders many copies of the same geometry with one draw call. It shares one material, so independent per-instance color/opacity changes require instanceColor and extra setup.
matrixWorldAutoUpdate
A scene-level flag that controls whether three.js updates world matrices every frame. Setting it false and updating manually can cut traversal cost for static scenes.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗