memory-dispose-geometry — Claude Code rule
memory-dispose-geometry is a Claude Code rule that tells the agent to release geometry when removing scene objects. It applies to Three.js resource cleanup.
MIT ★ 4,719 updated 2026-10-24
.claude/rules/memory-dispose-geometry.mdWhat memory-dispose-geometry does
The rule calls for geometry.dispose() when an object leaves the scene. JavaScript garbage collection does not free Three.js GPU buffers, so removing a mesh alone can leave its geometry allocated.
For nested models, it outlines recursive cleanup of geometry, materials, and textures. A React example removes the mesh and disposes of its geometry and material in the effect cleanup function.
What it helps with
- release GPU memory after removing a scene object
- clean up geometry and materials in a nested model
- dispose of textures during recursive object cleanup
- free Three.js resources when a React effect is cleaned up
Example prompts for the agent
- Update mesh removal so its geometry is released from GPU memory.
- Add cleanup for the geometry, materials, and textures in
complexModel. - Check that this React effect disposes of its Three.js resources.
How to install memory-dispose-geometry
Manually: put the files in place
.claude/rules/memory-dispose-geometry.mdThe paths frontmatter field limits the files
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.cursor/rules/memory-dispose-geometry.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/memory-dispose-geometry.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/memory-dispose-geometry.mdManually: put the files in place
.clinerules/memory-dispose-geometry.mdStep by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use
Questions about memory-dispose-geometry
How do I install memory-dispose-geometry in Claude Code?
Add the rule file at .claude/rules/memory-dispose-geometry.md in the Claude Code project.
What does memory-dispose-geometry do?
It instructs the agent to release geometry when removing scene objects and describes cleanup for materials and textures.
Which resources should be cleaned up for a complex model?
The rule covers geometry, materials, and textures, with recursive cleanup for nested objects.
Source text
memory-dispose-geometry.md · MIT
# memory-dispose-geometry
**Always dispose geometries when removing objects from scene.**
## Why It Matters
Three.js does NOT automatically garbage collect GPU resources. Geometries allocate GPU buffer memory that persists until explicitly freed. Failing to dispose causes memory leaks that eventually crash the browser.
## Bad Example
```javascript
// BAD - Memory leak
scene.remove(mesh);
mesh = null; // GPU buffers still allocated!
```
The JavaScript object is garbage collected, but the GPU memory remains allocated.
## Good Example
```javascript
// GOOD - Proper cleanup
scene.remove(mesh);
mesh.geometry.dispose();
mesh = null;
```
## Recursive Disposal
For complex hierarchies, use recursive disposal:
```javascript
function disposeObject(obj) {
if (obj.geometry) {
obj.geometry.dispose();
}
if (obj.material) {
if (Array.isArray(obj.material)) {
obj.material.forEach(disposeMaterial);
} else {
disposeMaterial(obj.material);
}
}
if (obj.children) {
obj.children.forEach(disposeObject);
}
}
function disposeMaterial(material) {
const textureKeys = [
'map', 'lightMap', 'bumpMap', 'normalMap', 'specularMap',
'envMap', 'alphaMap', 'aoMap', 'displacementMap',
'emissiveMap', 'gradientMap', 'metalnessMap', 'roughnessMap'
];
textureKeys.forEach(key => {
if (material[key]) {
material[key].dispose();
}
});
material.dispose();
}
// Usage
disposeObject(complexModel);
scene.remove(complexModel);
```
## React Example
```jsx
useEffect(() => {
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial();
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
return () => {
scene.remove(mesh);
geometry.dispose();
material.dispose();
};
}, []);
```
## References
- [Three.js Manual: Dispose](https://threejs.org/manual/#en/dispose)
© zebbern · MIT · zebbern/claude-code-guide