Does PM2 Cluster Mode impact memory overhead when using the max instance setting?
0 reputation · 19 Apr 2022, 15:31 UTC
0 reputation · 19 Apr 2022, 15:31 UTC
PM2 Cluster Mode utilizes the Node.js cluster module to distribute incoming connections across multiple worker processes. When the instances property is set to max in an ecosystem file, PM2 spawns a process for every logical CPU core detected on the host machine.
While this maximizes CPU utilization, each worker operates as a separate process with its own memory heap. In environments where CPU core counts are high but available RAM is limited, this scaling strategy may lead to resource exhaustion.
What is the expected memory overhead per worker process when scaling to the maximum number of cores? Does PM2 provide a mechanism to limit the total aggregate memory usage across the cluster without manually specifying a fixed instance count?
34775 reputation · 19 Apr 2022, 22:38 UTC
Yes – when PM2 runs in cluster mode with instances: max, the total memory footprint grows roughly linearly with the number of logical CPU cores. Each worker is a distinct Node.js process with its own V8 heap, so the master process plus the monitoring stack add only a small, constant overhead.
Because the max setting tells PM2 to spawn one worker per logical CPU core, an 8‑core host will run eight separate Node.js processes. If each worker consumes ~200 MB under load, the cluster will use ~1.6 GB plus the master’s ~15 MB, yielding ~1.6 GB total. Memory leaks in any worker can quickly inflate the aggregate usage.
pm2 start app.js
pm2 monit
Record the RSS (Resident Set Size) shown for the worker.pm2 start app.js -i max
Use pm2 monit or ps -o pid,rss -p $(pm2 pid ) to capture each worker’s RSS.pm2 monit).PM2 itself does not expose a per‑cluster memory cap. The max option simply caps the worker count. To constrain total memory you typically:
instances value that fits your RAM budget.ulimit, Docker resource constraints) to cap each worker or the whole process group.process.memoryUsage() and triggers a reload if a threshold is exceeded.To give a precise recommendation on whether instances: max is safe for your environment, we need the average per‑worker memory usage under realistic load. If you can provide that figure, we can calculate the expected aggregate footprint and advise whether a stricter worker count or OS limits are warranted.
Use comments to ask for clarification. Post a solution as an answer.
5,940 reputation · 20 Apr 2022, 01:54 UTC
To build on the point about linear memory growth, it is critical to note that Node.js V8 heap limits are applied per worker, not globally across the PM2 cluster. If you use instances: max on a high-core machine, the default V8 heap limit could allow the aggregate memory usage to far exceed physical RAM before any single process triggers a garbage collection cycle or crashes.
To prevent system-wide Out-of-Memory (OOM) events, you should explicitly define the --max-old-space-size flag in your ecosystem file. This ensures each worker stays within a predictable budget:
module.exports = {
apps: [{
name: "app",
script: "app.js",
instances: "max",
node_args: "--max-old-space-size=512"
}]
}
Verification: After deploying with this flag, you can verify the limit is active by checking the pm2 monit output under heavy load to ensure workers are capped and not expanding until the host OS kills the processes.