How RAM and disk differ
RAM is the temporary workspace a computer uses while programs run, and disk is where files stay. The idea to keep: a program works on a copy of its data in RAM, and the computer moves data between the two.
The short version
The Linux kernel documentation calls physical memory volatile: what is in RAM does not survive without power, so most of it was read in from files. When a file is read, the kernel keeps its data in memory, in the page cache, so the next read skips the slow disk. Writes go to that cache first and reach the disk later. Apple's archived guide agrees on the cost: reading a page from disk is much slower than reading it from RAM.
Step by step
- A file sits on disk.
- A program reads it, and its data is copied into RAM, in the page cache.
- Later reads of the same data come from RAM, not from the disk.
- A change is made in RAM first, and the changed page is marked dirty.
- The kernel writes dirty pages to the storage device later, before it reuses the page.
- RAM has a fixed size. When it runs short, the system drops cached pages or writes them out, and reads them back from disk when needed.
Why it matters
A first read can be slower than the ones after it, and a change you just made may not be on disk yet. Think of RAM as the desk you work at and the disk as the cabinet: the desk is quick but small and gets cleared, the cabinet is slower and keeps things.
Sources
- Concepts overview — Linux kernel documentation — volatile physical memory, the page cache, dirty pages written to the storage device later, limited RAM and reclaim
- About the Virtual Memory System — Apple, Memory Usage Performance Guidelines (archive, 2013) — reading a page from disk is much slower than reading it from RAM