06

Memory and Sleep: Leak Checks and an Engine That Rests

A low-end phone has little memory as well as a slow CPU, and an engine that updates 90 objects when the child can see five wastes both. Day 6 adds memory to every measurement, proves there is no leak, and lets far-away objects sleep.

Memory on every run

From this day on, every web scenario in the suite also reports, after a forced garbage collection: JS heap size, DOM nodes, and layout objects (DevTools Performance.getMetrics). The Node rails report the heap held by one kit and by one loaded world. Memory then ratchets like frame time does: a scenario that suddenly holds 5 000 more nodes fails the run.

Day 5's lazy scenery was the big memory win (34 398 → 8 258 nodes at world open). Today's question is different: does memory come back?

A leak check you can rerun

leak-check.mjs mounts the stage, then remounts it 10 times and reloads the world through a door 10 times, measuring after each cycle with a forced GC. It fails if DOM nodes or event listeners grow, or if the heap does not plateau.

nodes      8 251 -> 8 251        (10 remounts + 10 door reloads)
listeners    644 ->   644
heap       plateau, +0.17 MB over the second half
result     no leak

This experiment changed no code: it measured, found nothing, and was recorded in the ledger anyway. The next time someone suspects a leak, the check and its last result are one command away.

Native memory: dispose what you replace

On native, every frame produces a new Skia picture, a native object that the JavaScript garbage collector does not see the size of. They were left to the GC. Now each frame's picture is disposed two frames after it is replaced (React Native Skia may still be drawing the previous one), and a world change disposes its old backdrop chunks. This is hygiene: the cloud container cannot measure device memory, and the ledger says so in the entry's evidence.

An engine that rests

The engine updated every object every frame: physics, springs, breathing, gaze. In a wide world, about 85 of 90 objects are more than a screen away and at rest. Now an object sleeps when it is far from the view and at rest: not held, not moving, not reacting. Anything thrown keeps flying and lands even far away; everything wakes as the camera comes within a screen; and a stage without a host camera (tests, headless tools) never sleeps.

// StageController.tick (simplified)
const reach = Math.max(600, this.viewportWidth);
for (const a of this.cast) {
  if (this.cameraX >= 0 && asleep(a, lo - reach, hi + reach)) continue;  // far and at rest
  a.update(dt);
}

Five guard tests pin the rule down, one per sentence above, plus one for the next fix.

Swipes: a background swipe used to wake the whole world (every object hops a little). Swipes, hops and the scroll jiggle now move only objects within reach of the view.

Paint order: the order objects are drawn in (lanes, riders in seats, things resting on things) was re-sorted and re-keyed every frame. It is now memoised by a numeric signature of the inputs that decide it, and recomputed only when one changes.

Before and after side by side

Low-end, 6× CPU, playground: drag, hold, zigzag, taps and swipes. HUD 33–53 fps before, 41–60 fps after (20–28 % → 5–17 % dropped).

HUD at five moments, low-end playground: before vs after this loop

Mid-range, 3× CPU, market: dips to 45 fps before, a steady 60 fps after.

measurement (same run)loop startloop end
engine p95, idlebaseline−65 %
engine p95, drag-holdbaseline−55 %
engine p95, swipe0.69 / 0.75 ms0.35 / 0.33 ms
low market swipe, dropped · input p9561 / 75 % · 86 / 64 ms38 / 29 % · 55 / 47 ms
low market drag-slow, dropped65 %38 %
JS heap, low market72.1 MB68.5 MB

Design principles at work

  • The controller owns simulation policy. Sleeping is a rule about the model ("an object far away and at rest needs no update"), so it lives in StageController and benefits the web view, the canvas view and the native view alike.
  • Explicit lifetimes for resources the runtime cannot see. Skia pictures are owned objects: whoever replaces one disposes the old one at a defined moment.
  • Make invariants executable. "A thrown object far away still lands" is a test, not a comment. The rule is easy to break with a well-meant optimisation; the test catches it.

Key takeaways

  • Measure memory after a forced GC, on every run, and ratchet it like frame time.
  • A leak check is a loop (mount, unmount, reload) with a plateau assertion. Record a clean result too.
  • Native resources need explicit disposal; the JS heap number does not include them.
  • Don't simulate what nobody can see, but keep anything moving moving, and wake things before they come into view.