Skip to content

keiko: each play() stays in memory (most of it held by devOverlay), so keiko mutate runs out of heap #129

Backlog

Description

@LoopyJack

What

Every play() in a keiko test keeps the booted game in memory after the next play() replaces it, even after forced GC. Most of it is held by devOverlay(); a smaller part remains without it. Ordinary suites don't notice, but npx keiko mutate boots the game for every test once per mutant and runs out of heap: on a 42-test suite it died at a 4 GB heap after about 50 s, and again at --max-old-space-size=12288 after 197 s (peak RSS 12.9 GB, JavaScript heap out of memory, exit 134). keiko mutate tests/<one file>.test.ts with 6 tests also died at 4 GB after 54 mutants.

Reproduction

A game made from the basic template (its main.ts installs devTools({ input }) and devOverlay() inside if (import.meta.env.DEV)), plus this test:

import { play, test } from "keiko";

test("each play() is freed", async () => {
  const gc = (globalThis as { gc?: () => void }).gc;
  if (!gc) throw new Error("run with NODE_OPTIONS=--expose-gc");
  const mb = () => {
    gc();
    gc();
    return Math.round(process.memoryUsage().heapUsed / 1e6);
  };
  const out = [mb()];
  for (let i = 0; i < 20; i++) {
    const game = await play();
    game.wait(10);
    if (i % 5 === 4) out.push(mb());
  }
  throw new Error(`heapUsed MB: ${JSON.stringify(out)}`);
});

NODE_OPTIONS=--expose-gc npx debuff test tests/leak.test.ts

Numbers (heapUsed after 2x gc, every 5 boots)

main.ts heapUsed Per boot
devTools(...) + devOverlay() 27 → 82 → 119 → 156 → 193 MB ~7.4 MB
devTools(...) only 27 → 52 → 59 → 66 → 73 MB ~1.4 MB
devTools(...) only, and no new Audio() 27 → 51 → 58 → 65 → 73 MB ~1.4 MB

On a second machine a 60-boot version grew steadily from 51 to 535 MB (~8 MB per boot, with devOverlay()). The game's own state is plain arrays and objects created per boot, and leaving out devOverlay() alone removes most of the growth, so the overlay seems to stay reachable after keiko disposes the page (a listener or registry on a Node global, perhaps). keiko refuses to boot without devTools, so I couldn't bisect the remaining ~1.4 MB from the game side; it may be in devTools, core or keiko's environment.

Versions

debuff 0.35.0 (@debuffjp/core, dev, input, audio, juice), keiko 0.9.0, Node 26.13.0, Linux. The mutate runs were on a second Linux machine with the same packages.

Workaround

Running keiko mutate per test file (or per -t title for large files) with a raised heap, then merging the parts.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions