Sub topic 1
Why Memory Is a Hierarchy
A computer does not use one memory device for every purpose. It combines several levels of storage. Small, fast memories stay close to the CPU. Larger, slower memories hold more data at lower cost. This arrangement is called the memory hierarchy.
Prerequisites
This chapter assumes that you can read binary and hexadecimal numbers, understand powers of two, and know that a CPU fetches instructions and data from memory. You should also know that a byte-addressable system assigns one address to each byte.
The speed, capacity, and cost trade-off
No practical memory technology is simultaneously very fast, very large, and inexpensive. SRAM, used for CPU caches, can respond quickly but requires more chip area and costs more per bit. DRAM provides much more capacity, but it is slower and must be periodically refreshed. Secondary storage, such as an SSD, is much larger and cheaper per bit, but its access time is far greater than that of semiconductor memory.
| Level | Typical role | Relative speed | Capacity | Cost per bit |
|---|---|---|---|---|
| Registers | Current operands and control values | Highest | Smallest | Highest |
| Cache | Recently used CPU data and instructions | Very high | Small | High |
| Main memory | Active programs and their data | Medium | Medium to large | Medium |
| Secondary storage | Persistent files and inactive programs | Lowest | Largest | Lowest |
The CPU usually checks the hierarchy from the top down. If the required value is in a register, access is immediate. Otherwise, the CPU checks the cache, then main memory, and eventually secondary storage. Moving data from a lower level to a higher level takes time, but later accesses may become faster.
A cache is a small memory that keeps copies of data from a larger, slower memory. The CPU does not normally request an arbitrary full memory region from a cache. It requests a block, also called a cache line when it is stored in the cache. A block contains several adjacent bytes.
Why the hierarchy works
Programs usually exhibit locality, which means that their memory accesses are not random.
- Temporal locality means that a recently used address is likely to be used again soon. A loop repeatedly reading the same instruction sequence is an example.
- Spatial locality means that addresses near a recently used address are likely to be used soon. Reading consecutive elements of an array is an example.
Caches exploit both forms. They retain recently accessed blocks for temporal locality and fetch neighboring bytes in the same block for spatial locality. The cache therefore avoids paying the slower access cost for every CPU reference.
A reference moving through the hierarchy
The following model shows the direction of a typical request. A cache hit ends the search early. A miss causes the next level to be checked.
A hit is a lookup that finds the requested data at the level being checked. A miss is a lookup that does not find it. The miss penalty is the extra time needed to obtain the data from a lower level and possibly copy it upward.
Why multiple cache levels are useful
Modern processors often use L1, L2, and L3 caches. L1 is the smallest and fastest. L2 is larger but slower. L3 is larger again and may be shared by multiple CPU cores. This arrangement provides a compromise: most requests can be served quickly, while less frequently used data still has room in larger caches.
The hierarchy also separates volatile and persistent storage. Registers, caches, and main memory normally lose their contents when power is removed. Secondary storage retains files and programs, allowing them to be loaded again later.
Exercise
Why can a computer not simply use a very large cache as its only memory?
Show answer
A very large cache would require expensive, space-consuming fast memory. The hierarchy uses a small fast cache for frequent data and larger, cheaper memory for the rest.