Virtual Memory and Swap: The Trick for Small Desks
"Can I run PUBG on 8GB RAM?"
My computer has 8GB of RAM. Yet it doesn't crash when I run PUBG, 30 Chrome tabs, and YouTube all at once. Combined, they easily exceed 12GB. I was genuinely puzzled at first. How can programs that need 12GB run on 8GB of physical RAM?
The answer was simple. The Operating System was lying. "Don't worry, I have infinite RAM. Use all you want."
This is Virtual Memory.
1. Why I Studied Virtual Memory
My journey into virtual memory started with an AWS EC2 t2.micro instance. With only 1GB RAM, I was running a Node.js server that kept crashing. The error log showed a terrifying message: OOM killed process.
Google told me to "configure Swap memory." So I did. The server stopped dying, but response times became glacial. Something didn't add up. "Doesn't swap increase memory? Why is it slower?"
That question pulled me into the world of virtual memory and swap.
2. What Confused Me at First
Here's what tripped me up initially.
Confusion 1: "Virtual Memory = Swap?"
I thought these were the same thing. Wrong. Virtual memory is a concept, swap is one implementation method.
- Virtual Memory: The illusion that "we have infinite RAM"
- Swap: The technique of secretly using the hard disk to maintain that illusion
Confusion 2: "Swap increases memory, so why the slowdown?"
I completely misunderstood this. Swap doesn't increase memory—it prevents death from memory shortage by sacrificing speed.
- RAM speed: Nanoseconds (10⁻⁹ seconds)
- SSD speed: Microseconds (10⁻⁶ seconds)
- HDD speed: Milliseconds (10⁻³ seconds)
RAM vs SSD: 1,000x difference. RAM vs HDD: 1,000,000x difference.
Confusion 3: "So I should just allocate huge swap?"
Also wrong. Bigger swap isn't better. The moment swap is actually used, the system slows down. Swap is for emergency use only. The real fix is adding more RAM.
3. The Turning Point: Desk and Drawer Metaphor
Then I heard this metaphor and everything clicked.
Metaphor 1: The Small Library Desk
Imagine you're studying at a library.
- Physical Memory (RAM): Your small desk. Fits only 3 open books.
- Hard Disk (HDD/SSD): The massive library shelves holding thousands of books.
- Process (Program): A student who needs 10 books open simultaneously.
What if the desk is small but you need 10 books?
The OS tricks the student: "Here's a fake desk (virtual memory) that fits 10 books." In reality, it only places the one book you're reading right now on the real desk (RAM).
The other 9 books? They stay on the shelves. When the student wants a different book, the OS moves at lightning speed:
- Takes an unused book from the desk back to the shelves. (Swap Out)
- Brings the requested book from the shelves to the desk. (Swap In)
This is Swapping.
Metaphor 2: Hotel Overbooking
A hotel has 100 rooms but accepts 150 reservations. "They won't all show up. Some will be no-shows."
This is virtual memory. The OS promises each process "you can use 4GB," but actual RAM is only 8GB. The OS calculates: "They won't all use it. Some will be idle."
But what if all guests actually show up? The hotel panics. Same with the OS. This is Thrashing.
Metaphor 3: Warehouse with Workbench
You're a technician repairing items in a warehouse.
- RAM: A small workbench. Fits 3 items.
- Disk: A giant warehouse with thousands of items.
- Page Fault: "Oh, this item isn't on the workbench? I need to fetch it from the warehouse."
If the workbench is small? You constantly swap items. This is slow because the round trip to the warehouse takes forever.
These metaphors organized everything in my head. Ultimately, virtual memory is an illusion, and swap is the price paid to maintain that illusion.
4. Deep Dive: How Virtual Memory Works
Let me go deeper now.
4.1 Address Translation
Processes see Virtual Addresses. For example, a program requests: "Read the value at memory address 0x1000."
But this address is fake. It's not a real RAM address.
The OS uses a Page Table to translate the virtual address to a Physical Address.
Virtual Address 0x1000 → Page Table Lookup → Physical Address 0x3000
This translation is handled by hardware called the MMU (Memory Management Unit).
4.2 Page Table
Memory is divided into units called Pages, typically 4KB each.
A page table looks like this:
| Virtual Page # | Physical Page # | Status |
|---|---|---|
| 0 | 3 | In RAM |
| 1 | - | On Disk (Swap) |
| 2 | 7 | In RAM |
| 3 | - | On Disk (Swap) |
When a process tries to read virtual page 1, the page table says "On Disk." This is a Page Fault.
4.3 Page Fault
When a page fault occurs, the OS:
- Triggers an interrupt: CPU stops what it's doing.
- Swap In: Fetches the page from disk to RAM.
- Updates page table: Marks "now in RAM."
- Resumes process: Continues execution.
This process is extremely slow because disk I/O is thousands of times slower than RAM access.
4.4 Demand Paging
The OS loads pages only when needed (on demand). When launching a program, it doesn't load everything into RAM—just the immediately needed parts.
For example, Chrome might be 100MB total, but only 10MB is actively used right now. The OS loads only 10MB into RAM. The remaining 90MB stays on disk.
When the user accesses a new feature, the OS loads those pages into RAM then. This is Demand Paging.
4.5 Page Replacement Algorithms
If RAM is full but a new page must be loaded, an existing page must be evicted. Which page to evict is decided by page replacement algorithms.
Common algorithms:
- FIFO (First In First Out): Evicts the oldest page. Simple but inefficient.
- LRU (Least Recently Used): Evicts the least recently used page. Most commonly used in practice.
- LFU (Least Frequently Used): Evicts the least frequently used page.
Linux primarily uses LRU variants.
5. Swap Space
Let me organize my understanding of swap space.
5.1 Swap Partition vs Swap File
There are two ways to create swap space.
Swap Partition
Dedicates part of the disk exclusively for swap. Even formatted with a special swap format.
Pros:
- Slightly better performance (no filesystem overhead).
- Less fragmentation.
Cons:
- Hard to resize (requires repartitioning).
Swap File
Creates a large file within a regular filesystem to use as swap.
Pros:
- Easy to resize (just delete and recreate the file).
- Simple setup.
Cons:
- Slightly worse performance (filesystem overhead).
Modern systems prefer Swap Files, especially in cloud environments.
5.2 How Much Swap?
Old rule: "2x RAM" Modern rule: "It depends"
My accepted principle:
- Dev servers: ~50% of RAM. Buffer for occasional memory spikes.
- Production servers: ~20-30% of RAM. Insurance against OOM. Alert if swap is actually used.
- Database servers: Minimal swap. Performance tanks the moment swap is used.
6. Real-World Application
6.1 Configuring Swap on AWS EC2
Here's what I did:
# 1. Create swap file (1GB)
sudo dd if=/dev/zero of=/swapfile bs=1M count=1024
# 2. Set permissions (security)
sudo chmod 600 /swapfile
# 3. Format as swap
sudo mkswap /swapfile
# 4. Enable swap
sudo swapon /swapfile
# 5. Check status
free -h
Output:
total used free shared buff/cache available
Mem: 1.0G 500M 200M 10M 300M 400M
Swap: 1.0G 0B 1.0G
6.2 Swappiness Setting
Swappiness determines "when to start using swap." Range: 0-100.
- 100: Aggressively uses swap even with available RAM.
- 0: Avoids swap until RAM is completely full.
- Default (Ubuntu): 60
I use 10: "Use swap only in emergencies."
# Check current value
cat /proc/sys/vm/swappiness
# Temporary change (resets on reboot)
sudo sysctl vm.swappiness=10
# Permanent change
echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf
6.3 Monitoring Swap Usage
# Check swap usage
free -h
# Find processes using swap
for file in /proc/*/status ; do
awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file
done | sort -k 2 -n -r | head -10
This script shows "which processes are using the most swap." I ran this on production and found my Node.js process consuming 500MB of swap. That moment: "Ah, that's why it was slow."
6.4 Memory Limits in Docker
Docker containers can have memory limits:
# Limit to 512MB RAM
docker run -m 512m my-app
# Memory + swap limit (total 1GB)
docker run -m 512m --memory-swap 1g my-app
--memory-swap is the total of memory + swap. The example above means "512MB memory + 512MB swap."
Without --memory-swap, Docker automatically sets swap to 2x memory.
6.5 OOM Killer
Linux has a terrifying feature: the OOM Killer. When "Out of Memory" occurs, the OS forcibly kills a process.
Which process gets killed? The one with the highest OOM Score.
# Check OOM scores of all processes
ps -eo pid,comm,oom_score | sort -k3 -n -r | head -10
Output:
PID COMMAND OOM_SCORE
1234 node 800
5678 postgres 300
9012 nginx 50
My Node.js process scored 800—first in line to be killed.
To lower the OOM score:
# Set OOM Score Adjust for PID 1234 to -500
echo -500 | sudo tee /proc/1234/oom_score_adj
Negative values mean "this process is important, don't kill it."
7. Thrashing: When the System Dies
Thrashing was a nightmare I experienced firsthand.
7.1 What is Thrashing?
A state where page faults happen too frequently. The OS spends all its time swapping in/out, with no time for actual work.
Imagine a librarian so busy carrying books that students can't read.
7.2 When Does It Occur?
When RAM is insufficient but many processes are active.
Example:
- RAM: 2GB
- 10 processes, each needing 300MB → Total 3GB needed
RAM is insufficient, so the OS uses swap. But if all processes are simultaneously active, the OS goes into a frenzy of swap in/out.
7.3 Symptoms
- CPU usage: Low (10-20%)
- Disk I/O: Maxed out (100%)
- System response: Glacial
- HDD LED: Constantly blinking
When my server was in this state, even SSH login took a minute.
7.4 Solutions
Root fix: Add more RAM or reduce processes.
Temporary fixes:
- Kill unnecessary processes
- Lower swappiness value
- Move some services to another server
8. Summary: Lies Have a Cost
| Type | Default | Virtual Memory & Swap |
|---|---|---|
| Analogy | Books on Desk Only | Using Shelf as Fake Desk |
| Pros | Super Fast (All RAM) | Overcome Capacity Limit |
| Cons | Expensive, Crash if full | Slow (Disk Speed) |
| Result | Crash (OOM) | Lag (Thrashing) |
This is how I understood it:
Virtual memory is the OS's illusion. It lies: "We have infinite memory." Swap maintains the illusion by secretly using the disk. The cost is speed.
"Let's just increase swap memory?"
This translates to: "Let's prevent the crash for now and worry about the slowness later."
The real solution is adding more RAM. Swap is insurance. The best insurance is the one you never use.