Understand virtual memory and its role

Data Storage – Virtual Memory

1. What Is Virtual Memory?

Virtual memory is a technique that lets the operating system (OS) make a programme think it has a large, continuous block of memory, even though the computer only has a limited amount of fast RAM. The OS does this by using part of the secondary storage (hard‑disk or SSD) as an extension of RAM.

2. Why Is Virtual Memory Needed?

  • Primary vs. secondary storage – RAM is fast but small; the hard‑disk/SSD is much larger but slower.
  • Virtual memory combines a portion of the secondary storage (the swap space or paging file) with RAM, giving each programme the impression of a large, private memory area.
  • It allows:
    • Running programmes that are bigger than the physical RAM.
    • Multitasking – each process gets its own virtual address space.
    • Protection – a faulty programme cannot overwrite another programme’s memory.
    • Simpler programming – the programmer does not need to know the exact amount of RAM available.

3. How Virtual Memory Works

The OS manages virtual memory by dividing both the virtual address space and the physical RAM into equal‑size blocks.

  • Page – a fixed‑size block of virtual memory (e.g., 4 KB).
  • Frame – a fixed‑size block of physical RAM that holds one page.
  • Page table – a data structure that records, for every virtual page, whether it is in a RAM frame or in the swap space, and if in RAM, which frame it occupies.

Page‑fault handling – five steps

Key points – the OS response to a page fault
  1. Detect the fault – the CPU tries to use a virtual address that is not currently in RAM.
  2. Find a free frame – if no frame is free, the OS selects a “victim” frame using a simple replacement rule (e.g., “least‑recently‑used” or “first‑in‑first‑out”).
  3. Write‑back if dirty – if the victim frame contains a modified page, that page is written to the swap space (paging out).
  4. Read the required page – the needed page is fetched from the swap space into the now‑free frame (paging in).
  5. Update the page table and resume the programme.

Page‑replacement policies (brief)

The OS must decide which resident page to replace when a free frame is needed. The IGCSE only requires a simple description, for example:

  • Least‑recently‑used (LRU) – replace the page that has not been used for the longest time.
  • First‑in‑first‑out (FIFO) – replace the page that has been in RAM the longest.

4. Key Terminology

Term Definition (IGCSE wording)
Page Fixed‑size block of virtual memory (e.g., 4 KB).
Frame Fixed‑size block of physical RAM that holds one page.
Page table Structure used by the OS to map each virtual page to a RAM frame or to the swap space.
Swap space / paging file Reserved area on secondary storage where pages that do not fit in RAM are stored.
Page fault Occurs when a programme tries to access a page that is not currently in RAM.
Page‑replacement algorithm Method the OS uses to decide which resident page to replace (e.g., LRU, FIFO).

5. Example Calculation (using the IGCSE binary convention)

Assume a computer with 8 GB of RAM and a page size of 4 KB. The IGCSE uses the 1024‑based units, so:

  • 1 GB = 1 × 1024 × 1024 KB = 1 048 576 KB
  • 8 GB = 8 × 1 048 576 KB = 8 388 608 KB
  • Number of frames in RAM = 8 GB ÷ 4 KB = 8 388 608 KB ÷ 4 KB = 2 097 152 frames

If a programme needs 2 GB of virtual memory:

  • 2 GB = 2 × 1 048 576 KB = 2 097 152 KB
  • Pages required = 2 GB ÷ 4 KB = 2 097 152 KB ÷ 4 KB = 1 048 576 pages

Only a portion of these pages can be resident in RAM at any one time; the rest are stored in the swap space.

6. Advantages and Disadvantages of Virtual Memory

Advantage Disadvantage
Allows execution of programmes larger than physical RAM. Disk access is much slower than RAM, so heavy paging reduces performance.
Supports multitasking – each process has its own address space. Too much paging can cause “thrashing”, making the system appear to freeze.
Provides process isolation, improving security and stability. Requires dedicated disk space for swap; if the disk fills, virtual memory cannot function.
Simplifies programming – the OS handles memory allocation. Extra I/O activity increases power use and can wear out SSDs.

7. Comparison of Memory Types (IGCSE style)

Memory Type Typical Speed Volatility Typical Use
RAM (primary memory) ≈10 GB s⁻¹ (DDR4/DDR5) Volatile – data lost when power is removed Running programmes and active data
Virtual memory (RAM + swap space) RAM speed for resident pages; disk speed (≈0.1–5 GB s⁻¹) for swapped pages Volatile – swap area cleared on shutdown Extends usable memory beyond physical RAM
Secondary storage (HDD/SSD) ≈0.1 GB s⁻¹ (HDD) – 5 GB s⁻¹ (SSD) Non‑volatile – retains data without power Long‑term data storage; holds the swap space/paging file

8. Summary

Virtual memory bridges the gap between limited, fast RAM and abundant, slower secondary storage. By dividing memory into pages and using a page table, the OS can give each programme the illusion of a large, private memory area. This enables larger programmes, multitasking, and process isolation, but it also introduces latency and the risk of thrashing if the system relies too heavily on swapping.

9. Quick Quiz (AO3 – evaluate and apply)

  1. Define a page fault and list the five steps the OS takes to handle it.
  2. Explain the difference between a page and a frame, using a 4 KB page size as an example.
  3. Why can excessive paging lead to “thrashing”? What impact does thrashing have on system performance?
  4. Calculate the number of pages needed for a 16 GB virtual address space when the page size is 8 KB. Show your working and state the conversion rule you used.
Suggested diagram: Flowchart of the paging process – CPU request → page‑table lookup → hit / miss → (if miss) paging out → paging in → update table → resume execution.

Create an account or Login to take a Quiz

45 views
0 improvement suggestions

Log in to suggest improvements to this note.