Hi all! We found a memory leak in MirrorTexture: disposing a mirror doesn’t fully release it. In our game, every level change left the previous level in memory through its water reflection, about 4 MB each time, so long play sessions slowly grew.
In short: dispose() is supposed to unhook the mirror from the scene’s image-processing updates, but it tries to do so after the mirror has already forgotten its scene, so the unhook silently never happens. The disposed mirror stays reachable, and so does everything it points at (its render list, its predicate closure and the meshes it last rendered).
The fix is a small reordering inside dispose(); details, numbers and a suggested patch are below.
Repro (Playground): Babylon.js Playground
Each “zone” is a reflective floor (a MirrorTexture) plus 40 spheres. The buttons dispose the zone and build a new one, the way a game changes levels. The on-screen counters show the imageProcessing observer count and how many disposed mirrors are still hooked up. A “Workaround” toggle removes the observer before dispose() for comparison.
Measured (Chrome, Babylon 9.31.0, JS heap after forced GC; disposed mirrors tracked with WeakRef):
| Zones disposed | 0 | 10 | 20 | 50 |
|---|---|---|---|---|
dispose() as is: heap |
20.6 MB | 25.5 MB | 28.7 MB | 39.2 MB |
dispose() as is: disposed mirrors still alive |
0 | 10 | 20 | 50 |
| observer removed first: heap | 20.5 MB | 22.2 MB | 22.7 MB | 22.2 MB |
| observer removed first: disposed mirrors still alive | 0 | 1 | 1 | 1 |
(Screenshots and a chart attached.)
Where: mirrorTexture.pure.ts, unchanged on master:
public override dispose() {
super.dispose(); // BaseTexture.dispose() sets this._scene = null
const scene = this.getScene(); // so this is now null...
if (scene) { // ...and the observer is never removed
scene.imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingConfigChangeObserver);
}
this._sceneUBO?.dispose();
}
Even smaller repro (console only):
const scene = new BABYLON.Scene(engine);
const before = scene.imageProcessingConfiguration.onUpdateParameters.observers.length;
for (let i = 0; i < 5; i++) new BABYLON.MirrorTexture("m" + i, 256, scene, true).dispose();
setTimeout(() => console.log(before, scene.imageProcessingConfiguration.onUpdateParameters.observers.length), 0); // 0 → 5
Real-world impact: in our game each zone has a water mirror whose renderListPredicate closes over the zone’s state, so every zone the player left stayed in memory: meshes, materials and textures, about 4 MB per zone round trip. We found it with heap snapshots. The retainer path was scene._imageProcessingConfiguration.onUpdateParameters._observers[i].callback → the MirrorTexture → _objectRenderer.renderListPredicate / _activeMeshes → the old zone.
Suggested fix: remove the observer before calling super.dispose():
public override dispose() {
const scene = this.getScene();
scene?.imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingConfigChangeObserver);
this._imageProcessingConfigChangeObserver = null;
super.dispose();
this._sceneUBO?.dispose();
}
Our workaround in the meantime: remove mirror._imageProcessingConfigChangeObserver from scene.imageProcessingConfiguration.onUpdateParameters ourselves, then call dispose().
Happy to open a PR with this fix and a unit test if that helps.

