Fixing Vercel Builds That Run Out of Memory

A static site build on Vercel dies at the same point every few deploys: the log stops in the middle of image processing, and the final line says the build exceeded the memory limit, or simply that it was killed. Locally on a 32 GB laptop the same build passes every time. Vercel's build containers have a fixed amount of memory for each plan's machine size, and a static build that peaks above it is killed by the container, not by Node — so there is often no stack trace and no indication of what used the memory.

This guide fixes out-of-memory kills on a Next.js static export of a 6,500-page product catalogue on Vercel, where about one build in four failed. It shows how to measure peak memory, the two causes found — image processing concurrency and a large data file loaded whole — and the settings that keep the build well under the limit. It belongs to Netlify vs Vercel Deployment Strategies.

Symptoms

  • Build log ends with Error: Command "npm run build" exited with SIGKILL during "Generating static pages", around page 4,000 of 6,500.
  • On some runs, instead: FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory.
  • The project had NODE_OPTIONS=--max-old-space-size=8192 set months earlier "to fix memory errors", on a build machine with 8 GB of memory.

That last point was the first problem: an 8 GB heap limit on an 8 GB machine means Node never stops itself; the container kills it first, with no useful message.

Step 1: Measure Peak Memory Locally Under the Same Limit

Reproduce the container's limit locally, with the same Node version, and record peak memory:

docker run --rm -m 8g -v "$PWD":/app -w /app node:20 bash -c \
  'npm ci && /usr/bin/time -v npm run build 2>&1 | tail -25' | grep -E 'Maximum resident|Elapsed|Exit status'

The build was killed at 8 GB in the container, as on Vercel. Without the limit, peak resident memory was 11.2 GB. Sampling memory every second with pidstat -r during the unconstrained run showed two spikes.

Memory attributed to each part of the build at peak Horizontal bars of gigabytes. Image processing native memory with eight images decoded at once 5.4 gigabytes. The product catalogue JSON loaded and parsed whole 2.9 gigabytes. Next.js page generation workers 2.1 gigabytes. Other 0.8 gigabytes. What used the memory at peak Image processing (native) 5.4 GB Catalogue JSON in memory 2.9 GB Page generation workers 2.1 GB Other 0.8 GB
Attributed with pidstat sampling and heap snapshots during an unconstrained local build; total peak 11.2 GB.

Cause 1: Unbounded Image Processing Concurrency

The site's prebuild script resized 7,800 product photos with sharp into three widths, using Promise.all over batches of 200. sharp's own thread pool used all CPUs, and each in-flight image held its decoded pixels in native memory — invisible to Node's heap limit. Supplier photos were often 5,000–6,000 pixels wide, about 90 MB decoded each. With dozens in flight, native memory reached 5.4 GB.

Bounding concurrency and telling sharp to limit its pixel cache fixed it:

import sharp from 'sharp';
import pLimit from 'p-limit';
sharp.concurrency(2);           // threads per image operation
sharp.cache({ memory: 50 });    // MB of libvips cache
const limit = pLimit(4);        // images in flight
await Promise.all(files.map((f) => limit(() => resize(f))));

Peak native memory for image processing fell to 0.9 GB, and the step took only 12% longer, because it had been memory-bound rather than CPU-bound. Caching the resized output between builds then removed most of the work anyway, as in Caching Processed Images Between Builds.

Cause 2: A Large Data File Loaded Whole

Every product page's generateStaticParams and page component imported catalogue.json — 640 MB, exported nightly from the product database. Next.js's static generation workers each loaded it, and the parsed objects took several times the file size in memory. The fix was to split the data at build start into one small JSON file per product plus a compact index:

// scripts/split-catalogue.mjs (runs before next build)
import { createReadStream } from 'node:fs';
import { parser } from 'stream-json';
import { streamArray } from 'stream-json/streamers/StreamArray';
const index = [];
for await (const { value: p } of createReadStream('data/catalogue.json').pipe(parser()).pipe(streamArray())) {
  await fs.writeFile(`data/products/${p.slug}.json`, JSON.stringify(p));
  index.push({ slug: p.slug, name: p.name, category: p.category });
}
await fs.writeFile('data/index.json', JSON.stringify(index));

generateStaticParams reads the 1.1 MB index; each page reads only its own product file. Streaming the parse keeps the split step itself under 200 MB. The same pattern applies to any generator; Fixing Eleventy Out-of-Memory Errors on Large Collections shows it in Eleventy.

Peak resident memory of the build before and after each change Paired bars in gigabytes. Bounding image concurrency took peak memory from 11.2 to 6.7. Splitting the catalogue data took it from 6.7 to 3.6. Setting the Node heap limit to 5 gigabytes with headroom kept it at 3.6 with clean errors if exceeded. Peak memory after each fix Before change After change Bound image concurrency 11.2 GB 6.7 GB Split catalogue JSON 6.7 GB 3.6 GB
Measured with /usr/bin/time -v in a container limited to 8 GB, same Node version as Vercel.

Step 2: Set the Heap Limit With Headroom

With peak memory at 3.6 GB, the heap limit was set to leave room for native memory:

NODE_OPTIONS=--max-old-space-size=5120

5 GB of heap on an 8 GB machine leaves about 3 GB for sharp, esbuild, worker processes and the OS. If the heap grows past 5 GB in future, Node fails with a clear heap error and stack trace instead of an anonymous kill. A CI job records peak memory of every main-branch build and warns at 6 GB.

Step 3: Limit Static Generation Workers if Needed

Next.js generates static pages in parallel workers, each a separate process with its own memory. On an 8-core build machine, that is several copies of whatever each page imports. After the data split this was no longer a problem, but experimental.cpus in next.config.js can cap the worker count when page modules are inherently large. Other generators have equivalents: Astro builds pages in one process; Hugo uses goroutines in one process; Eleventy has no worker pool.

Decision tree for diagnosing an out-of-memory build on Vercel The root asks how the build died. A SIGKILL with no stack trace means the container limit was hit, often by native memory such as image processing, so measure peak resident memory and bound concurrency. A JavaScript heap out of memory error means the Node heap limit was hit, so take a heap snapshot and find large retained data. A heap limit set above the machine's memory hides the second case behind the first, so lower it first. Reading an out-of-memory failure How did the build die? SIGKILL / no trace Container limit native memory, concurrency Heap out of memory Node heap limit heap snapshot Heap limit ≥ machine RAM Lower it first get readable errors
Lowering an oversized heap limit is often the first step, because it turns silent kills into readable errors.

Measured Impact

MeasureBeforeAfterTool
Builds killed for memory~1 in 40 in 60Vercel deploy history
Peak memory (8 GB container)killed (11.2 GB unconstrained)3.6 GB/usr/bin/time -v
Build time9 min 40 s (when it passed)6 min 10 sVercel build log
Data loaded per page640 MB file~6 KB product file—

The build got faster as well: less memory pressure meant fewer garbage collection pauses, and per-product files were quicker to read than re-parsing the whole catalogue.

Peak memory has stayed between 3.4 and 3.9 GB over the two months since, while the catalogue grew by 600 products.

Local reproduction in a memory-limited container was the step that made everything else possible; guessing on Vercel had cost two days.

Pitfalls & Rollback

  • Heap limits at or above machine memory. Errors become silent kills.
  • Unbounded Promise.all over images. Native memory is outside the heap limit.
  • Importing huge data files in every page. Split them and load per page.
  • Upgrading the machine first. It costs more and the next data growth hits the new limit.
  • Rollback: each change is independent; reverting one shows its memory effect clearly in the recorded peak.

Conclusion

Static builds killed for memory on Vercel usually combine an oversized heap limit that hides the error, image processing with unbounded concurrency using native memory, and a large data file loaded whole into every page worker. Bounding sharp's concurrency, splitting the catalogue into per-product files and setting the heap limit with headroom took peak memory from over 11 GB to 3.6 GB, ended the failures and cut build time by a third — on the same machine size.

FAQ

How do I know a Vercel build ran out of memory?

The build log ends with a message that the build was killed or that it exceeded the memory limit, sometimes with SIGKILL or exit code 137 and no stack trace, or a JavaScript heap out of memory error from Node. The first means the container hit its memory limit; the second means Node's heap limit was reached before that.

Should I raise Node's max-old-space-size on Vercel?

Only up to a value safely below the container's memory, leaving room for native memory used by sharp, esbuild and the rest of the process. Raising it above the container limit turns a clear heap error into an opaque kill. Reduce peak memory first, then set the heap limit with headroom.

Why does image processing use so much memory?

Image libraries decode each image into raw pixels, which for a 6,000 by 4,000 photo is about 96 megabytes, and process several images in parallel, one per CPU by default. A few large originals processed at once can use gigabytes of native memory that the Node heap limit does not control.

Can I get a bigger build machine on Vercel?

Some plans offer larger build machines with more memory and CPUs, at a higher cost. They help when the build genuinely needs more memory. Most out-of-memory static builds instead have one or two causes, such as unbounded image concurrency or loading a large dataset at once, that are cheaper to fix.

Filed underFixesVercel