Linux ENOMEM (errno 12) — Cannot Allocate Memory (Deep Dive)
About Linux ENOMEM (errno 12)
Deep dive into Linux ENOMEM covering OOM killer behavior, overcommit settings, memory cgroups, swap exhaustion, and kernel memory allocation failures. This guide covers everything you need to know about this topic, including common causes, step-by-step solutions, and answers to frequently asked questions.
Here are the key things to understand: ENOMEM can come from user-space malloc failure or kernel-space allocation failure. Linux overcommit memory by default (vm.overcommit_memory=0), so ENOMEM is rare until truly out. When physical RAM + swap is exhausted, OOM killer selects a process to kill. OOM killer logs in dmesg: 'Out of memory: Kill process [PID] (name) score [N]'. Memory cgroups can impose per-container or per-service memory limits triggering ENOMEM earlier. Understanding these fundamentals will help you diagnose and resolve this issue more effectively.
The most common reasons this occurs include: Physical RAM + swap completely exhausted (system-wide OOM). Memory cgroup limit reached (container/service-level OOM). Memory leak in application consuming ever-increasing memory. Kernel memory (slab, page tables) exhausted despite user-space memory available. vm.overcommit_memory=2 (no overcommit) and commit limit reached. Identifying the root cause is the first step toward finding the right solution.
To resolve this, follow these recommended steps: Check memory usage: free -h to see RAM and swap status. Check OOM killer activity: dmesg | grep -i 'out of memory' or 'oom-killer'. Find memory hogs: ps aux --sort=-%mem | head -20. Add swap space temporarily: sudo fallocate -l 4G /swapfile && sudo chmod 600 /swapfile && sudo mkswap /swapfile && sudo swapon /swapfile. Check cgroup limits: cat /sys/fs/cgroup/memory/memory.limit_in_bytes (cgroup v1) or memory.max (v2). If these steps do not resolve the issue, consider consulting additional resources or a qualified professional.
This article is part of our Linux Error Codes collection on Error Codes Wiki. We provide comprehensive, up-to-date information to help you find solutions quickly.
Quick Answer
Why was my process killed without returning ENOMEM?
Linux overcommits memory. The OOM killer kills processes when memory is truly exhausted, rather than returning ENOMEM at allocation time.
Overview
Deep dive into Linux ENOMEM covering OOM killer behavior, overcommit settings, memory cgroups, swap exhaustion, and kernel memory allocation failures.
Key Details
- ENOMEM can come from user-space malloc failure or kernel-space allocation failure
- Linux overcommit memory by default (vm.overcommit_memory=0), so ENOMEM is rare until truly out
- When physical RAM + swap is exhausted, OOM killer selects a process to kill
- OOM killer logs in dmesg: 'Out of memory: Kill process [PID] (name) score [N]'
- Memory cgroups can impose per-container or per-service memory limits triggering ENOMEM earlier
Common Causes
- Physical RAM + swap completely exhausted (system-wide OOM)
- Memory cgroup limit reached (container/service-level OOM)
- Memory leak in application consuming ever-increasing memory
- Kernel memory (slab, page tables) exhausted despite user-space memory available
- vm.overcommit_memory=2 (no overcommit) and commit limit reached
Steps
- 1Check memory usage: free -h to see RAM and swap status
- 2Check OOM killer activity: dmesg | grep -i 'out of memory' or 'oom-killer'
- 3Find memory hogs: ps aux --sort=-%mem | head -20
- 4Add swap space temporarily: sudo fallocate -l 4G /swapfile && sudo chmod 600 /swapfile && sudo mkswap /swapfile && sudo swapon /swapfile
- 5Check cgroup limits: cat /sys/fs/cgroup/memory/memory.limit_in_bytes (cgroup v1) or memory.max (v2)