Out of Memory
Production Risk
ENOMEM in production may indicate a memory leak. Profile the application and set appropriate container memory limits.
The system cannot allocate the requested memory. RAM and swap are exhausted, or the process has hit its virtual address space limit (ulimit -v). The Linux OOM killer may terminate processes to relieve memory pressure.
- 1The system is running out of RAM and swap space.
- 2A memory leak in the application has consumed all available heap.
- 3The process virtual memory limit set by ulimit -v is too low.
- 4A large allocation request exceeds the available contiguous memory.
malloc() failing when system RAM and swap are exhausted.
$ ./memory_leak_app malloc: Cannot allocate memory $ dmesg | grep -i oom [12345.678] Out of memory: Kill process 1234 score 900
expected output
malloc: Cannot allocate memory $ echo $? 1
Fix 1
Find and fix memory leaks with valgrind
WHEN When ENOMEM appears after long runtime but not immediately
$ valgrind --leak-check=full --show-leak-kinds=all ./myapp
Why this works
valgrind tracks every allocation and reports those that are never freed, identifying the source of the leak.
Fix 2
Increase swap space
WHEN As a temporary measure on memory-constrained systems
# Create a 4GB swap file sudo fallocate -l 4G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile
Why this works
Additional swap gives the kernel more room to page out cold memory, relieving immediate memory pressure.
✕ Disable the OOM killer to prevent process termination
The OOM killer protects system stability. Disabling it can cause a complete system hang.
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev