Part I · How it works
3.1 RAM and the memory hierarchy
alsomemory hierarchydramddr4ddr5memory latencymemory bandwidthlatency numbersmemory channelsdual channeldimmdimmsmemory speed
TL;DR
- Every level trades size for speed. Rough orders of magnitude: registers under 1 ns, L1 1 ns, L2 4 ns, L3 15 ns, RAM 100 ns, NVMe SSD tens of µs, network to another continent 100 ms.
- RAM (DRAM) keeps each bit in a tiny capacitor that leaks: it must be refreshed constantly, and it loses everything when powered off.
- Bandwidth comes from channels: two modules in the right slots work in parallel and double it; one module alone halves it.
sudo dmidecode -t memory | grep -E 'Size|Type:|Speed|Locator'
lsmem # memory blocks the kernel sees
On your machine
$ atfq 3.1 L1 data 4 × 48.0 KiB, each shared by 2 CPUs L1 code 4 × 32.0 KiB, each shared by 2 CPUs L2 4 × 1.2 MiB, each shared by 2 CPUs L3 12.0 MiB, shared by all 8 CPUs Line 64 bytes: the block memory moves in RAM 10.5 GiB available of 15.5 GiB (68%) · 1.1 GiB completely free Cache 7.5 GiB in buff/cache, 604.0 MiB of it shared (tmpfs) Swap 256.0 MiB used of 8.0 GiB · swappiness 60
An example: Alice's laptop, one of the test machines. In your terminal, this block shows your own machine, read live.
Pitfalls
- Latency hardly improved in 20 years; bandwidth did. Code that chases pointers waits on latency, code that streams arrays enjoys bandwidth.
- The rated speed (DDR5-6000) often needs a profile (XMP or EXPO) turned on in the firmware; otherwise the RAM runs at a safer default.
- DDR5's "on-die ECC" only protects inside the chip: it isn't ECC memory and reports nothing (3.3).
- "Free" RAM is wasted RAM: Linux fills it with cache (17.1).
Deep dive 2 questions
Why is RAM so much slower than the CPU?
Distance and design. RAM sits centimetres away, on another chip, behind a memory controller; DRAM must open a row of cells, read it into a buffer, then send the bytes over the bus. Caches are SRAM inside the chip: much faster, but each bit takes six transistors instead of one capacitor, so there can't be gigabytes of it.