
OOM (Out-of-Memory) Mechanism Explained
This diagram illustrates how the Linux OOM (Out-of-Memory) Killer operates when the system runs out of memory.
Main Process Flow (Left Side)
- Request
- An application requests memory from the system
- VM Commit (Reserve)
- The system reserves virtual memory
- Overcommit policy allows reservation beyond physical capacity
- First Use (HW mapping) → Page Fault
- Hardware mapping occurs when memory is actually accessed
- Triggers a page fault for physical allocation
- Reclaim/Compaction
- System attempts to free memory through cache, SLAB, writeback, and compaction
- Can be throttled via cgroup memory.high settings
- Swap (if enabled)
- Uses swap space if available and enabled
- OOM Killer
- As a last resort, terminates processes to free memory
Detailed Decision Points (Center & Right Columns)
Memory Request
- App asks for memory
- Controlled via
brk/sbrk,mmap/munmap,mremap, andprlimit(RLIMIT_AS)
Virtual Address Allocation
- Overcommit policy allows reservation beyond physical limits
- Uses
mmap(e.g.,MAP_PRIVATE) withmadvise(MADV_WILLNEED)hints
Physical Memory Allocation
- Checks if zone watermarks are OK
- If yes, maps a physical page; if no, attempts reclamation
- Optional:
mlock/munlock,mprotect,mincore
Any Other Free Memory Space?
- Attempts to free memory via cache/SLAB/writeback/compaction
- May throttle on cgroup memory.high
- Hints:
madvise(MADV_DONTNEED)
Swap Space?
- Checks if swap space is available to offload anonymous pages
- System:
swapon/swapoff; App:mlock*(to avoid swap)
OOM Killer
- Sends SIGKILL to selected victim when below watermarks or cgroup memory.max is hit
- Victim selection based on badness/oom_score_adj
- Configurable via
/proc/<pid>/oom_score_adjandvm.panic_on_oom
Summary
When an app requests memory, Linux first reserves virtual address space (overcommit), then allocates physical memory on first use. If physical memory runs low, the system tries to reclaim pages from caches and swap, but when all else fails, the OOM Killer terminates processes based on their oom_score to free up memory and keep the system running.
#Linux #OOM #MemoryManagement #KernelPanic #SystemAdministration #DevOps #OperatingSystem #Performance #MemoryOptimization #LinuxKernel
With Claude