Skip to content

@ogygia/devtools · Profiler

Memory

What a render allocates, who caused the garbage collector's pauses, when the heap grew, and what each render leaves alive after it ends. Also covers telling a leak from a cache, a heap that earlier requests filled, and the countdown to running out of memory on a server or on AWS Lambda.

On this page

Memory costs a server render twice. Every object allocated is work for the garbage collector, and a long collection freezes every request in flight. And whatever a render leaves alive grows the heap until the instance slows down or dies. The memory part of a report answers both: who made the garbage, and who keeps what.

The most expensive shape is a cache with no limit, which looks like a leak because it is one:

import { buildMenu, type Menu } from '$lib/server/build-menu';

// before: one entry per request, never removed
// const cache = new Map<string, Menu>();

// after: a bounded cache; the oldest entry goes when it's full
const cache = new Map<string, Menu>();
const MAX = 100;

export function menu_for(locale: string): Menu {
  let m = cache.get(locale);
  if (!m) {
    m = buildMenu(locale);
    cache.set(locale, m);
     if (cache.size > MAX)
      cache.delete(cache.keys().next().value!); 
  }
  return m;
}

Allocators

The top allocators (who creates the objects, and so the garbage), RSS over the window, and every GC pause from the runtime. A long pause freezes every request in flight at once, so when GC is a large slice of busy time, this table says who’s responsible.

It’s grouped by component where the profiler can tell: each allocation is charged to the nearest component on its stack, so ProductCard: 4.2 MB is what rendering that component allocated across the window, not some inner helper. A component that allocates far more than it renders is building something it throws away: a big intermediate array, a string concatenated in a loop, or an object per item.

Findings: gc-heavy when GC is over 15% of busy time and 5 ms per render, gc-pause when one pause is over 20 ms, loop-stall when the event loop’s p99 delay is over 50 ms, and mem-growth when RSS grew over 50 MB during the recording (with nothing kept per render to explain it; record again, and if it grows as much again, something is kept).

GC attribution

A page report connects the GC pauses to the garbage that caused them. The recorder runs V8’s sampling allocation profiler across the window and keeps already-collected objects in the profile, so one read at the end gives the whole allocation stream by stack, not only what survived. The garbage makers are ranked by the pause time they caused, on the rule that each allocator’s share of everything allocated is its share of the pauses. A builtin such as replace or structuredClone is named by the app line that called it, with the component on the stack.

Each pause is listed with when it happened, its kind (minor when the young space fills, major when the old space grows), how much was allocated since the previous one, and what the CPU was running at that moment. The timeline gets a red tick per pause. gc-cause names the top garbage maker when GC took 5 ms or more and that maker caused 3 ms and 15% of it, and says whether the heap grew (something keeps what it allocates) or not (pure churn).

The profile is read only once, at the end, because each read during the work set off a major collection of its own. The profiler’s own garbage is removed from every pause in proportion to its share. gc=0 on a page profile, or heap: false in the options, records without it.

Heap growth

The garbage makers say who allocated over the whole window; this says when. The recorder reads V8’s used-heap figure every 20 ms while a full window runs (a statistic that takes microseconds), and the report draws it across the recording, with collections as bars and the explained render highlighted.

The stretches where the heap grew fastest are listed with what the CPU ran during them, so a burst reads “+29 MB in 20 ms while toVM ran”. A timer only fires when the event loop yields, so each interval falls between two awaits: the longest gap between two readings is reported as the longest synchronous stretch the loop saw. A collection inside an interval shrinks the heap, so that interval’s growth is a lower bound, and the row says so. alloc-burst names a stretch that made 30% or more of a growth of 20 MB or more. In the JSON it’s alloc.

Retained memory

After the runs, the recorder renders the page once more under a live-objects sampler and then forces a full collection. Whatever survives was made by that render and is still held after the warm runs filled any caches: growth on every request. The card names the lines, charges a library’s kept memory to the line of yours that calls it, and counts down: “the heap (4.1 GB, 1.1 GB used) is full after about 43 more requests”. retained-per-render warns at 5 MB or more per render, and the first fixes list it first at 8 MB or more.

Leak or cache. When it finds something and there’s time, it renders up to six more times with a collection after each. A leak climbs every time (“confirmed … about 71 MB a render, and kept climbing”), while a cache with a size limit stops. It tells them apart by reading the same sampler halfway through and at the end: a leaking line doubles between the two reads, a full cache stays put. A line in between gets no verdict rather than a wrong one, and so does a check of fewer than five renders. Calling a line a leak takes six renders, because over five a cache of four entries looks the same as a leak.

The sampler records where a kept object was made, not what holds it. So when something keeps a whole render’s data alive, a line that merely built part of it (a menu tree, a formatted string) shows as kept too. The card lists only lines that hold at least 2% of the total and counts the rest in one sentence. ogygia’s own code is never listed as your line.

When most of the kept memory was made inside a library your line calls, the card says so (”… inside some-lib”), names the library function that allocated it, and suggests library fixes: create its instance once and reuse it, call its cleanup method, turn off its internal cache, upgrade it, or, as a last resort, run it in a worker you replace from time to time.

On AWS Lambda (where Amplify runs SSR), the countdown uses the function’s memory size (AWS_LAMBDA_FUNCTION_MEMORY_SIZE) instead of the heap’s limit, because the platform kills the instance at that size: “the function’s memory (1 GB, 384 MB in use now) runs out after about 10 more requests on one instance”. That request fails, and the next pays for a cold start.

Leftover allocations

The table behind the retained-memory card: the lines that made the objects still alive after the forced collection, with the app caller, the component and the total per render. What keeps them alive is their owner, usually a module-level cache or a registry. In the JSON it’s retained.

Heap pressure

A page that keeps memory on every render also slows every render after it, because a bigger heap costs more to collect and to allocate in. The renders inside one profile can look flat while the next profile on the same server reads far higher: one test page read 720 ms on a fresh server, then 1,020 ms, then 1,310 ms, with the same code.

heap-filled-before warns when a page profile started with 256 MB or more of heap in use, holding six or more renders’ worth of what the page keeps (8 MB or more each). Fix what’s kept, and until then compare only numbers from freshly started servers. A comparison of two such profiles warns that their order skews the result.

When the heap is nearly full, a page profile protects the server: it won’t start at 75%, stops rendering at 85%, and skips its extra renders past 70% (heap-near-limit, Limits).