Every computer needs memory to store data and instructions that the processor uses to perform tasks. But not all memory is the same. Some memory is very fast but expensive and small, while other memory is slower but cheaper and much larger. To balance these differences, computers use a memory hierarchy, which organizes memory into different levels based on speed, cost, and size.
Think of it like your kitchen storage. You keep the items you use every day, like spices or utensils, within easy reach (fast access but limited space). Less frequently used items, like large pots or canned goods, are stored in cupboards or the pantry (slower access but more space). Similarly, a computer stores data in different types of memory to optimize performance and cost.
This hierarchy is crucial because the processor runs at very high speeds and needs data quickly. If it had to wait for slow memory every time, the whole system would be sluggish. By using faster memory close to the processor and slower memory further away, computers achieve a good balance between speed, capacity, and cost.
The memory hierarchy consists of several levels, each with distinct characteristics. From the fastest and smallest to the slowest and largest, these levels are:
As you move down the pyramid:
Cache memory is a special type of fast memory that stores copies of data from frequently used main memory locations. It acts as a buffer between the CPU and main memory, reducing the time needed to access data.
Cache is organized into three levels:
This multi-level cache system helps improve performance by increasing the chance that the CPU finds the data it needs quickly.
graph TD CPU[CPU] -->|Request Data| L1[L1 Cache] L1 -->|Hit| CPU L1 -->|Miss| L2[L2 Cache] L2 -->|Hit| CPU L2 -->|Miss| L3[L3 Cache] L3 -->|Hit| CPU L3 -->|Miss| RAM[Main Memory (RAM)] RAM --> CPU
In this flow:
To understand how memory hierarchy affects performance, we use key metrics:
| Memory Type | Access Time | Cost per MB (INR) | Typical Size |
|---|---|---|---|
| Registers | ~1 ns | Very High | Few KB |
| Cache Memory | 1 - 10 ns | High | KB to a few MB |
| Main Memory (RAM) | 50 - 100 ns | Moderate | GBs |
| Secondary Storage (SSD, HDD) | 1 ms - 10 ms | Low | TBs |
The overall speed of memory access depends on how often data is found in the cache (hit ratio) and how long it takes when it is not (miss penalty). The formula to calculate the Average Memory Access Time (AMAT) is:
This formula helps us understand the impact of cache performance on overall memory speed.
Step 1: Identify given values:
Step 2: Use the AMAT formula:
\[ AMAT = (Hit\ Ratio \times Cache\ Access\ Time) + (Miss\ Ratio \times Main\ Memory\ Access\ Time) \]
\[ AMAT = (0.90 \times 5) + (0.10 \times 100) \]
\[ AMAT = 4.5 + 10 = 14.5\ ns \]
Answer: The average memory access time is 14.5 nanoseconds.
Step 1: Calculate speed per rupee for each memory type:
Speed is inversely proportional to access time, so speed per rupee = (1 / Access Time) / Cost
For Cache:
\[ \frac{1}{5\ ns} \div 5000 = \frac{0.2}{5000} = 0.00004 \]
For RAM:
\[ \frac{1}{100\ ns} \div 500 = \frac{0.01}{500} = 0.00002 \]
Step 2: Compare values:
Cache speed per rupee = 0.00004
RAM speed per rupee = 0.00002
Answer: Cache provides better speed per rupee, but it is more expensive overall and smaller in size.
Step 1: Calculate number of hits and misses:
Hits = 80% of 1000 = 800
Misses = 20% of 1000 = 200
Step 2: Calculate total time:
Time for hits = 800 x 2 ns = 1600 ns
Time for misses = 200 x 50 ns = 10,000 ns
Total time = 1600 + 10,000 = 11,600 ns
Answer: Total access time for 1000 requests is 11,600 nanoseconds.
Step 1: Calculate total time with 95% hit ratio:
Hits = 0.95 x 1,000,000 = 950,000
Misses = 50,000
Time for hits = 950,000 x 4 ns = 3,800,000 ns
Time for misses = 50,000 x 100 ns = 5,000,000 ns
Total time = 3,800,000 + 5,000,000 = 8,800,000 ns
Step 2: Calculate total time with 98% hit ratio:
Hits = 980,000
Misses = 20,000
Time for hits = 980,000 x 4 ns = 3,920,000 ns
Time for misses = 20,000 x 100 ns = 2,000,000 ns
Total time = 3,920,000 + 2,000,000 = 5,920,000 ns
Step 3: Calculate time saved:
Time saved = 8,800,000 - 5,920,000 = 2,880,000 ns
Answer: Improving hit ratio from 95% to 98% saves 2.88 milliseconds in memory access time for 1 million accesses.
Step 1: Given main memory size = 2 GB
Step 2: Calculate cache size = 1% of main memory
Cache size = 1% x 2 GB = 0.01 x 2 GB = 0.02 GB = 20 MB
Step 3: Calculate register size = 0.1% of cache size
Register size = 0.1% x 20 MB = 0.001 x 20 MB = 0.02 MB = 20 KB
Answer:
When to use: To quickly recall the relationship between speed, size, and cost in memory hierarchy
When to use: When solving problems related to average memory access time
When to use: While calculating sizes and speeds to avoid unit conversion errors
When to use: To understand why L1 cache is faster but smaller than L3 cache
When to use: To avoid mistakes in average memory access time calculations
Progress tracking is paywalled — subscribe to mark subtopics as understood and save your streak.
Go to practice →