Capítulo 806 de 859
Three.js cannot automatically free GPU memory; geometries, materials and textures must be disposed manually, and a small tracker class can automate discovering and disposing everything a loaded model allocated.
dispose(): the method that frees GPU-side memory for a geometry, material, or texture — required because WebGL gives JavaScript no automatic GC hook for GPU resources.ResourceTracker: a custom helper class that records resources as they're created (or discovered) and disposes them all at once.resource.geometry, resource.material (which may be an array), and resource.children to find everything to free.uniforms, both need to be checked when tracking a material.track time) is more flexible than trying to infer what to free later, especially once objects like a tool get parented onto a loaded character.class ResourceTracker {
constructor() {
this.resources = new Set();
}
track(resource) {
if (!resource) return resource;
if (Array.isArray(resource)) {
resource.forEach((r) => this.track(r));
return resource;
}
if (resource.dispose || resource instanceof THREE.Object3D) {
this.resources.add(resource);
}
if (resource instanceof THREE.Object3D) {
this.track(resource.geometry);
this.track(resource.material);
this.track(resource.children);
}
return resource;
}
// dispose() iterates this.resources and calls .dispose() / removes from parent
}
Object3D for later bulk disposal.dispose() yourself on textures, geometries and materials.Object3D hierarchy.dispose() method this pattern relies on.