Memory Management — Virtual Memory, Paging, and Allocation
Memory Management — Virtual Memory, Paging, and Allocation
🎯 After Reading This Lesson
After finishing this lesson, you will be able to confidently handle the following three topics.
- ▸✅ Stack / Heap / Code / Data segments
- ▸✅ Paging vs Segmentation + Virtual Memory
- ▸✅ Page Fault + LRU page replacement
Keep the learning goals as a checklist, and close the lesson once you can answer all of them.
Virtual Memory — *The Illusion of Infinite Memory*
One-liner: The illusion that each process owns all of memory. In reality, the OS maps pages to physical RAM.
Why it is needed:
6 steps to access a variable (e.g., int x = 5; printf("%d", x);):
1. Virtual address — &x = 0x7fff5fbff8ac (stack segment)
2. MMU translation — the Memory Management Unit looks up the page table
3. TLB cache — frequently used mappings are cached in the CPU (1-cycle hit)
4. Physical address — 0x7fff5fbff8ac → physical RAM 0x12345678
5. Cache lookup — L1 → L2 → L3 → RAM (4 → 12 → 40 → 200 cycles)
6. Value returned — 5 is loaded into a register
> 💡 Page Fault = no mapping in the page table → OS loads from disk → very slow (several ms).
Paging — Memory Management in 4 KB Units
Page = 4 KB (Linux standard). Both virtual and physical memory are managed in page-sized units.
Page table structure:
- ▸A separate page table for each process
- ▸Virtual page number → physical page number + permissions (r/w/x)
- ▸64-bit systems = 4-level page table (PML4 → PDPT → PD → PT)
TLB (Translation Lookaside Buffer):
- ▸A cache of page mappings inside the CPU (typically 64–1024 entries)
- ▸1 cycle on a hit; page table walk on a miss (10–100 cycles)
Paging pitfalls:
Huge Pages (2 MB / 1 GB):
- ▸Used by databases and JVMs that handle large amounts of memory
- ▸Fewer TLB misses → better performance
- ▸Linux:
echo 1024 > /proc/sys/vm/nr_hugepages
Memory Leaks + OOM Debugging
Memory Leak: allocated memory that is never freed → accumulates over time → eventually causes OOM.
Risk by language:
Leak diagnosis tools:
- ▸Linux:
valgrind --leak-check=full ./app(C/C++) - ▸Java:
jmap -heap <PID>→jhator VisualVM - ▸Python:
tracemallocmodule orobjgraph - ▸Node.js:
--inspect+ Chrome DevTools Heap Snapshot - ▸All languages: If RSS in
toporhtopkeeps rising over time, suspect a leak
OOM Killer behavior:
1. RAM + swap are nearly exhausted
2. The kernel computes an oom_score (based on memory usage and priority)
3. The process with the highest score receives SIGKILL
4. Logged in /var/log/syslog or dmesg (Out of memory: Kill process ...)
> 💡 On a production server, set vm.swappiness=10 (minimize swapping) and configure memory monitoring alerts.
🤖 Try Asking AI Like This
Knowing the concepts in this lesson lets you give AI specific, precise instructions. Instead of a vague "fix this," you can make vocabulary-driven requests — and that is where token savings start.
- ▸"Analyze the memory usage pattern (heap/stack) of this code"
- ▸"Tell me the command to measure RSS / Virtual Memory of this process"
Why This Reduces Tokens
Without the right concepts, you have to ask "What does that mean?" after every AI response. Those follow-up questions eat up tokens. Learn the concept once, and the conversation wraps up in one go.