
Virtual Memory and Swap: The Trick When Your Desk Is Too Small
How do you run a 10GB game with only 8GB of RAM? The OS secretly uses part of your hard drive.

How do you run a 10GB game with only 8GB of RAM? The OS secretly uses part of your hard drive.
Why does MacBook battery last long? Should I use AWS Graviton? A deep dive into the philosophy of CISC (Complex) vs RISC (Simple) architectures.

The gold mine of AI era, NVIDIA GPUs. Why do we run AI on gaming graphics cards? Learn the difference between workers (CUDA) and matrix geniuses (Tensor Cores).

Bought a fast SSD but it's still slow? One-lane country road (SATA) vs 16-lane highway (NVMe).

LP records vs USB drives. Why physically spinning disks are inherently slow, and how SSDs made servers 100x faster.

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.
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.
Here's what tripped me up initially.
I thought these were the same thing. Wrong. Virtual memory is a concept, swap is one implementation method.
I completely misunderstood this. Swap doesn't increase memory—it prevents death from memory shortage by sacrificing speed.
RAM vs SSD: 1,000x difference. RAM vs HDD: 1,000,000x difference.
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.
Then I heard this metaphor and everything clicked.
Imagine you're studying at a library.
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:
This is Swapping.
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.
You're a technician repairing items in a 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.
Let me go deeper now.
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).
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.
When a page fault occurs, the OS:
This process is extremely slow because disk I/O is thousands of times slower than RAM access.
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.
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:
Linux primarily uses LRU variants.
Let me organize my understanding of swap space.
There are two ways to create swap space.
Dedicates part of the disk exclusively for swap. Even formatted with a special swap format.
Pros:
Cons:
Creates a large file within a regular filesystem to use as swap.
Pros:
Cons:
Modern systems prefer Swap Files, especially in cloud environments.
Old rule: "2x RAM" Modern rule: "It depends"
My accepted principle:
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
Swappiness determines "when to start using swap." Range: 0-100.
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
# 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."
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.
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."
Thrashing was a nightmare I experienced firsthand.
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.
When RAM is insufficient but many processes are active.
Example:
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.
When my server was in this state, even SSH login took a minute.
Root fix: Add more RAM or reduce processes.
Temporary fixes:
| 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.