Skip to content
Latchkey

Node.js worker_threads Out of Memory - Fix Worker Heap Crash in CI

Each Worker runs its own V8 isolate with its own heap. A worker can exhaust memory and abort even while the main thread looks fine - and the limit you set on the parent does not automatically apply to the worker.

What this error means

A job using worker_threads (or a tool that spawns workers, like some test runners and bundlers) crashes with a worker-side heap-out-of-memory or ERR_WORKER_OUT_OF_MEMORY. The parent process did not hit its own limit; the worker did.

Node output
<--- Last few GCs --->
FATAL ERROR: Reached heap limit Allocation failed -
JavaScript heap out of memory
Error [ERR_WORKER_OUT_OF_MEMORY]: Worker terminated due to reaching
memory limit: JS heap out of memory

Common causes

The worker has its own heap limit

Setting --max-old-space-size on the parent does not raise the worker’s limit. A worker created with a low resourceLimits.maxOldGenerationSizeMb (or the default) hits its own ceiling.

Too many concurrent workers for the runner

Spawning many workers in parallel multiplies heap usage; collectively they exceed the runner’s physical memory and one is OOM-terminated.

How to fix it

Raise the worker’s own limit

Set resourceLimits when creating the Worker, or pass execArgv to the worker.

JavaScript
new Worker('./task.js', {
  resourceLimits: { maxOldGenerationSizeMb: 2048 },
})

Reduce concurrency and peak memory

  1. Lower the number of workers a pool spawns at once.
  2. Stream or chunk large inputs instead of holding everything in the worker heap.
  3. Use a runner with more RAM if the workload genuinely needs it.

How to prevent it

  • Size worker resourceLimits relative to runner memory.
  • Cap worker-pool concurrency in CI.
  • Process large data in chunks rather than all at once.

Frequently asked questions

Why did NODE_OPTIONS=--max-old-space-size not help?
That raises the limit for the process that reads NODE_OPTIONS. Workers get their own isolate and their own limit - set it via the worker’s resourceLimits (or its execArgv), not only on the parent.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →