Explain the following in brief:

(i) Cache memory

(ii) Virtual memory

Cache Memory and Virtual Memory (Brief Explanation)

(i) Cache Memory

Cache memory is a very fast, small-sized memory located close to the CPU. It stores copies of frequently used data and instructions so that the processor can access them much faster than accessing main memory (RAM). This reduces the average memory access time and keeps the CPU busy, improving overall system performance.

  • Key idea: Uses the principles of locality:
    • Temporal locality: recently used data is likely to be used again soon.
    • Spatial locality: data near recently accessed locations is likely to be used.
  • Levels: L1 (smallest, fastest), L2, and L3 (larger but slower than L1).
  • How it works (simplified):
    1. CPU requests data; cache is checked first.
    2. If found (cache hit), data is returned quickly.
    3. If not found (cache miss), data is fetched from RAM, placed in cache, then given to CPU.
  • Organization: Data is stored in cache lines/blocks; mapping can be direct-mapped, fully associative, or set-associative.
  • Replacement policies: When cache is full, common policies include LRU (Least Recently Used) and FIFO.
  • Write policies: Write-through (update RAM immediately) and write-back (update RAM later).
  • Metrics: Hit rate, miss rate, and miss penalty (extra time on a miss).

Advantages: Very high speed, reduces CPU stalls, boosts throughput.

Limitations: Small capacity, higher cost per bit, coherence challenges in multicore systems.

Example: A loop processing an array benefits from cache because nearby elements are loaded into the same cache line, enabling faster repeated access.

(ii) Virtual Memory

Virtual memory is a memory management technique used by the operating system and hardware (MMU) to give each process a large, continuous logical address space, even if the physical RAM is smaller. It stores inactive pages on secondary storage (disk/SSD) and brings them into RAM on demand.

  • Key components:
    • Pages (fixed-size blocks of virtual memory) and frames (fixed-size blocks in RAM).
    • Page table: maps virtual pages to physical frames.
    • MMU and TLB: perform fast address translation and cache recent mappings.
  • How it works (paging):
    1. Program issues a virtual address.
    2. MMU translates it using the page table (often via TLB).
    3. If the page is in RAM, access continues normally.
    4. If not, a page fault occurs: OS loads the page from disk into a free frame (or evicts another page using a replacement policy like LRU/Clock) and updates the page table.
  • Protection and isolation: Each process has its own virtual address space; access permissions prevent interference.
  • Features: Demand paging, memory overcommitment, shared memory regions (e.g., shared libraries), copy-on-write.

Advantages: Runs larger programs than physical RAM allows, improves multiprogramming, provides process isolation and security.

Limitations: Disk access is far slower than RAM; excessive page faults cause thrashing and severe slowdown.

Example: A process may use 8 GB of virtual memory on a system with 4 GB RAM; only the actively used pages are kept in RAM, while the rest stay on disk until needed.

Quick Comparison

  • Purpose: Cache memory speeds up access to data already in RAM; virtual memory extends usable memory beyond RAM.
  • Location: Cache is small and close to the CPU; virtual memory uses disk/SSD as a backing store.
  • Speed: Cache is extremely fast; virtual memory accesses to disk are very slow compared to RAM.
  • Managed by: Cache is managed by hardware (with minimal OS involvement); virtual memory is managed by the OS with hardware support (MMU/TLB).