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.
<--- 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 memoryCommon 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.
new Worker('./task.js', {
resourceLimits: { maxOldGenerationSizeMb: 2048 },
})Reduce concurrency and peak memory
- Lower the number of workers a pool spawns at once.
- Stream or chunk large inputs instead of holding everything in the worker heap.
- 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.