A Node.js pod crashes with "FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory" under load, but the container limit is 4Gi and usage was 1.5Gi.

AdvancedTroubleshootingDisk, memory and CPU
Answer

Likely causes → V8's old-space limit, not the container, ran out. Depending on the Node version and how it detects memory, the default heap ceiling can be well below the container limit. Under load a large response, an unbounded in-memory cache, or a leak pushes the heap past it.

Diagnose →

kubectl exec api-6f9 -- node -e 'console.log(v8.getHeapStatistics().heap_size_limit/1024/1024)'
kubectl logs api-6f9 --previous | grep -B5 'heap limit'

Take heap snapshots (node --heapsnapshot-signal=SIGUSR2) at low and high load and compare retained objects.

Fix → Set the heap explicitly relative to the container limit (NODE_OPTIONS=--max-old-space-size=3072 for a 4Gi limit) and fix the unbounded allocation (stream large responses, cap caches with LRU).

Prevent → Set --max-old-space-size in the base image from the memory limit, export nodejs_heap_size_used_bytes, and load-test with realistic payload sizes.

All Real-World Interview questions Roadmap: Interview readiness

More on Disk, memory and CPU