AI-Generated Three.js Dashboards Die in Minutes: Memory Leaks and Scene-Graph Growth Behind 'Crashed After 3 Days'
In the past couple of days, Juejin's hot list has been flooded with AI-generated dashboard content: a post titled "Codex + Blender MCP + React for a Hospital 3D Dashboard" sitting at 10k heat, and another titled "AI Helped Me Generate a Cool 3D Dashboard, and It Crashed 3 Days After Going Live" also entered the recommended feed. The success stories cover the full pipeline, the failure stories cover the journey, but nobody has actually given any data about the lag itself — where it stutters, how fast memory grows, and when it dies.
I collected the most common 'quick-and-dirty patterns' in AI-generated Three.js code and reproduced them one by one in Node. First, the measurement boundaries: Node has no WebGLRenderer, so the GPU side can't be tested, but the problems in AI-generated code mostly live on the JS side — how the scene graph is stacked, how objects leak, these can be measured clearly in Node. A side benefit: no GPU differences, so the curves from the same machine are clean for horizontal comparison, not muddied by 'your card is just bad.' Three version 0.187.0 (official npm package), Node v22, three experimental scripts, all reproducible.
Creating New Objects Every Frame, Not a Single remove
AI-generated dashboard code has a common pattern: data changes every frame, so the AI casually writes 'rebuild' — every frame it news a BufferGeometry, news a material, news a Mesh, and adds it to the scene. I reproduced this with a rolling data-point scene; the code looks harmless:
// bad.mjs: rebuild every frame, never clean up
for (let f = 0; f < FRAMES; f++) { // FRAMES = 3600, simulating 60fps x 60 seconds
const geo = new THREE.BufferGeometry();
geo.setAttribute('position',
new THREE.BufferAttribute(makePoints(POINTS), 3)); // 2000 data points
const mat = new THREE.MeshBasicMaterial({ color: Math.random() });
const mesh = new THREE.Mesh(geo, mat);
scene.add(mesh);
}
For 2000 points, position is a Float32Array(6000), 24KB each. In AI's eyes this is a perfectly normal way to write it — the logic is fine, it runs, the effect is correct.
Run 3600 frames (corresponding to 60 seconds in a browser), sample every 300 frames, and look at the memory that 'GC can never reclaim':
Frame: 0 300 600 900 1200 1500 1800 2100 2400 2700 3000 3300
Leak (MB): 0 6.9 13.8 20.6 27.5 34.4 41.2 48.1 55.0 61.8 68.7 75.6
In 55 seconds it grows 75.6MB, about 1.38MB per second, matching the theoretical 24KB x 60. The reason it can't be reclaimed is simple: these Float32Arrays hang on BufferAttribute, referenced by Mesh, referenced by scene.children — and the scene in a dashboard page is resident, alive until the page closes. No matter how hard GC works, it can only collect objects with no references; this reference chain goes from window all the way down.
Linear extrapolation: 1.4GB in 17 minutes, 4GB in 50 minutes — exactly the scale of the V8 heap limit in 64-bit Chrome. One class period is enough to crash the tab. Real dashboards also add GC storms and frame drops, so it feels even earlier. The so-called 'crashed after 3 days' would take a few hours on this curve; 3 days is actually resilient. And a crash is just the endpoint; the process is worse: the heap is full of hundreds of thousands of dead objects to sweep, V8's major GC often takes tens of milliseconds — within the 16.7ms frame budget, that's the regular jank users see. The lag isn't born at the crash; it's planted from the first frame's garbage.
There's a side story worth noting. The first version of the experiment sampled heapUsed, and it only grew 11MB in 60 seconds, which didn't match the cumulative allocation of 24KB x 3600 ≈ 86MB, so I briefly thought this pattern 'wasn't so bad'. After digging I learned that V8's heapUsed doesn't include the underlying TypedArray data — that data is recorded in the arrayBuffers field. After switching metrics and rerunning, the numbers lined up. Measuring memory leaks by only looking at heapUsed can give a wrong conclusion.
The control group creates only one geometry and updates the data in place each frame:
// good.mjs: persistent geometry, only change data each frame
const geo = new THREE.BufferGeometry();
geo.setAttribute('position',
new THREE.BufferAttribute(new Float32Array(POINTS * 3), 3));
const attr = geo.getAttribute('position');
for (let f = 0; f < FRAMES; f++) {
const arr = attr.array;
for (let i = 0; i < POINTS * 3; i++)
arr[i] = arr[i] * 0.999 + Math.sin(i + f * 0.01) * 0.001;
attr.needsUpdate = true; // key: tell three data changed
}
The same 3600 frames, arrayBuffers stays at 0 the whole time, heapUsed holds steady at 16.4MB. Side by side, the two curves are the difference between 'getting slower and slower' and 'steady as a rock' — and the fixed version is actually shorter.
Scene Graph Only Adds, Never Removes; the Per-Frame Traversal Bill Grows
The second problem is more subtle because it doesn't grow memory, it grows time. In a persistent dashboard page, if you add every frame without removing (or not clearing fast enough), scene.children keeps inflating. And three's render loop traverses the scene graph every frame — three 0.187's WebGLRenderer source line 1667 says:
if ( scene.matrixWorldAutoUpdate === true ) scene.updateMatrixWorld();
Every render call traverses once, updating the matrix for every node. How much difference between 1000 and 16000 nodes? I measured by scale, 30 runs per scale, taking the median:
children= 1000 0.058ms
children= 2000 0.117ms
children= 4000 0.281ms
children= 8000 0.599ms
children= 16000 3.084ms
From the thousands to ten-thousands, children grows 16x, and single traverse goes from 0.06ms to 3ms — the 16000 tier even shows a super-linear jump, 3ms is more than twice the 8000 tier. I'll be transparent about the data: the first run of the 100 tier gave an odd 0.137ms (slower than the 500 tier); rerunning revealed it was JIT warm-up on the first scene, stable at 0.005ms after. So with performance measurements, don't trust the first sample. And this is only the scene graph traversal layer; a render frame in the browser also has frustum culling, sorting, draw call submission, each following node count. 0.06ms looks harmless, but it happens on the main thread with a 16.7ms frame budget, and it's pure CPU work — a better GPU won't help.
And the source of children growth isn't just Mesh. Scrolling data labels, alert sprites, dynamic legends on dashboards — many AI implementations just add one per event, each a real node in the scene graph, all paying tax in the same traversal. When we watch monitoring, we only look at FPS and memory; few look at 'how many nodes are actually in the scene.' That number is one line of code in three — scene.children.length, and one more level outward gives the full count; most failed dashboards have thousands or more here.
Static nodes also carry a bill you can skip. Those building blocks, models, and decorations that are placed once and never move default to joining the per-frame matrix update. three provides a switch: set a node's matrixAutoUpdate = false and call updateMatrix() manually once, or set matrixWorldAutoUpdate = false at the scene level to control the pace yourself — the source line if ( scene.matrixWorldAutoUpdate === true ) is the evaluation of that switch. In a ten-thousand-node static scene, disabling that layer cuts traversal cost significantly. This trick is in the official docs, but I've almost never seen it in AI-generated code.
What Does dispose Actually Dispose?
dispose also appears frequently in AI-generated code, but it often appears in the wrong place, or is treated as a 'call it and you're safe' charm. I tested this to the ground in Node. Three groups of 300 Meshes each (about 3.6MB of position data per group), force GC after the run, see what's left in the heap:
A stays in scene (leaked state): build +4.5MB - GC reclaims 0.1MB references held by scene graph, GC can't touch
B remove without dispose: build +4.4MB - GC reclaims 4.3MB remove breaks references, JS heap reclaimed normally
C remove + dispose: build +5.4MB - GC reclaims 5.4MB no real difference from B
Yes, C and B are the same at the JS heap level. The build increments vary by about 1MB; don't get hung up on that — the three groups have different V8 internal states and GC timings, and this magnitude is within normal fluctuation. What we compare is the reclamation difference, not the absolute build value. Because dispose's source is just one line:
// three/src/core/BufferGeometry.js
dispose() {
this.dispatchEvent( { type: 'dispose' } );
}
It only broadcasts an event. The actual freeing of GPU resources happens in the code inside WebGLRenderer that listens for this event — when a geometry is first rendered, the renderer sends vertex data to the GPU to create a VBO and compiles materials into shader programs, registering these underlying resources in its own WeakMap; when the dispose event fires, the listener deletes the corresponding VBO and programs. An object removed from the scene can have its JS heap reclaimed, but the GPU-side resources the renderer already built for its geometry stay occupied until you call dispose on it — provided it was actually rendered.
This also explains two common confusions of 'I did dispose'. For objects that never entered the render pipeline, the renderer built nothing, so dispose has nothing to do, and the JS heap is reclaimed as usual; for objects that were touched by the renderer and then disposed, the resources have to be rebuilt and returned, so it's wasted effort. Another typical AI-written dispose misplacement: when switching layers, the new layer is added, the old removed, but dispose hangs in the component unmount lifecycle — in an SPA, that hook might not fire for hours, and the GPU memory quietly climbs in the meantime.
This was the most surprising part of my whole experiment. When AI writes code with dispose, it's like deleting a file into the recycle bin — you did the action, but space isn't freed. If written correctly, its value is on the GPU side; if written wrong, it's self-comfort.
Tutorials also bear half the blame. three's official examples and introductory tutorials never dispose objects — because when the demo page closes, the browser takes the whole page's resources away with it, so there's no leak to talk about. Models learn this 'demo context'; you deploy a 7x24 persistent page. The gap between tutorial posture and operational goals is where the GPU memory climbs day by day.
If you want to verify this in the browser yourself, you don't need to install anything: Chrome DevTools' Performance Monitor panel has a GPU memory item. Let it run overnight, and you'll see at a glance whether it's climbing and how fast. The part Node can't test, the browser keeps the books for you.
How to Fix
Three tricks, all verified on 0.187.
When data changes every frame, use a persistent geometry with attribute.needsUpdate, don't rebuild — this is the good-group pattern from the first pit, and the curve already speaks for it.
When an object goes offline, remove first, then dispose, always in pairs. Don't scatter these across callbacks; write them in a single release function:
function release(mesh) {
mesh.parent?.remove(mesh);
mesh.geometry?.dispose();
if (Array.isArray(mesh.material)) {
mesh.material.forEach(m => m.dispose());
} else {
mesh.material?.dispose();
}
}
The material-array branch isn't defensive programming — Meshes with multiple materials genuinely pass an array, and missing one means leaking one GPU allocation.
For tens of thousands of similar objects, use InstancedMesh, one draw call handles it. After setMatrixAt, remember instanceMatrix.needsUpdate = true; I've tested this on 0.187. But it has limits: an InstancedMesh shares a single material, so instances can't have independent color/opacity animations (unless you go through instanceColor). It suits server racks, building blocks, 'thousands of nearly identical' objects; if each label has its own style, splitting layers and cleaning up on a timer is more realistic.
For real performance issues, I've already walked the diagnosis order for you: first check scene.children.length, one line of code, failed dashboards almost always have thousands or more; then look at the slope of performance.memory, if it's still climbing it's pit one in action; finally open Performance Monitor and watch GPU memory for pit three. Don't reverse the order — the GPU layer is the hardest to reproduce, so clear out the JS-side accounting first; half of 'mysterious lag' ends here.
When AI generates code, it doesn't know your dashboard will run 24 hours; a runnable demo is its entire bar, but a 7x24 no-restart acceptance is exactly where these few lines of cleanup logic are missing. So when I now have AI write dashboards, my acceptance checklist adds one item: stare at scene.children.length and performance.memory in the console for 5 minutes — if the former only grows and the latter keeps climbing, one of the pitfalls in this article is happening.
That's all. The three experiment scripts total about two hundred lines; running them locally takes a few minutes, and the data is reproducible. What's a bit surprising is that these three pitfalls are all basic API usage, none of them obscure knowledge, but together they fully explain the 'crashed 3 days after going live' process.