How RAM Works: A Practical Guide for Developers
Registers, cache, RAM and disk: measured latency for each level, SRAM vs DRAM, virtual memory, and what happens when RAM runs out and the system starts swapping.
Your machine slowed to a crawl after you opened the tenth terminal, a few Chrome tabs and VS Code with a large project. You blamed the CPU, then the SSD, and the bottleneck was RAM: almost full, so the OS started pushing memory pages out to disk. Every access that used to take nanoseconds now took tens or hundreds of microseconds, and the whole system felt it.
To understand why that hurts so much, knowing what RAM is isn't enough. You need to see where it sits in the memory hierarchy, with real numbers. This post measures each level (cache, RAM, SSD) on a real machine, explains the difference between SRAM and DRAM, and uses free, vmstat and a container that blows its memory limit on purpose to show what happens when RAM runs out. How much RAM to buy is a separate topic: how to choose a laptop for programming.
What RAM is, in two sentences
RAM (Random Access Memory) is the computer's working memory: any address can be read or written directly, without walking through the ones before it, and its contents vanish when power goes away. That volatility isn't a flaw; it's the price of speed. The CPU doesn't execute anything straight from disk; everything that runs goes through RAM, which is why how much you have and how it's used decide how much you can do at once.
The analogy that works is a workshop: the disk is the cabinet with every part, RAM is the workbench. The bigger the bench, the fewer trips to the cabinet.
The memory hierarchy: from registers to disk
Between the CPU and your data there are several levels, and one rule organizes all of them: the closer to the CPU, the smaller, faster and more expensive per byte.
- Registers: inside the core, a few dozen values, accessed within the clock cycle itself.
- L1, L2 and L3 cache: SRAM on the processor die. L1 and L2 are per core; L3 is shared.
- RAM: DRAM modules outside the processor, connected through a memory bus.
- Storage (SSD, HDD): persistent, orders of magnitude larger and slower.
Latency numbers are usually quoted from the table Peter Norvig published in Teach Yourself Programming in Ten Years (2001, later popularized by Jeff Dean): L1 at 0.5 ns, L2 at 7 ns, RAM at 100 ns, a disk seek at 8 ms. Those numbers are two decades old and predate SSDs. So I measured my own.
The machine: a laptop with an Intel Core Ultra 7 255HX (48 KB of L1d and 3 MB of L2 per core, 30 MB of L3, according to lscpu), 32 GB of DDR5-6400 and a 512 GB NVMe SSD, measured inside a Docker container on WSL2 in November 2026. For cache and RAM I used pointer chasing: a randomly shuffled linked list, so each load depends on the previous one and the prefetcher can't guess the next address. The time per hop, as a function of buffer size, exposes each level of the hierarchy:
/* each element points to another, in random order, one per cache line (64 bytes) */
for (size_t i = 0; i < iters; i++)
p = (void **)*p; /* the next load depends on this one: pure latency */
For the SSD I used fio with 4 KiB random reads, one request at a time (--rw=randread --bs=4k --iodepth=1 --direct=1). There's no HDD in this machine, so that row is calculated: Norvig's 8 ms seek plus the average rotational latency of a 7,200 rpm drive (half a revolution: 60 / 7,200 / 2 = 4.17 ms).
| Level | Size on this machine | Latency | Norvig (2001) | If 1 ns were 1 second |
|---|---|---|---|---|
| Register | a few dozen values | ~0.2 ns (1 cycle at ~5 GHz, not measured) | not listed | 0.2 s |
| L1 cache | 48 KB per core | 0.8 ns | 0.5 ns | 0.8 s |
| L2 cache | 3 MB per core | ~5 ns | 7 ns | 5 s |
| L3 cache | 30 MB | ~15 ns | not listed | 15 s |
| RAM (DDR5) | 32 GB | ~150 ns | 100 ns | 2.5 minutes |
| NVMe SSD (4 KiB random read) | 512 GB | ~122 µs (mean) | not listed | 1.4 days |
| 7,200 rpm HDD | not measured | ~12 ms (calculated) | 8 ms (seek only) | 141 days |
Two honest caveats. The measured RAM latency (~150 ns with a 256 MB buffer) includes TLB misses and the WSL2 virtualization layer, which is why it sits above the classic 100 ns. And the NVMe reads go through the WSL2 virtual disk: the mean was 122 µs, with a minimum of 65 µs and half the reads under 100 µs. Absolute values vary from machine to machine; orders of magnitude don't.
And orders of magnitude are what matter. In my measurement, RAM is about 800 times faster than the NVMe SSD and about 80,000 times faster than an HDD; L1 is about 190 times faster than RAM. That's why swapping is so noticeable: when a page has to come back from disk, an access that would cost 2.5 minutes on the human scale of the last column now costs a day and a half. For why NVMe is so much faster than SATA and HDDs, see SATA vs NVMe SSD.
SRAM vs DRAM: why the cache is so small
Cache and RAM are different technologies, and that explains their sizes.
Cache is SRAM (static RAM): each bit is held in a circuit of several transistors (the classic cell has six) that keeps its value as long as there's power, with no upkeep. It's fast, but it takes a lot of silicon area per bit. That's why a laptop CPU's L3 is tens of megabytes, not gigabytes.
PC RAM is DRAM (dynamic RAM): each bit is the charge in a tiny capacitor gated by one transistor. Far more fits per chip and it costs far less per byte, but the capacitor leaks. Every row has to be read and rewritten periodically (refresh) within a window of tens of milliseconds: 64 ms for DDR4 and 32 ms for DDR5 at normal temperatures, per the spec as summarized on Wikipedia. Add the trip off the chip and DRAM ends up an order of magnitude slower than cache.
DDR (Double Data Rate) means the module transfers data on both edges of the clock. DDR4 and DDR5 aren't interchangeable: the notch on the module sits in a different position precisely to stop the wrong one from fitting, and DDR5 runs at a lower voltage (1.1 V versus DDR4's 1.2 V). Laptops add one more detail: many models have soldered RAM, and then there's no upgrade at all.
Another point buying guides repeat for good reason is dual channel: two identical modules in the right slots let the memory controller access two channels in parallel, doubling theoretical bandwidth. Latency doesn't change; data per second does.
Virtual memory, paging and swap
The OS doesn't hand physical RAM to programs directly. Each process sees its own virtual address space, and the OS keeps a per-process page table that translates virtual addresses to physical ones, in 4 KB blocks on most setups. That isolates processes (a bug in one program can't overwrite another's memory) and gives the OS freedom to decide where each page lives.
When a process touches a page that isn't in RAM, a page fault happens: the OS fetches the page (from the executable on disk, from a mapped file or from swap), puts it in RAM and resumes execution. When RAM gets tight, the OS goes the other way: it picks rarely used pages and evicts them, writing to swap the ones that have no copy elsewhere.
The OS also uses spare RAM as a disk cache. "Free RAM is wasted RAM" is literal on Linux, which is why the number that matters in free isn't free, it's available:
$ free -h
total used free shared buff/cache available
Mem: 15Gi 1.7Gi 6.9Gi 30Mi 7.0Gi 13Gi
Swap: 4.0Gi 0B 4.0Gi
(The 15 Gi total is the slice of RAM WSL2 gives to the VM Docker runs in.) Only 6.9 GB free, but 13 GB available: the 7 GB of buff/cache are cached files the kernel drops instantly if a process needs the memory. If your monitor says RAM is "90% used" and available is high, there's no problem.
What happens when RAM runs out
The symptom of too little RAM isn't high used: it's active swapping. vmstat shows it in the si and so columns (KB/s swapped in and out). To watch it happen without freezing my machine, I ran a Python script that allocates 100 MB at a time inside a container limited to 512 MB of RAM and 512 MB of swap (--memory=512m --memory-swap=1g), with vmstat 1 alongside:
r b swpd free buff cache si so bi bo in cs us sy id wa st gu
0 0 396 13490188 35080 746456 0 0 0 0 1077 1528 0 1 99 0 0 0
0 0 124512 13450164 35000 699396 472 123980 760 146272 1786 6040 0 1 99 0 0 0
0 0 230036 13450220 35000 699368 16 105524 28 105524 676 3979 0 1 99 0 0 0
0 0 416376 13443968 34992 688032 4 186344 4 186344 1578 1502 0 1 99 0 0 0
0 0 804 13915276 34696 682472 96 108308 796 108568 2379 3403 0 1 99 0 0 0
Once the process went past 512 MB, the kernel started pushing pages to swap at 100 to 180 MB/s (so) and swpd climbed to 416 MB. Shortly after 1,000 MB were allocated, the container's RAM and swap were both exhausted and the process died with exit code 137 (SIGKILL). The cgroup's memory.events recorded oom_kill 1 and Docker flagged OOMKilled=true. On the last line swap drops back to almost zero: the dead process's memory was freed.
That's the Linux OOM killer, and it only steps in when RAM and swap are both exhausted, not when "the disk runs out". Swap is a fixed-size partition or file; free space on the rest of the disk doesn't count. The classic description of the kernel, in Mel Gorman's chapter on OOM (written for 2.4/2.6), starts with exactly that check: if there's swap left, the system isn't out of memory. The victim isn't "background processes first": the /proc documentation explains that each process gets a score from 0 to 1000 proportional to the share of its allowed memory it uses, adjustable through /proc/<pid>/oom_score_adj, and usually the biggest consumer dies. In a container or service with a memory limit, the math is done against the cgroup limit, as in the test above.
Windows works differently. The commit limit is RAM plus the paging file (pagefile.sys), and a system-managed paging file grows on its own when usage hits 90% of that limit, up to three times RAM or 4 GB (whichever is larger), if there's disk space, per Microsoft's documentation. If the limit is still reached, the low-memory warning shows up and programs may freeze or close.
If you work with files that don't fit in memory, the problem is code, not hardware: processing large files without running out of memory shows how to stream instead of loading everything.
When more RAM helps (and when it doesn't)
More RAM fixes things when vmstat shows non-zero si/so during your normal work, or when available sits near zero: a browser with dozens of tabs, Docker containers, emulators, VMs, builds of large projects. More RAM doesn't help when the bottleneck is CPU (see cores and threads), disk I/O or network. If RAM is already spare, more gigabytes speed up nothing.
For programming, the short recommendation is 16 GB as a floor and 32 GB if you live in containers or VMs; the full reasoning, soldered-RAM laptops included, is in how to choose a laptop for programming. For development, the amount of RAM usually matters more than its speed (MT/s), and it's worth checking that your modules run in dual channel.
Frequently asked questions
What's the difference between RAM and ROM?
ROM (Read-Only Memory) is non-volatile: it keeps data without power and, in its classic form, isn't rewritten during normal operation. It's where firmware lived, like the PC BIOS. Today firmware sits in rewritable flash memory, but the role is the same: hold what must survive a shutdown. RAM is the CPU's temporary working area.
Why is cache so much smaller than RAM?
Because it's SRAM, which uses several transistors per bit and takes far more silicon area than DRAM, where each bit is one transistor and one capacitor. Cache is fast precisely because it's small and sits inside the processor; making it much bigger would make the chip more expensive and raise its own latency.
What happens when RAM runs out?
The system starts using swap (Linux) or the paging file (Windows) and everything slows down, because disk is hundreds to tens of thousands of times slower than RAM. On Linux, if RAM and swap are both exhausted, the OOM killer terminates the process with the highest score, usually the largest memory consumer. On Windows, the managed paging file grows up to a limit and, if that's not enough, the system warns that memory is low.
How do I tell whether my PC is short on RAM?
On Linux, run vmstat 1 during normal use: si and so consistently above zero mean active swapping, and free -h with available near zero confirms it. On Windows, the Performance tab in Task Manager shows "Committed" memory against the limit. "Full" RAM with high available is just disk cache and isn't a problem.
RAM is only slow when there isn't enough of it
The numbers tell the whole story: L1 under a nanosecond, RAM at ~150 ns, SSD at ~120 µs, HDD at ~12 ms. Between cache levels the steps are a few times; from RAM to SSD the jump is almost a thousandfold. Swapping is the moment your program falls two or three steps at once. Before buying RAM, look at si/so in vmstat and available in free; before blaming the hardware, check whether the code is loading into memory what it should be streaming. And when the OOM killer shows up in the log, remember it isn't the problem: it's the notice that RAM and swap ran out.
- 01 CPU Cores vs. Threads: What Actually Changes in Practice Understand the difference between CPU cores and threads and what actually changes for performance, multitasking, gaming, and development.
- 02 MIT, Apache, and GPL Licenses: Practical Differences for Developers MIT, Apache 2.0, and GPL are not the same thing. Understand permissive vs copyleft, patent protection, and compatibility before shipping code to production.