Why Long-Running PHP Worker Processes See Stale Config and Cached Data, and How to Fix It
A long-running PHP worker is a PHP process that stays alive while it handles multiple jobs or requests. Queue workers and application…
When a Node.js service runs out of memory, the phrase “out of memory” does not tell you what failed. V8, Node.js’s JavaScript engine, stops the process when the JavaScript heap hits its limit and V8 cannot free enough space. A container or operating system can also kill the process when the whole process runs out of memory. You need a different fix for each cause.
A JavaScript memory leak happens when your code keeps a reference to an object it no longer needs. While that reference stays alive, V8’s garbage collector cannot free the object. To fix a V8 out-of-memory crash, you have to find which part of memory is growing and what keeps those objects in use, then remove the reference or cap how much data it can hold. If you raise the heap limit, you give V8 more room for now, though you still have not fixed the leak. You also have to leave enough memory for the rest of the process.
Start by checking Node’s error output and how the process ended. When V8 reaches its heap limit, Node prints a fatal out-of-memory message. A container or operating system can kill the process without printing that message. If you run in Docker, check the container’s OOMKilled status and its memory limit. The Docker documentation explains how Linux kills processes when a container runs out of available memory.
Do not assume that every Node.js out-of-memory crash ends with SIGABRT. Record the actual exit code or signal, Node.js stderr, and any container or host events. Those details help you tell a V8 failure apart from a kill by the environment.
A single memory reading cannot show where Node.js uses memory. Track process.memoryUsage() over time while your service handles typical traffic. Node reports five main values, which the Node.js Process API explains in detail:
heapUsed and heapTotal for V8’s JavaScript heapexternal for native memory tied to JavaScript objectsarrayBuffers for memory used by Buffer and other array buffersrss for the process’s resident set size (total resident memory)Compare these trends over time, especially after garbage collection, when V8 frees objects your code no longer needs. If heapUsed keeps climbing after repeated collections, look for JavaScript objects that stay in use. If external or arrayBuffers grows instead, inspect buffers and native memory allocations. If rss rises while the heap stays flat, look outside the JavaScript heap. Node documents that memory fragmentation in glibc-based environments can push rss up even when heapTotal stays stable, so that pattern alone does not prove what caused a specific incident.
Worker threads change how you read these numbers. rss covers the whole process, while the other process.memoryUsage() fields cover only the current thread. If your service uses workers, collect measurements for each isolate, which is the separate JavaScript execution environment each worker runs in. One thread’s heap figures do not represent the whole process.
You can also log v8.getHeapStatistics() to track used_heap_size, heap_size_limit, and total_available_size. The last value shows how much space V8 has left before it hits its limit. A rising number_of_native_contexts or a nonzero number_of_detached_contexts can point to a context leak, though neither tells you which object is responsible. See the Node.js V8 API for the full list of fields.
In a controlled environment, use garbage-collection traces to see whether V8 frees memory as your workload runs. The Node.js GC-trace guide explains how to run with --trace-gc and read repeated collections. Watch for old-space usage that keeps climbing and collections that free very little memory. Old space is where V8 stores objects that stay in use longer.
To compare memory directly, take a heap snapshot after the service starts, run the operation you suspect, and take a second snapshot. A heap snapshot records the objects in one V8 isolate and the references between them. Compare the two snapshots in Chrome DevTools’ Memory panel. Look for jumps in object counts or sizes, then inspect the reference path to see what still points to those objects and stops V8 from freeing them.
Common places to check include long-lived maps and caches, listeners and callbacks, saved request or session data, and queues or batches with no size limit. Treat these as places to look, not proof of a leak. When the code that owns a reference finishes, remove that reference. Put size limits on data structures that could keep growing, and make sure listeners or callbacks do not outlive the objects that own them. Then run the same workload again and confirm that memory settles at a steady level after garbage collection.
Node’s V8 API warns that V8 can use about twice the current heap size when it creates a snapshot, and it blocks the event loop until the snapshot finishes. A snapshot of the main thread also leaves out worker-thread heaps. Test snapshot capture on a disposable copy or in staging before you turn it on in production. If you pass --heapsnapshot-near-heap-limit, Node tries to write snapshots as the heap nears its limit, though Node does not guarantee it will write every requested snapshot, and writing them uses memory.
The --max-old-space-size option sets V8’s old-space limit in mebibytes. When you raise this limit, you give V8 more room, but Node’s CLI documentation warns that V8 takes longer to run garbage collection as memory use approaches the ceiling. Giving V8 a larger heap also leaves less memory for buffers, native allocations, worker threads, code, and the operating system.
Test your real workload against the container’s or machine’s memory limit to choose a heap limit. Node’s documentation uses 1536 MiB as an example for a 2 GiB machine so other parts of the system still have room. That example does not fit every machine. Do not copy a heap size from an older deployment or assume the same default applies to every Node.js version, architecture, and environment.
If you raise the limit, you buy time, though you still have not fixed a leak. If heap use keeps climbing until V8 hits the new ceiling, your code still holds more data than the process can sustain. After you change the setting, check the memory trend again and verify that the process stays within its total memory budget.
For fatal runtime errors, Node’s --report-on-fatalerror option tells Node to write a diagnostic report that helps explain an out-of-memory failure. You can also capture a snapshot after a signal or as the heap nears its limit. First verify that the machine has enough disk space and memory, and check how capture affects runtime. A process close to its memory limit can crash before it finishes saving diagnostic data.
Treat reports and snapshots as sensitive files, and restrict access to any endpoint that creates a snapshot. Those files can hold service data and object references that expose internal state.
Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com