Skip to content

build: the last build stays in memory after build() returns #59

Closed

Description

@lukeed

After build() returns, the data of that build stays in memory until the next build replaces it. On the 10,000-page site of bun run bench (#56), the live JS heap after a full GC is 500 MB when build() returns. Before the build, it is 3 MB.

The chain

A heap snapshot after the build shows this chain, from the module to the pages:

  1. templates.collect() assigns the blocks from makeBlocks(argv, config) onto the module-level blocks object. Each block closure keeps config, which is the template context of run().
  2. The context has $assets. Its render method is a closure that run() makes, in assets().
  3. A closure keeps the scope of run(), and that scope holds every local that a closure in run() uses.

So these stay live:

local of run() size
ready, the final HTML of each file 180 MB
scanned, with the body of each page 64 MB
pages, the HTML of each page with placeholders 45 MB

The next collect() replaces the blocks, and only then can the GC free the last build. The size does not grow from one build to the next, so this is a hold, not a leak.

Effect

  • bongo dev calls the same build(), so between rebuilds it holds the last build twice: once in the memory sink, and once through this chain.
  • bongo build exits after the build, so this does not change its peak.

Repro

  1. bun run bench site --runs 1
  2. In the stage table, read "heap at start" on the exit row: it is the live heap after build() returns.

Acceptance

  • After build() returns, the live heap goes back near its level before the build.
  • bongo build writes the same files, byte for byte, as before.

Activity

  1. added
    bug
    on Oct 15, 2026
  2. lukeed commented on Oct 15, 2026

    @lukeed
    OwnerAuthor

    More detail from a review of this issue:

    • The compiler closure in templates.collect() (packages/bongo/src/utils/templates.ts, the compiler function for each partial) also keeps config, the template context, because it spreads config on each call. Partials and blocks[key] both hold these compilers. So a change to makeBlocks() alone does not free the last build.
    • No code deletes keys from blocks. bongo dev clears Partials and Layouts on a rebuild, but a partial that you delete or rename stays in blocks. So across renames in dev, the held set can grow; "a hold, not a leak" is true only while the partial names stay the same.

    Direction: give the template state an explicit life of one build. Keep the registries and blocks in a context for each build, and pass it to rendering. Move the assets() factory out of run(), and pass it only the values it needs. Compiled templates can stay cached if they do not keep the build context.

    Measure: run several builds in one process, also a failed build and a rename of a partial, and read the heap after a full GC after each build. The bench runs each build in a new process, so it cannot show this fix by itself.

  3. added 2 commits that reference this issue on Oct 15, 2026
    df4fdbd
    fa9a7c0
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions