Control memory in browser games. Ownership rules (who creates, who frees), scene and level lifecycles with complete cleanup, AbortController for listeners, pooling and preallocation, typed arrays for large data, caches with limits and WeakMap, image, audio and GPU memory budgets, disposing three.js and Pixi resources, phone memory limits and tab crashes, and verifying with heap measurements. Us...
Download skill (.zip) Files on GitHubGame memory management
Leak detection is game-memory-leak-detection. This skill is how to structure the game so memory stays flat.
Ownership: whoever creates it frees it
- Each scene / level owns what it creates: entities, listeners, timers, sounds, textures loaded for it.
enter()creates,exit()frees, always. A restart isexit()thenenter().
class PlayScene {
enter() {
this.ac = new AbortController();
const { signal } = this.ac;
addEventListener("resize", this.onResize, { signal }); // all listeners tied to one signal
this.offCoin = this.game.events.on("coin", this.onCoin);
this.timers = new Timers(); // dt-based; dies with the scene
}
exit() {
this.ac.abort(); // removes every listener above
this.offCoin();
this.bullets.releaseAll(); this.particles.releaseAll();
this.level?.dispose(); // GPU resources, if any
this.entities = null;
}
}
Budgets (phones are the limit)
| Resource | Rough budget for a mobile web game |
|---|---|
| JS heap | < 100–200 MB |
| Decoded images | width × height × 4 bytes each: a 4096² texture is 64 MB |
| Decoded audio | ~10 MB per minute of stereo 44.1 kHz. Stream music instead of decoding it |
| GPU textures | < 150–256 MB total |
iOS Safari kills tabs that use too much memory, often with no error. A big texture atlas is the usual cause.
Techniques
- Preallocate and pool everything that spawns during play (game-object-pooling).
- Typed arrays for big numeric data (tile layers, particles as structure-of-arrays, flow fields):
Float32Array(n * 2)is compact and garbage-free. - Caches need limits: LRU with a max size;
WeakMapwhen keyed by objects so entries die with them. - Images: load at the size drawn; release big ones (
img.src = "", drop references) when a level unloads;ImageBitmap.close(). - Audio: one
AudioContext; decode short sfx, stream long music with<audio>. - three.js: on unload traverse and dispose:
root.traverse((o) => { o.geometry?.dispose(); [].concat(o.material || []).forEach((m) => { for (const k in m) m[k]?.isTexture && m[k].dispose(); m.dispose(); }); });Shared materials or textures: reference-count them, or keep them in a shared group that’s never disposed.
- Pixi:
sprite.destroy(),texture.destroy(true)for textures you created;Assets.unload(key). - Strings and logs: don’t keep growing debug logs or replay histories without a cap.
Verify
leak-check.pycycles (restart, level change) must show flat heap and listeners.- three.js:
renderer.info.memory.textures/geometriesreturn to the same numbers after each level. - Long session test: 20 minutes of play on a phone through the viewer’s preview without slowdown or a crash.