2.3 Memory Split into Layers

Memory occupies a large place in what a computer does. In the previous chapter, we divided chips into chips that compute (system semiconductors) and chips that remember (memory semiconductors), and in the previous section we met them as the CPU and memory. But memory doesn’t exist only in “memory.” As we saw in the previous section, there is memory space inside the CPU as well. Why isn’t all memory gathered in one place?

Recall 3 + 5 from the previous section. In Figure 2-5, the CPU couldn’t add 3 and 5 while they sat in memory; it could add them only after a LOAD instruction brought them into the memory space right next to where computation happens. But that memory space isn’t very large. So only some of the data needed for the computation at hand was kept close to where computation happens, and the rest was kept far from it, in memory.

Come to think of it, this resembles the way people remember. People don’t retrieve everything they remember in the same way, either. Some things are in your head right now, ready to use; some you saw a moment ago and can recall quickly. Some are old memories you have to work to recall, and some aren’t in your head at all but written in a notebook you have to look up. Computers are the same. There is one more layer of memory space between the space right next to computation and memory, and one more layer behind memory.

In this way, a computer’s memory is divided broadly into four layers: registers, cache, memory, and storage. Registers and cache are inside the CPU; memory and storage are separate components outside it. Memory is the DRAM we saw in the previous section (Figure 2-2), and the typical storage device is the SSD (Solid State Drive). Figure 2-6 shows a photo of an actual SSD.

Photo of an SSD
Figure 2-6. An SSD, a storage device
Pyramid of the memory hierarchy: registers, cache, memory, and storage
Figure 2-7. The memory hierarchy — comparing the speed and size of each layer

Figure 2-7 arranges the four layers in order of closeness to where computation happens and shows their speed and size together. Let’s go through them one by one, starting from the layer closest to computation.

Registers briefly hold values about to be used in computation, or the results of a computation. They are the fastest but also the smallest.

Cache is where copies of some of the instructions and data in memory are kept close at hand. If the same instruction or data is needed again for a computation, the copy stored in the cache is used instead of going all the way to memory.

Memory holds the instructions and data of the programs currently running. It’s where 3 and 5 — and the instructions to fetch and add them — sat in the previous section.

Storage holds everything, including things not in immediate use, such as installed programs and the documents and photos we’ve created. The first three layers are volatile: their contents vanish when the power is cut. Storage keeps its contents even when the computer is turned off and on again.

These memory spaces share a pattern: the farther a layer is from where computation happens, the slower it gets, but the larger its capacity. Conversely, the closer it is, the faster it is and the smaller its space. It varies by device, but comparing the two extremes, a register delivers a value in less than about a billionth of a second, while an SSD can take tens of thousands of times longer or more. And the faster a memory space is, the more it costs to hold the same amount of data — the cost per bit shown in Figure 2-7.

Why, then, are fast memory spaces small and expensive? And why do registers and cache differ in speed and size, even though both are inside the CPU? To answer these questions, we need to look at how each memory space is built inside the chip. We’ll find the answer in Section 4.8.

A computer uses these memory spaces, each with different characteristics, together. Because fast space is expensive, it is kept only as small as necessary, while places that must hold a lot of content trade a little speed for large capacity at low cost.

Because memory is layered this way, data is generally found in the nearest lower layer that holds it and pulled up, one layer at a time, toward where computation happens. When a program runs, its instructions and data move up from storage into memory, some of them into cache, and the values needed for immediate computation into registers. When a value is needed, it is looked for first in the cache; if it isn’t there, in memory; and if it still isn’t found, in storage — and it is then brought into a register, close to where computation happens. Bringing data up doesn’t erase it from the layer it came from; the same data can sit in several layers at once. A computation’s result may stay in a register for use in the next computation, or, if needed, be sent back down and written to memory. To keep that result after the computer is turned off, it must be written to storage. That’s what happens when we press “Save.”

For now, it’s enough to know that memory is divided into several layers, and that the closer a layer is to where computation happens, the faster but smaller it is; the farther away, the slower but larger. Also, while this section used “memory” as the name of one layer, the one between cache and storage, from here on we’ll use the word “memory” as the general term for memory spaces, and call each layer by name only when its particular characteristics matter.

Leave a question or comment

Questions, corrections, or thoughts about this section are welcome. Your first comment will appear after review. (Privacy Policy)