Catching a memory leak in the pull request that adds it
Quick answer
Retained heap is a number a shared CI runner can repeat. Across 13 jobs on GitHub-hosted runners, a route that keeps 8 KB per request measured +7.91 MB per 1,000 requests on every Node 22 job and +8.04 to +8.06 on every Node 24 job. That is steady enough to measure every pull request against its base branch, so I added a GitHub Action to next-leak. On a pull request that made a healthy route retain memory, its comment read worse: was stable (+0.19 MB/1000 req), 93 seconds into the step.
Why I didn't expect this to work
Performance checks in CI have a bad name, and they earned it. A hosted runner is a shared machine and its timings drift, so any threshold on time is either loose enough to miss regressions or tight enough to fail at random. In the jobs below, the same step took between 89 and 95 seconds.
Memory leaks looked like the same problem with a worse cost. Every leak I've measured so far (the one in Next.js 16.3.0 was the last) I measured after the fact, from an issue someone opened once production had found it. The pull request that introduced it was long merged.
The cheap moment to catch a leak is the diff that adds it. Whether that's possible comes down to one question: how much does the same measurement move between two runners?
The same three routes on 13 runners
The repository of next-leak tests its own action on every CI run. The target is a small server laid out like a Next.js standalone build, with three routes: / does nothing, /cached fills a store that has a ceiling, and /leaky pushes an 8 KB string into a module-level array on every request.
Each job measures them with 4 cycles of 300 requests, a forced GC before every sample, and each route in a fresh process. These are the 13 jobs from 2 December 2026 that printed the full table, seven on Node 22.23.5 and six on Node 24.22.3:
| Route | Node 22.23 (7 jobs) | Node 24.22 (6 jobs) | Verdict, all 13 |
|---|---|---|---|
/ | +0.07 in all seven | +0.18 to +0.20 | stable |
/cached | +0.88 in all seven | +0.91 in five, +0.69 in one | saturating |
/leaky | +7.91 in all seven | +8.04 to +8.06 | leak |
All values are MB per 1,000 requests. The threshold for calling growth a leak sits at +0.83, so the healthy route is four times under it and the leaking one more than nine times over. No runner came close to moving a route across the line.
Two things in that table are worth more than the headline.
First, the Node version matters more than the runner. The gap between Node 22 and Node 24 on /leaky is 0.14 MB; the gap between runners on the same version is 0.02 at most. A base run and a pull request run on different Node versions would compare the versions, not the code.
Second, the one number that did move is the honest one. /cached read +0.69 once instead of +0.91. That route is a cache still filling up, so its growth depends on where the sampling window falls. The verdict held (saturating, not leak) because the tool re-measures with eight cycles when growth is still slowing down, but I wouldn't gate on the third digit of a route like that.
A pull request that starts leaking
Repeatable numbers make a comparison possible. Runs on the default branch store their result in the Actions cache, keyed by commit; a pull request run reads the one from its base commit and compares route by route.
To see it on GitHub I opened a pull request that makes the healthy route keep 8 KB per request:
visits.push(Buffer.alloc(8192, visits.length & 0xff).toString("latin1"));
This is the comment the action left, unedited:
/ went from +0.19 to +8.05 MB per 1,000 requests and the last column says so: worse: was stable. The other two rows matter as much. /leaky is a leak too, but it was already one on the base branch, with the same number. Without that column the pull request gets blamed for a leak it only measured.
The step took 93 seconds, including the base lookup, three routes and the comment. Over 19 jobs it ranged from 89 to 95.
Setting it up
The app needs output: "standalone" in next.config. The workflow has to run on pushes to the default branch as well as on pull requests, with the same arguments, or there is no base run to compare with:
on: push: branches: [main] pull_request:permissions: contents: read pull-requests: write # only for the commentjobs: memory: runs-on: ubuntu-latest steps: - uses: actions/checkout@v7 - uses: actions/setup-node@v7 with: node-version: 24 - run: npm ci && npm run build - uses: xabierlameiro/next-leak@v0.22.0 with: args: --routes /api,/blog --quick
The comment is rewritten on every push instead of piling up, the full HTML report is uploaded as an artifact of the run, and the job fails when a route's verdict is leak. Set fail-on: "" to report without failing.
Where this stops applying
- The 13 jobs measured a fixture, not a real app. It allocates the same bytes on every request, so that spread is the floor of the method. A real Next.js route has more going on. The closest I have is four different builds of one Next.js app from the bisect, which landed between +0.55 and +0.58 MB per 1,000 requests, but those ran on one machine, not on hosted runners.
- The job fails on any leak, old ones included. The comment tells you which ones are yours; the exit code doesn't yet.
- A check like this finds leaks that a few hundred requests to a route can trigger. A leak that needs a specific payload, a logged-in session or a day of uptime won't show up.
- Pull requests from forks get a read-only token. The measurement still runs and lands in the job summary, without the comment.
- It costs CI minutes per route. Name the routes that matter with
--routesrather than measuring all of them on every push.
The runs behind every number here are public: they are the verify jobs of the CI workflow in xabierlameiro/next-leak on 2 December 2026, and the action is in the GitHub Marketplace.
