Memory Hierarchy
The Hierarchy of Memory in a Computer System
Memory Hierarchy (Quick Summary)
Designing a computer system requires balancing "Speed", "Capacity", and "Cost", which leads to the concept of a pyramid-shaped memory hierarchy.
1. Hierarchy (From Fastest to Slowest)
- Registers: Located directly inside the CPU, the fastest (capacity in Bytes).
- Cache Memory (SRAM): Located in or very close to the CPU to reduce the time the CPU has to wait for data from RAM.
L1 Cache: Fastest, smallest (per Core).L2 Cache: Slower than L1 (per Core).L3 Cache: Largest among Caches (shared across all Cores).
- Main Memory (RAM / DRAM): The main workspace of the system (capacity in GB).
- Secondary Storage (SSD / HDD): Stores permanent data, the slowest, but with the largest capacity and lowest cost (in TB).
2. Principle of Locality
The reason Cache Memory works so well comes from the data access behavior of programs:
- Spatial Locality: If the CPU accesses data at a specific address, there is a very high probability that the CPU will soon access data that is "nearby" (e.g., iterating through an Array).
- Temporal Locality: If the CPU accesses a specific piece of data, there is a very high probability that the CPU will soon request "the same data again" (e.g., a Counter variable in a Loop).