Memory Management
Virtual memory, page cache, OOM killer, swap, huge pages, and container memory limits.
Overview
Linux uses a virtual memory system where every process sees a flat 64-bit address space. Physical RAM is managed in 4 KB pages.
Page Cache
The page cache is the kernel's disk cache. Linux deliberately keeps almost no RAM truly idle. free -h shows buffers and cached columns. available memory (not free) is the realistic estimate of memory new processes can use.
Memory Pressure and Reclaim
Memory pressure triggers reclaim: kswapd daemon scans and reclaims LRU pages. File-backed pages are simply dropped. Anonymous pages (heap, stack) must be written to swap before reclaim.
OOM Killer
The OOM killer activates when the system cannot reclaim enough memory. To protect a critical process:
Huge Pages
Transparent Huge Pages (THP) can cause latency spikes — databases (Oracle, Cassandra, MongoDB) recommend disabling:
Key concepts
- Page cache
- Kernel uses all free RAM as a disk cache. The available column in free -h (not free) is what matters.
- OOM killer
- Kills a process when memory cannot be reclaimed. Selects victim by oom_score. Adjust oom_score_adj per-process (-1000 to +1000).
- Anonymous vs file-backed pages
- Anonymous pages (heap/stack) must be swapped to disk before reclaim. File-backed pages can be dropped and re-read — much cheaper.
- Transparent Huge Pages (THP)
- 2 MB pages managed automatically. Can cause latency spikes in databases. Disable with echo never > /sys/kernel/mm/transparent_hugepage/enabled.
- cgroup memory limit
- Hard memory cap per container. When exceeded, an OOM kill occurs within the cgroup, visible in dmesg.
When to use it
- Explaining why a server with 2% "free" RAM is not out of memory — the page cache fills free RAM
- Debugging container OOMKilled events — memory limit too low or memory leak
- Recommending huge pages for a database workload to reduce TLB misses
Interview questions
- Your Java application in Kubernetes keeps getting OOMKilled. How do you diagnose and fix it?
- What is the Linux OOM killer and how do you control which process it kills?
Sign up free to read the full topic
More Linux topics
Start free on Cariara · Coding problems · System design studio