Memory Hierarchy

COS2621 - Computer Organisation · Computer Memory

Memory Hierarchy

Memory hierarchy is a crucial concept in computer organisation. It refers to the arrangement of different types of memory in a computer system, organised by speed, cost, and size. The goal of this hierarchy is to provide a balance between performance and cost-effectiveness.

The Levels of Memory Hierarchy

The memory hierarchy consists of several levels, each with distinct characteristics. These levels include:

  • Registers
  • Cache Memory
  • Main Memory (RAM)
  • Secondary Storage

Registers

Registers are the fastest type of memory. They are small storage locations within the CPU (Central Processing Unit) that hold data temporarily during processing. Registers are used to store operands for arithmetic operations and to keep track of the program counter, which indicates the next instruction to execute.

For example, consider a simple addition operation:

int a = 5;  // a is stored in a register
int b = 10; // b is stored in a register
int sum = a + b; // sum is stored in a register

In this example, the values of a and b are stored in registers while the CPU performs the addition.

Cache Memory

Cache memory is a smaller, faster type of volatile memory located inside or very close to the CPU. It stores frequently accessed data and instructions to speed up processing times. Cache memory is divided into levels, typically L1, L2, and L3.

Remember: L1 cache is the fastest and closest to the CPU, while L3 cache is larger but slower.

For example, when a program requests data, the CPU first checks the L1 cache. If the data is not found there, it checks the L2 cache, and then the L3 cache, before finally accessing the main memory. This process is known as cache lookup.

Main Memory (RAM)

Main memory, or RAM (Random Access Memory), is the primary storage used by the computer to hold data and instructions that are currently in use. RAM is faster than secondary storage but slower than cache memory. It is volatile, meaning it loses its content when the power is turned off.

For example, when you open a document on your computer, the operating system loads it from the secondary storage into RAM. This allows the CPU to access the document quickly while you work on it.

Secondary Storage

Secondary storage includes devices such as hard drives, solid-state drives, and USB flash drives. These devices are used for long-term data storage. They are slower than RAM and cache memory but are non-volatile, meaning they retain data even when the power is off.

For instance, when you save a document, it is written to the secondary storage, ensuring that it is available for future access.

Memory Access Speed

The speed of memory access varies across the hierarchy. Registers are the fastest, followed by cache memory, RAM, and finally secondary storage. This difference in speed is a critical factor in the design of computer systems. The aim is to keep the CPU fed with data as quickly as possible.

Watch out: Do not confuse volatile memory (like RAM) with non-volatile memory (like hard drives). Volatile memory loses data when power is off, while non-volatile memory retains data.

Memory Size and Cost

Another aspect of the memory hierarchy is the size and cost of each memory type. Registers are very small and expensive, cache memory is larger but still costly, RAM is larger and less expensive than cache, and secondary storage is the largest and cheapest per gigabyte.

This relationship is often summarised as follows:

  • Registers: Smallest and most expensive
  • Cache Memory: Small and expensive
  • Main Memory (RAM): Larger and less expensive
  • Secondary Storage: Largest and cheapest

Memory Hierarchy in Action

To illustrate how the memory hierarchy functions in a practical scenario, consider a computer running a software application. When the application starts, it is loaded into RAM from secondary storage. As the application runs, frequently used data is stored in cache memory for quick access. If the CPU needs data that is not in the cache, it retrieves it from RAM. If the data is not in RAM, it will be fetched from secondary storage, which is slower.

Optimising Memory Usage

Understanding the memory hierarchy allows developers and system architects to optimise memory usage. For example, programmers can design applications to make efficient use of cache memory by accessing data in a predictable pattern. This can significantly improve performance.

Tip: Use algorithms that access data sequentially to take advantage of cache memory. This approach can improve the speed of data retrieval.

Conclusion

The memory hierarchy is a fundamental concept in computer organisation. It allows for efficient data storage and retrieval by balancing speed, cost, and size. Understanding how different types of memory interact helps in designing better computer systems.

Summary

  • The memory hierarchy consists of registers, cache memory, main memory (RAM), and secondary storage.
  • Registers are the fastest type of memory, followed by cache memory, RAM, and secondary storage.
  • Cache memory stores frequently accessed data for quick retrieval.
  • Main memory (RAM) is volatile and used for active data and instructions.
  • Secondary storage is non-volatile and used for long-term data storage.

Check your understanding

  1. What are the four levels of memory in the memory hierarchy?
  2. Explain the difference between volatile and non-volatile memory.
  3. How does cache memory improve the performance of a computer?
  4. Why is secondary storage considered the cheapest form of memory?