A three-worker Node API on an 8 GB server with no swap was killed by the kernel for memory 9 times in 9 days. On the ninth kill, all apps went down for about 3 minutes.

The usual memory graph looked fine the whole time. The cause was a cache that was small next to the box, saved in a way that was not small at all, and then run three times over.

Key facts

  • 9 kernel memory kills in 9 days, on an 8 GB server with no swap.
  • Each of the three workers kept its own 131 MB in-memory cache and rewrote the whole 111 MB cache file every few seconds while busy.
  • Each save made about 230 MB of garbage, and the runtime was allowed to keep it: the heap limit was 2,096 MB.
  • Peak memory per worker: 1.49 / 1.80 / 1.65 GB. The 10-minute memory samples never showed a kill.
  • The fix: no file and no full copy, only a 5,000-entry cache in each worker. Peaks are now 0.65 to 0.87 GB, with no kill since.

What happened

Each worker served an image-matching feature. At start it loaded a 175 MB model. On first use it loaded a cache from a file into memory: 131 MB of live objects once collected. The process manager ran three copies of the app, so there were three models and three caches.

Every kill came when the box’s resident memory reached 7.50 to 7.56 GB. At that moment the three workers together held 3.1 to 5.2 GB. The box normally had 1.5 to 2.5 GB available, so each kill was a spike, not a slow climb to the top.

The kernel kills whichever single process is largest at that moment. That was a worker 4 times, the database 3 times and another background worker 2 times. So the database went down for a problem that was not its own.

Why the usual graph did not show it

The server recorded memory with sar, one sample every 10 minutes. Per day, the mean used was 4,817 to 5,882 MB, and the daily maximum was 5,668 to 7,213 MB. Every kill sat inside a spike between two samples.

The workers moved by 200 to 600 MB within a minute: garbage built up, then got collected. A sample every 10 minutes lands somewhere on that wave, and almost never on its top.

The figure that did show it is the peak since start. Linux keeps it for each process as VmHWM in /proc/<pid>/status. Before the fix it read 1,486, 1,804 and 1,654 MB for the three workers. About 2 minutes after a start, a worker was already at 0.8 to 1.2 GB.

The cause in three steps

  1. Each save copies the whole cache. A save builds a fresh object with every entry, turns it into one JSON string as large as the file (111 MB) and writes it out. In a replay of the same save, one save took the heap from 131 MB to 358 MB in use: about 230 MB of short-lived memory. Resident memory peaked at 539 MB, from a 52 MB baseline.
  2. The runtime keeps the garbage. Garbage is not handed back right away. With a heap limit of 2,096 MB, the runtime can let it pile up before a full collection. In the replay, 30 saves in a row left 464 MB of heap in use and 540 MB resident. Only a forced full collection took it back to 131 MB. On the server, one worker held a single 111 MB block, exactly the file’s size, for over a minute after a write.
  3. Three workers multiply it. Each copy had its own cache and scheduled its own save 5 s after each new entry. During busy bursts the file was written up to 3 times per 5 s. So the cost was three times one worker’s, on a box that had only 1.5 to 2.5 GB to spare.
Per workerBeforeAfter
Cache in memory131 MB, a full copy5,000-entry LRU
Cache file111 MB, rewritten every few seconds while busynone
Garbage per saveabout 230 MBnone
Peak memory1.49 / 1.80 / 1.65 GB0.65 to 0.87 GB
Kernel kills9 in 9 daysnone since

The fix

The data already lived in the database. The file and the full copy in each worker were only there to make lookups fast. So the fix removed both: no file, and no full copy.

Each worker now keeps a 5,000-entry in-process LRU cache of recent entries. A miss reads one row from the database. Nothing is saved to disk, so there is no save to copy.

After the fix, each worker sat at 0.6 GB at 15 minutes. Peaks were 0.65 to 0.87 GB, down from 1.49 / 1.80 / 1.65 GB. There has been no kill since.

Check your own

  • Read the peak, not the average. grep -E 'VmHWM|VmRSS' /proc/<pid>/status shows a process’s peak since start next to its current resident size. A big gap between the two means spikes that a sampled graph will miss.
  • Spot a whole-file rewrite. Look for JSON.stringify next to writeFileSync, or a save on a timer, over a large Map or object. Then check the file’s size and how often it changes: run ls -l --time-style=+%T path/to/file a few times. Each save costs at least a copy of the file in memory. Here, a 111 MB file made about 230 MB of garbage per save.
  • Multiply by the worker count. Every copy the process manager runs has its own cache and its own saves. The real cost is the worker count times the cache plus the garbage of each save. To see how much garbage the runtime may keep, read its heap limit in MB: node -e "console.log(require('v8').getHeapStatistics().heap_size_limit/1048576)".
  • Read the kernel log for kills. journalctl -k | grep -i 'killed process' names the process the kernel killed and its memory at that moment. Do not hunt kills with 10-minute averages.

Common questions

Would a bigger server have fixed it?

Only until the cache grows. The garbage of each save grows with the file, so a bigger box only moves the line.

Why not just force garbage collection?

It treats the symptom. The cost is the full copy and the string built on every save, and a sooner collection still has to make room for both each time.

Does this only happen in Node?

No. Any runtime that turns the whole cache into one string on every save makes the same copy. The numbers change; the shape does not.

Melta is free; get it on the App Store or Google Play.