Understanding Physical Memory and Swap Memory is foundational for system performance tuning, debugging, and SRE/DevOps roles—crucial for FAANG-level interviews and production troubleshooting.
✅ 1. Fundamentals: Physical Memory vs Swap Memory
✅ 2. Physical Memory (RAM)
🔹 How It Works
RAM stores active processes, kernel code, file caches.
Fast, volatile memory.
When a process is launched, its binary and libraries are loaded into RAM.
Linux tries to use all available RAM for performance (file cache/buffers).
🔹 RAM Is Categorized Into:
🛠️ Check RAM:
free -h
vmstat -s
top
✅ 3. Swap Memory
🔹 Purpose
Acts as backup memory when RAM is exhausted.
Lives on disk: much slower (~100x) than RAM.
Prevents OOM (Out of Memory) kills.
🔹 Swap Use Cases
Systems with limited RAM
Heavy workloads temporarily exceeding RAM
Memory overcommit situations
🔹 Swap Can Be:
🛠️ Check Swap:
swapon --show
free -h
cat /proc/swaps
🛠️ Create Swap File:
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
🛠️ Make Persistent:
Add to /etc/fstab:
/swapfile none swap sw 0 0
✅ 4. Linux Memory Management Flow
User Process
↓
Used RAM
↓
File Cache / Buffers
↓
If RAM full → Swapping
↓
Swap Used
✅ 5. Key Kernel Parameters (sysctl)
🛠️ View & Set:
sysctl vm.swappiness
sysctl -w vm.swappiness=10
✅ 6. Performance & Debugging Tools
✅ 7. Hands-On Use Cases
🔹 1. Check available physical + swap memory
free -h
🔹 2. See if swap is being used
swapon --show
vmstat 1
🔹 3. Add a temporary 1GB swap
fallocate -l 1G /swaptemp
chmod 600 /swaptemp
mkswap /swaptemp
swapon /swaptemp
🔹 4. Disable swap temporarily
swapoff -a
🔹 5. Persist low swappiness value
echo "vm.swappiness=10" >> /etc/sysctl.conf
sysctl -p
🔥 FAANG-Level 20 Questions & Answers
🔸 Conceptual
Q: What is the difference between physical memory and swap?
A: RAM is fast, volatile memory. Swap is slower, disk-based fallback when RAM is full.Q: What is vm.swappiness, and how does it affect memory usage?
A: It controls how aggressively the kernel uses swap. 0 = rarely, 100 = often.Q: Why does free -h show high memory usage even if you're idle?
A: Linux uses free RAM for caching to improve performance.Q: When does Linux start using swap?
A: When free RAM drops below a threshold and swappiness encourages swapping.Q: What does available memory in free -h represent?
A: An estimate of how much memory is safely usable without swapping.
🔸 Debugging & System Management
Q: How to find which processes are using the most memory?
A: Use top, htop, or smem.Q: Why might swap be used even if RAM is available?
A: Kernel may proactively swap based on swappiness or inactive memory.Q: How to disable swap without reboot?
A: sudo swapoff -aQ: How to make a swap file persistent?
A: Add entry in /etc/fstab.Q: How does overcommit memory work?
A: Allows allocating more memory than physically available, based on heuristic or strict mode.
🔸 Performance & Design-Level
Q: What are risks of high swappiness in web servers?
A: Latency spikes due to disk I/O; better to keep active data in RAM.Q: How to identify excessive swapping?
A: vmstat, high si/so values or top showing high swap usage.Q: How to reduce swapping behavior in containers?
A: Use --memory-swappiness=0 with Docker or limit RAM tightly.Q: Should DB servers use swap?
A: Avoid if possible—use tuned RAM sizing; swap causes latency.Q: How does NUMA affect memory allocation?
A: Memory locality affects latency in multi-socket systems; tuning needed for performance.
🔸 Edge Cases & Advanced Internals
Q: What is "OOM killer", and when does it trigger?
A: If memory + swap are exhausted, kernel kills processes to free RAM.Q: How do you tune swap usage for high-performance workloads?
A: Lower vm.swappiness, add more RAM, disable swap if safe.Q: Can you have swap without a partition?
A: Yes, using a swap file.Q: What is memory ballooning in virtual machines?
A: Dynamic memory resizing—can cause swap usage if host is oversubscribed.Q: How does memory fragmentation impact performance?
A: Kernel may fail to allocate large contiguous memory blocks → can be tuned via hugepages.
✅ Summary Cheat Sheet
Would you like:
A Bash script to monitor memory and auto-disable swap if overused?
A memory tuning checklist for Kubernetes/containers?
Or a live performance lab with memory exhaustion simulation?