Memory Management: Virtual Memory, Paging and Thrashing

UNIT 2 • MODULE 3 • LESSON 05

Open a dozen browser tabs, a video editor and a game, and your computer may want more memory than the RAM chips actually hold. Yet it usually keeps working, perhaps a little slower. The trick is virtual memory: the operating system uses part of the disk as an overflow area for RAM, and moves pieces of programs in and out as they are needed. Done well, it is invisible. Done badly, the computer grinds almost to a halt, a state called thrashing.

This lesson explains the memory manager’s jobs, how paging splits programs and memory into fixed-size pieces, how a logical address becomes a physical one, and why too little memory leads to thrashing. You will translate addresses by hand and in C, and run a page-fault simulator.

What you will learn

  • The main jobs of memory management.
  • What virtual memory is and why it is useful.
  • Pages, frames and the page table.
  • How a logical address is translated into a physical address.
  • What a page fault is and how the OS handles it.
  • What thrashing is, what causes it and how to reduce it.

Part 1: What memory management does

Recall from Memory Types and Features that RAM is fast, volatile and limited, while secondary storage is slower but much larger. A process can only run while its instructions and data are in RAM. With many processes running at once (Lesson 02), the OS must:

  • Allocate memory to each process when it starts and when it asks for more, and free it when the process ends.
  • Protect each process’s memory, so one process cannot read or overwrite another’s (or the OS’s) data.
  • Keep track of which parts of memory are in use and by whom.
  • Use the space well, keeping as many processes ready to run as possible, and moving parts of processes between RAM and disk when RAM is short.

Early systems gave each process one continuous block of memory. As processes came and went, free memory broke into small scattered gaps (fragmentation), and a large process might not fit even though the total free space was enough. Paging was designed to solve this.

Part 2: Paging

In paging, each process’s logical memory is divided into fixed-size blocks called pages, and physical RAM is divided into blocks of the same size called frames. Any page can be placed in any free frame. A page table, one per process, records which frame holds each page (or that the page is currently on disk). The page table’s location is kept in the process’s PCB (Lesson 03).

Pages, page table, frames Process pages Page 0 Page 1 Page 2 Page 3 Page table 0 → 5 1 → 2 2 → disk 3 → 0 RAM frames 0: page 3 1: other 2: page 1 3: other 4: free 5: page 0 Disk
Pages do not need to sit in neighbouring frames. Page 2 is not in RAM at all; touching it causes a page fault.

Because every frame is the same size, there are no awkward gaps between processes: any free frame will do. Real page sizes are powers of two, typically 4 KB. In this lesson we use a page size of 100 so you can do the arithmetic in your head.

Translating an address

A program uses logical (virtual) addresses, counting from 0 at the start of the program. The hardware and OS translate each one to a physical address in RAM:

page   = logical DIV page size
offset = logical MOD page size
frame  = pageTable[page]
IF frame is "on disk" THEN
    page fault
ELSE
    physical = frame * page size
               + offset
ENDIF

Example with page size 100 and the table in the diagram: logical address 137 is page 1, offset 37. Page 1 is in frame 2, so the physical address is 2 × 100 + 37 = 237. Logical address 245 is page 2, which is on disk, so the access causes a page fault.

Here is the same calculation in C, with a different six-page table. -1 marks a page that is on disk.

#include <stdio.h>

#define PAGE_SIZE 100
#define PAGES 6

/* frame for each page; -1 = on disk */
int table[PAGES] = {3, -1, 7, 0, -1, 2};

int translate(int logical)
{
    int page = logical / PAGE_SIZE;
    int offset = logical % PAGE_SIZE;
    if (page >= PAGES) {
        printf("%4d: outside process\n",
               logical);
        return -1;
    }
    if (table[page] == -1) {
        printf("%4d: page %d not in RAM"
               " -> page fault\n",
               logical, page);
        return -1;
    }
    int physical =
        table[page] * PAGE_SIZE + offset;
    printf("%4d: page %d offset %2d"
           " -> frame %d = %d\n",
           logical, page, offset,
           table[page], physical);
    return physical;
}

int main(void)
{
    int tests[] = {245, 37, 512, 160, 399,
                   650};
    int i;
    for (i = 0; i < 6; i++)
        translate(tests[i]);
    return 0;
}

Output:

 245: page 2 offset 45 -> frame 7 = 745
  37: page 0 offset 37 -> frame 3 = 337
 512: page 5 offset 12 -> frame 2 = 212
 160: page 1 not in RAM -> page fault
 399: page 3 offset 99 -> frame 0 = 99
 650: outside process

Note the two different failures. Address 160 belongs to the process, but page 1 is on disk: the OS must load it (a page fault, which is normal). Address 650 is beyond the process’s pages: that is an illegal access, and the OS stops the process with an error. This is how paging also gives memory protection: a process can only reach frames listed in its own page table.

Part 3: Virtual memory and page faults

Virtual memory lets a process use more memory than is physically free, by keeping only the pages it is actively using in RAM and the rest on the disk, in a swap area or page file. Programs are written as if a large, continuous memory were available; the OS makes it so.

When a process touches a page that is not in RAM, the hardware raises a page fault (an interrupt, Lesson 03). The OS then:

  1. blocks the process (it must wait for the disk);
  2. finds a free frame, or chooses a victim page to remove if RAM is full (writing it to disk first if it has been changed);
  3. reads the needed page from disk into the frame and updates the page table;
  4. marks the process Ready, so it can repeat the instruction that faulted.

Choosing the victim is a page replacement policy. The simplest is first-in, first-out (FIFO): remove the page that has been in RAM longest. Better policies try to remove the page least likely to be needed soon, such as the least recently used (LRU) page.

Benefit of virtual memoryCost
Programs larger than RAM can runDisk is thousands of times slower than RAM, so each fault is expensive
More processes can be kept active at oncePage tables use memory and translation needs hardware support
Each process gets its own protected address spaceToo many faults lead to thrashing

Part 4: Thrashing

Thrashing happens when the processes running need more pages than there are frames, so pages are constantly swapped out and then needed again almost immediately. The system spends most of its time moving pages between RAM and disk instead of doing useful work. The disk light stays on, the CPU is mostly idle waiting for page faults, and everything feels frozen.

Run the simulator. The Mixed string is a typical program; the Loop string repeatedly uses four pages. Watch what happens to the Loop string with 3 frames, then with 4.

Try it 1Page fault simulator (FIFO replacement)

Each number is a page the process needs, in order. F marks a page fault; ✓ marks a hit.

Loop string: what is the smallest number of frames that stops the thrashing?

With 3 frames, the Loop string faults on all 12 references: each page is thrown out just before it is needed again. One more frame and only the first 4 references fault. The set of pages a process is actively using is called its working set. If the frames given to a process are fewer than its working set, it thrashes.

Causes and cures

CauseCure
Too many processes running for the amount of RAMReduce the degree of multiprogramming: suspend or close some processes
A process is given fewer frames than its working setGive it more frames, using its fault rate as a guide
Not enough physical memory for normal workloadsAdd RAM
Poor replacement policyUse a policy such as LRU that keeps recently used pages

There is an interesting trap here. When the CPU is idle because processes are waiting for page faults, a naive OS might think "the CPU is not busy, let me start more processes". That makes thrashing worse. Good operating systems watch the page fault rate, not just CPU use.

Part 5: Common mistakes

  • Virtual memory = more RAM. No. It uses disk space to extend the address space; the disk is much slower.
  • Pages and frames mixed up. Pages belong to the process (logical). Frames are slots in physical RAM. They are the same size.
  • A page fault is an error. Usually not: it is a normal event that tells the OS to load a page. Only an access outside the process is an error.
  • Thrashing = a slow disk. Thrashing is caused by too few frames for the working sets, so pages are swapped constantly.

Try it 2Address translation drill

Page size is 100. Page table: page 0 → frame 3, page 1 → on disk, page 2 → frame 7, page 3 → frame 0, page 4 → on disk, page 5 → frame 2. Type the answer, or FAULT for a page fault.

Summary

IdeaRemember
Memory managerAllocates, protects, tracks and frees memory.
PagingLogical pages map to physical frames of the same size, using a page table.
Translationpage = address DIV size; offset = address MOD size; physical = frame × size + offset.
Virtual memoryKeep only needed pages in RAM; the rest wait on disk.
Page faultNeeded page is not in RAM; the OS loads it, replacing a victim if necessary.
ThrashingConstant swapping because frames are fewer than working sets; cure by running fewer processes or adding RAM.

Review questions

1. State three functions of memory management.

Show answer

Allocating memory to processes; protecting each process’s memory from others; keeping track of used and free memory; freeing memory when processes end; swapping pages between RAM and disk.

2. Explain what is meant by virtual memory.

Show answer

A technique in which the OS uses secondary storage as an extension of main memory. Only the pages a process is currently using are kept in RAM; others are stored on disk and loaded when needed, so programs can be larger than the free RAM.

3. Distinguish between a page and a frame.

Show answer

A page is a fixed-size block of a process’s logical memory. A frame is a block of physical RAM of the same size into which a page can be loaded.

4. With a page size of 100 and page 4 in frame 9, what is the physical address of logical address 462?

Show answer

Page 4, offset 62. Physical = 9 × 100 + 62 = 962.

5. What is a page fault, and what does the OS do when one occurs?

Show answer

An attempt to use a page that is not in RAM. The OS blocks the process, finds or frees a frame, loads the page from disk, updates the page table and makes the process Ready to retry the instruction.

6. Define thrashing and give one cause.

Show answer

A state in which the system spends most of its time swapping pages in and out rather than executing. Cause: too many processes for the available RAM, so each has fewer frames than its working set.

7. Suggest two ways to reduce thrashing.

Show answer

Reduce the number of running processes; add more RAM; give processes enough frames for their working sets; use a better page replacement policy.

8. Explain how paging helps protect processes from each other.

Show answer

Each process can only reach the frames listed in its own page table, so it cannot form an address in another process’s frames. An address outside its pages is trapped as an error.

Homework

  1. Run the Mixed string with 2, 3, 4 and 5 frames and record the faults. Draw a graph of faults against frames. What do you notice?
  2. Add a seventh page to pagetable.c and test an address in it.
  3. Change pagetable.c so that a page fault "loads" the page into frame 9 and then prints the translated address.
  4. Open the task manager on a computer while running many programs. Find the figures for physical memory and the page file (or swap). Write down what you see.

Mastery quiz: Virtual Memory, Paging and Thrashing

This 10-question quiz is separate from the short Quick quiz at the end of the lesson. Score 8/10 or higher to earn this lesson’s badge. Your best score is saved in this browser on this device.

Mastery quiz: Memory Management: Virtual Memory, Paging and Thrashing

Your course badges: loading saved progress…

View badges

    No course badges earned yet. Score 8/10 in a lesson quiz to earn its badge.

    Saved in this browser on this device. Clearing browser data may remove your badges.

    Answer all 10 questions. Earn this lesson’s badge at 8/10 or higher. Read the explanations, revisit the Try it activities and retry as often as you need. These are self-study badges, not formal grades.

    1. Virtual memory uses which device to extend main memory?
    2. A fixed-size block of physical RAM is called a...
    3. What records which frame holds each page?
    4. Page size 100. Logical address 356 is in page...
    5. Page size 100. Page 3 is in frame 8. Logical 356 maps to physical...
    6. A page fault occurs when...
    7. Thrashing is...
    8. Which reduces thrashing?
    9. FIFO page replacement removes...
    10. In the Try it, the Loop string with 3 frames had a fault rate of...

    Exam tips

    Define, then explain. For virtual memory, say what it is (disk used as an extension of RAM), how it works (pages swapped in and out, page table), and why it is useful. For thrashing, give the cause and the effect on performance.

    Show the working. In translation questions write page, offset and the multiplication separately. If the page size is a power of two, the same method applies; only the numbers change.

    Past paper practice. 2018 Unit 2 Paper 02 Question 5 and 2017 Unit 2 Paper 02 Question 5 include memory management. Worked answers are on the CS Solutions page.

    Previous: Unit 2 • Module 3 • Lesson 04 – CPU Scheduling and Deadlock. Next: Unit 2 • Module 3 • Lesson 06 – Files, Folders, Compression and File Security.

    Syllabus link: CAPE Computer Science (CXC A19/U2/21), Operating Systems and Computer Networks (Unit 2, Module 3). Specific Objective 3: describe the functions of operating systems (memory management: virtual memory, paging, thrashing).

    Video search terms

    Search YouTube for: "virtual memory paging explained", "page table address translation example", "what is thrashing operating system".

    Try it 3Quick quiz

    Tap an answer. All eight questions are self-checks on this lesson.

    Updates

    2026/12/01

    - First version of the lesson.

    © 2026 islandclass.org

    Leave a Reply