Beyond Physical Memory - Swap Space

 

Beyond Physical Memory: Mechanisms

So far, we assumed:

  • Every process fits entirely in physical memory (RAM)

  • All pages of all running processes are always resident in RAM

This is unrealistic.

Modern systems must support:

  • Many processes

  • Large address spaces

  • Memory usage bigger than physical RAM

To achieve this, the OS introduces another level in the memory hierarchy


The Problem

Physical memory is:

  • Limited

  • Expensive

  • Small compared to disk

But programs expect:

  • Large virtual address spaces

  • Simple memory allocation

  • No manual memory management


✅ The Solution: Use Disk as an Extension of Memory

The OS uses a large, slower storage device (traditionally a hard disk, now often SSD) as backup memory.

This creates the illusion:

Virtual Memory > Physical Memory

This mechanism is called swapping.


Why Support Large Virtual Address Spaces?

Because it makes programming easier.

Without virtual memory:

  • Programmers must manually load/unload code (memory overlays)

  • Must worry about memory fitting

  • Must manage code/data placement

With virtual memory:

  • Programs allocate memory naturally

  • OS transparently handles movement

  • Simpler programming model


Multiprogramming and Memory Pressure

When multiple programs run:

  • Total memory demand may exceed RAM

  • Not all pages of all processes can fit

Thus:

  • Some pages stay in RAM

  • Some pages are placed on disk

  • OS swaps pages in and out dynamically

 Swapping and Swap Space 

To support programs whose total memory demand exceeds RAM, the OS uses disk space as an extension of memory. This mechanism is called swapping, and the disk area used for it is called swap space.


What Is Swap Space?

Swap space is:

  • A reserved region on disk

  • Used to store pages moved out of RAM

  • Managed in page-sized units

The OS must:

  • Track which virtual page is stored in which disk block

  • Update page tables accordingly


What Is Swapping?

Swapping is the process of:

  • Moving a page from RAM → disk (swap out)

  • Moving a page from disk → RAM (swap in)

It allows:

Total memory in use > Physical memory size


 Why Do We Need It?

Because:

  • Many processes run at the same time

  • Each process may have a large address space

  • RAM is limited

Without swapping:

  • Some processes could not run

  • System would run out of memory

Example




Physical Memory:

4 page frames (PFN 0–3)

Swap Space:

8 disk blocks

Active Processes:

  • Proc 0

  • Proc 1

  • Proc 2

  • Proc 3


What Is in RAM?

Only some pages of processes:

  • Proc 0 → VPN 0

  • Proc 1 → VPN 2, VPN 3

  • Proc 2 → VPN 0


 What Is in Swap?

Other pages are stored on disk:

  • Proc 0 → VPN 1, VPN 2

  • Proc 1 → VPN 0, VPN 1

  • Proc 2 → VPN 1

  • Proc 3 → VPN 0, VPN 1 (all pages swapped out)

Proc 3 is not currently running because:

  • None of its pages are in memory.

How Swapping Happens

When RAM Is Full

  1. OS selects a victim page.

  2. If the page was modified → write to swap.

  3. Mark page as not present.

  4. Free the frame.

  5. Load new needed page.


When a Missing Page Is Accessed

  1. CPU triggers a page fault.

  2. OS finds page location (swap or file).

  3. OS loads it into RAM.

  4. Page table updated.

  5. Instruction restarts.


Important: Swap vs File System

Not all pages need swap space.

There are two types:


1️⃣ Anonymous Pages (Heap/Stack)

  • Created during execution

  • No original copy on disk

  • Must be written to swap when evicted


2️⃣ File-Backed Pages (Program Code)

Example: Running ls

  • Code is already stored on disk (executable file)

  • When evicted:

    • No need to write to swap

    • Just discard

    • Reload later from the executable file

This reduces swap usage.


Why Swap Space Size Matters

Swap space determines:

  • Maximum number of pages system can support

  • Total possible virtual memory usage

If swap is small:

  • System may refuse allocations

  • Processes may be killed

If swap is large:

  • System can support more memory pressure


 Performance Considerations

RAM access: nanoseconds
Disk access: milliseconds

Disk is thousands of times slower.

Too much swapping causes:

🔥 Thrashing

  • Constant page faults

  • Heavy disk I/O

  • System becomes extremely slow


Key Concepts Summary

TermMeaning
Swap Space        Disk area used to store evicted pages
Swap Out        Move page from RAM to disk
Swap In        Load page from disk to RAM
Page Fault        Occurs when page not in RAM
File-backed page        Can be reloaded from executable
Anonymous page        Must use swap space

Summary

Swap space allows the OS to:

  • Pretend memory is larger than it really is

  • Support many processes

  • Provide large virtual address spaces

  • Maintain ease of programming

It works by dynamically moving pages between RAM and disk, keeping active pages in memory and inactive pages on disk.

Comments

Popular posts from this blog

Operating Systems OS PCCST403 Semester 4 BTech KTU CS 2024 Scheme

Introduction to Operating System -Virtualization, Concurrency, and Persistence

Operating Systems PCCST403 Scheme and Syllabus