ENOMEM
Linux / POSIXERRORCriticalResourcesHIGH confidence

Out of Memory

Production Risk

ENOMEM in production may indicate a memory leak. Profile the application and set appropriate container memory limits.

What this means

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.

Why it happens
  1. 1The system is running out of RAM and swap space.
  2. 2A memory leak in the application has consumed all available heap.
  3. 3The process virtual memory limit set by ulimit -v is too low.
  4. 4A large allocation request exceeds the available contiguous memory.
How to reproduce

malloc() failing when system RAM and swap are exhausted.

trigger — this will error
trigger — this will error
$ ./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

Find and fix memory leaks with valgrind
$ 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

Increase swap space
# 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.

What not to do

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

← All Linux / POSIX errors