Skip to main content
rule CX Codex

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

Install into Claude Code · Manually: put the files in place All methods and platforms ↓
.claude/rules/memory-dispose-geometry.md
Open on GitHub

What 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.md

The paths frontmatter field limits the files

Step 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

Similar IDE rules

All IDE rules for Game dev and 3D: Unity, Unreal, Godot, Blender →

From the same set86

The whole set →
server-auth-actions The server-auth-actions rule puts authentication and permission checks inside every Server Action. Use it to guard server mutations that callers can invoke directly. CX Codex +1 ★ 4,719repo stars setup-use-import-maps setup-use-import-maps — a rule that swaps the old Three.js CDN <script src> tag for an import map with ES modules and a pinned version. CX Codex +1 ★ 4,719repo stars render-delta-time render-delta-time instructs agents to use delta time in animation loops so motion remains consistent across all display refresh rates. CX Codex +1 ★ 4,719repo stars root-cause-debugger The root-cause-debugger subagent for Claude Code and Codex reproduces failures and finds bug root causes before code changes. It helps diagnose bugs without editing code. CX Codex ★ 4,719repo stars code-reviewer code-reviewer is a Claude Code subagent that checks completed work for bugs and regressions. It runs after implementation or when an existing diff needs review. CX Codex ★ 4,719repo stars perf-visibility-toggle perf-visibility-toggle guides the AI to toggle the visible prop on Three.js components rather than conditionally unmounting them, avoiding GPU shader recompilation. CX Codex +1 ★ 4,719repo stars perf-r3f-perf The perf-r3f-perf rule teaches Claude Code about r3f-perf, a performance monitoring library for Three.js scenes in real time. Spot frame drops, memory leaks, and unnecessary matrix recalculations as they happen. CX Codex +1 ★ 4,719repo stars codebase-mapper codebase-mapper maps repository structure, call chains, and data flows before code changes without mutating any files. CX Codex ★ 4,719repo stars

Project sponsors

Companies that keep this analytics open