Back to the 2015 paper

Module 4: Memory Organization

201514m

What is swapping and how is it useful in memory management?

Worked SolutionAI Assisted

Swapping and Use in Memory Management

Definition

Swapping is moving a process (or part) temporarily out of main memory (RAM) to secondary storage (backing store / swap space on disk) and bringing it back later for execution. Introduced for multiprogramming when RAM insufficient.

Swap Out: RAM --> Disk; Swap In: Disk --> RAM. Managed by medium-term scheduler (swapper) and dispatcher.

How Swapping Works

  1. System keeps Ready Queue of processes in memory and Swap Device queue on disk.
  2. When memory pressure high or process blocked (e.g. I/O wait) and higher priority process arrives, dispatcher selects victim (e.g. lowest priority, sleeping).
  3. Victim's entire address space (or at least resident pages) is copied to swap file, its PCB updated (state = swapped, location = disk), frames freed.
  4. Later when memory free or process becomes ready, swapper selects swapped process, finds contiguous / non-contiguous free frames, loads it back, possibly at different physical address (requires relocation via base-limit registers or paging).

Time = swap size / transfer rate. E.g. 10 MB process, disk 20 MB/s => 0.5s out +0.5s in =1s overhead -> swapping is expensive, attempted only when needed.

Types

  • Whole process swapping (old UNIX, embedded) - simple but costly.
  • Page swapping / paging (modern VM) - swap only pages/frames, not whole process.
  • Roll in / Roll out for priority scheduling.

Usefulness / Advantages

  1. Degree of Multiprogramming: Allows more processes than fits in RAM to co-exist by multiplexing memory over time, increasing CPU utilization.
  2. Handles Insufficient Memory: Large process (>RAM) can still run by swapping parts; avoids need for all processes to fit simultaneously.
  3. Compaction: Swapping out creates holes, allows memory compaction to reduce external fragmentation (combine free blocks) and allocate contiguous memory.
  4. Priority Scheduling: High priority process swapped in quickly by swapping out low priority.
  5. I/O Idle Utilization: Blocked process swapped out so its RAM used by ready processes.
  6. Foundation for Virtual Memory: Modern demand paging is refined swapping per page with lazy loading.

Disadvantages / Considerations

  • Swap time overhead and disk I/O bottleneck; double transfer.
  • Need for relocation hardware.
  • Not suitable for real-time if swap time unpredictable.
  • Thrashing if swapped too frequently.
  • Requires swap space reservation on disk.

Swapping vs Paging

Swapping Paging (demand)
Whole process moved Only required pages moved
Contiguous allocation needed Non-contiguous frames ok
High overhead Lower per-fault overhead

Effective only if combined with compaction and medium-term scheduling; modern OS uses swapping + virtual memory so that Total Logical Memory >> Physical RAM.

Diagram:

Main Memory                Disk (Swap Space)
[ P1 ][hole][ P2 ]   swap out P2 -->  [P2 image]
[ P1 ][ P3 ][hole]   <-- swap in P4   [P4 image][P2]

Free frames consolidated after swap.

Similar questions

Budget AppAndroid App

Pocketly on Android

Log daily food, travel, and hostel expenses instantly. Set monthly spending limits and see where your money goes without complex spreadsheets.