Session 4.5 – Swapping and Relocation

Understanding swapping mechanisms, dynamic relocation, and base-limit registers

1 Hour Module 4 Memory Management

Learning Objectives

By the end of this session, you will be able to:

  • Understand the concept and need for swapping in memory management
  • Explain different swapping mechanisms and strategies
  • Analyze the role of dynamic relocation in modern systems
  • Describe the function of base and limit registers
  • Compare different types of address binding
  • Evaluate the performance implications of swapping

Introduction to Swapping and Relocation

Swapping and relocation are fundamental techniques in memory management that allow operating systems to efficiently utilize limited physical memory and support multiprogramming by moving processes between main memory and secondary storage.

Why Swapping?
  • Limited physical memory resources
  • Support for multiprogramming
  • Process memory requirements exceed RAM
  • Temporary process suspension
  • Load balancing across memory hierarchy
Why Relocation?
  • Programs loaded at different addresses
  • Support for dynamic loading
  • Memory compaction requirements
  • Address space independence
  • Security and isolation
Memory Hierarchy and Swapping
Main Memory (RAM)
Process A

Process B

Free Space

Process C

Fast Access
Limited Size

Secondary Storage (Disk)
Process D

Process E

Swap Space

Process F

Slow Access
Large Size

Swapping Concepts

Swapping involves moving entire processes or parts of processes between main memory and secondary storage to optimize memory utilization and system performance.

Key Terminology

  • Swap Out: Move process from memory to disk
  • Swap In: Move process from disk to memory
  • Swap Space: Dedicated disk area for swapping
  • Backing Store: Secondary storage for swapped processes
  • Context Switch: Process state preservation
  • Roll Out/Roll In: Priority-based swapping
  • Thrashing: Excessive swapping overhead
  • Swap Time: Time to transfer data

Types of Swapping

Type Description When Used Granularity
Process Swapping Entire process moved to/from disk Memory shortage, process suspension Whole process
Page Swapping Individual pages moved to/from disk Virtual memory systems Page-level
Segment Swapping Individual segments moved to/from disk Segmented memory systems Segment-level
Selective Swapping Only inactive parts swapped out Intelligent memory management Variable

Swapping Process Timeline

1. Memory Pressure Detected
System identifies need for more memory space
2. Victim Selection
Choose process/page to swap out based on policy (LRU, FIFO, etc.)
3. Context Preservation
Save process state (registers, program counter, etc.)
4. Swap Out Operation
Transfer selected memory content to backing store
5. Memory Reclamation
Mark freed memory as available for allocation
6. Swap In (When Needed)
Load process back to memory when scheduled

Swapping Mechanisms and Policies

Different swapping mechanisms and policies determine when, what, and how to swap processes or memory segments.

When to Swap

  • Memory Full: No free memory available
  • Low Priority: Process has low scheduling priority
  • Idle Process: Process blocked for I/O
  • Time Quantum: Round-robin time expired
  • System Load: Excessive multiprogramming degree
Swap Decision Algorithm:
if (free_memory < threshold) {
    select_victim();
    if (victim.priority < current.priority) {
        swap_out(victim);
    }
}

What to Swap

  • Entire Process: Complete address space
  • Inactive Segments: Unused code/data
  • Clean Pages: Unmodified pages first
  • Large Processes: High memory footprint
  • Blocked Processes: Waiting for I/O
Victim Selection:
for each process p {
    score = priority_weight * p.priority +
            size_weight * p.memory_size +
            idle_weight * p.idle_time;
    if (score > best_score) {
        victim = p;
    }
}

Swapping Performance Metrics

Time Components
  • Seek Time: Disk head positioning
  • Rotational Latency: Wait for sector
  • Transfer Time: Data read/write
  • Context Switch: Process state save/restore
Performance Formula
Total Swap Time:
T_swap = T_context_save + 
         T_disk_access + 
         T_transfer + 
         T_context_restore

Example (100MB process):
T_swap = 1ms + 10ms + 200ms + 1ms = 212ms
Swapping Operation Example
Before Swap
Process A
Priority: 5

Process B
Priority: 3

Process C
Priority: 8

Memory Full!

Need: Process D (Priority: 7)

During Swap
Process A
Priority: 5

Process B
Swapping Out...

Process C
Priority: 8

Process B
On Disk
After Swap
Process A
Priority: 5

Process D
Priority: 7

Process C
Priority: 8

Free Space

Memory Available

Dynamic Relocation

Dynamic relocation allows programs to be loaded and executed from different memory locations without modification, enabling flexible memory management and supporting swapping operations.

Address Translation in Dynamic Relocation

Logical Address
Generated by CPU
1000
+
Base Register
Relocation Register
14000
Physical Address
Sent to Memory
15000
Benefits of Dynamic Relocation
  • Programs can run at any memory location
  • Supports memory compaction
  • Enables efficient swapping
  • Simplifies linking and loading
  • Provides memory protection
  • Supports dynamic loading
Implementation Requirements
  • Hardware support (MMU)
  • Base and limit registers
  • Address translation on every access
  • Context switching overhead
  • Operating system management
  • Protection violation handling
Process Relocation Example
Initial Loading (Base = 0)
Program Code:
LOAD R1, 100    ; Load from address 100
ADD  R1, R2     ; Add registers
STORE 200, R1   ; Store to address 200

Logical Addresses: 100, 200
Physical Addresses: 100, 200
After Relocation (Base = 5000)
Same Program Code:
LOAD R1, 100    ; Load from address 100
ADD  R1, R2     ; Add registers  
STORE 200, R1   ; Store to address 200

Logical Addresses: 100, 200
Physical Addresses: 5100, 5200

Same program, different physical locations - no code changes needed!

Base and Limit Registers

Base and limit registers provide a simple but effective mechanism for dynamic relocation and memory protection in single-user or simple multiprogramming systems.

Hardware Implementation

MMU Components:
┌─────────────────────┐    ┌─────────────────────┐
│   Base Register     │    │   Limit Register    │
│      (Relocation)   │    │     (Protection)    │
│                     │    │                     │
│      14000          │    │       2000          │
└─────────────────────┘    └─────────────────────┘
           │                           │
           ▼                           ▼
┌─────────────────────────────────────────────────┐
│           Memory Management Unit (MMU)          │
│                                                 │
│  Physical_Addr = Logical_Addr + Base           │
│  if (Logical_Addr >= Limit) → Protection_Fault │
└─────────────────────────────────────────────────┘

Base Register

  • Purpose: Address translation/relocation
  • Content: Starting physical address of process
  • Operation: Added to every logical address
  • Update: Changed during context switches
  • Access: Privileged instruction only
Base Register Usage:
Process A: Base = 0     (0-4K)
Process B: Base = 4000  (4K-8K) 
Process C: Base = 8000  (8K-12K)

Context Switch:
OLD_BASE = save_base_register();
load_base_register(NEW_BASE);

Limit Register

  • Purpose: Memory protection and bounds checking
  • Content: Maximum valid logical address
  • Operation: Compared with every logical address
  • Protection: Prevents access beyond process memory
  • Fault: Generates interrupt on violation
Protection Check:
if (logical_address >= limit_register) {
    generate_protection_fault();
    return;
}
physical_address = logical_address + base_register;
access_memory(physical_address);
Base-Limit Register Operation
Process Memory Layout
Process Code
0-1000

Process Data
1000-1500

Unused
1500-2000

Limit = 2000

Valid Access (Addr: 800)
Logical: 800
Check: 800 < 2000 ✓
Base: 14000
Physical: 800 + 14000 
        = 14800 ✓

Access Granted
Invalid Access (Addr: 2500)
Logical: 2500  
Check: 2500 < 2000 ✗
Limit Exceeded!

Protection Fault
Generated

Advantages and Limitations

Advantages
  • Simple hardware implementation
  • Fast address translation
  • Automatic memory protection
  • Support for relocation
  • Process isolation
Limitations
  • Only contiguous memory allocation
  • No sharing between processes
  • Fixed process size
  • External fragmentation issues
  • Limited flexibility

Address Binding

Address binding is the process of mapping program addresses to actual memory locations. Different binding strategies offer various trade-offs between flexibility and performance.

Binding Type When Performed Flexibility Examples Use Cases
Compile Time During compilation None - Fixed addresses MS-DOS .COM files Single-user systems, embedded
Load Time When program loaded Limited - Fixed during execution Relocatable code Simple multiprogramming
Execution Time During program execution High - Can change during runtime Base-limit registers, paging Modern operating systems

Address Binding Timeline

Source Code
int x; x = 10;
Variable 'x' has symbolic address
Compilation
STORE 100, #10
Symbolic address → Relative address (100)
Linking
STORE 1100, #10
Relative address → Logical address (1100)
Loading/Execution
Physical address: 15100
Logical address (1100) + Base (14000) = Physical (15100)
Compile-Time Binding
Characteristics:
• Fixed load address
• No relocation possible
• Must recompile to move
• Simple implementation

Example:
Program always loads 
at address 1000
Load-Time Binding
Characteristics:
• Address set at load time
• Cannot move during execution
• Relocatable object code
• Loader performs binding

Example:
Loader places program
at first available space
Execution-Time Binding
Characteristics:
• Dynamic address translation
• Can move during execution
• Hardware support required
• Maximum flexibility

Example:
Base register updated
during context switches

Performance Analysis

Understanding the performance implications of swapping and relocation is crucial for system design and optimization.

Swapping Performance Calculations

Time Analysis
Swap Out Time:
T_out = T_context_save + T_write_to_disk

Swap In Time:  
T_in = T_read_from_disk + T_context_restore

Total Swap Time:
T_total = T_out + T_in

Example (50MB process, 100MB/s disk):
T_write = T_read = 50MB / 100MB/s = 0.5s
T_context = 1ms (typical)
T_total = 1ms + 500ms + 500ms + 1ms ≈ 1s
Throughput Impact
System Utilization:
Without swapping: 100% CPU utilization possible
With swapping: Reduced due to swap overhead

Effective CPU Utilization:
U_eff = U_cpu × (1 - swap_fraction × swap_time)

Example:
If 10% of time spent swapping:
U_eff = 100% × (1 - 0.1 × 1.0) = 90%
Performance Optimization
  • Faster Storage: SSDs vs HDDs
  • Dedicated Swap Space: Separate partition
  • Compression: Reduce data transfer
  • Intelligent Scheduling: Predictive swapping
  • Memory Hierarchy: Multiple swap levels
  • Partial Swapping: Only dirty pages
Thrashing

Definition: Excessive swapping that degrades performance

  • Cause: Insufficient physical memory
  • Symptom: High disk I/O, low CPU utilization
  • Effect: System becomes unresponsive
  • Solution: Reduce multiprogramming degree
Performance vs Memory Usage
Memory Usage Regions:

Region 1: Sufficient Memory (0-80% usage)
├─ No swapping needed
├─ Optimal performance
└─ CPU utilization: 95-100%

Region 2: Memory Pressure (80-95% usage)  
├─ Occasional swapping
├─ Some performance degradation
└─ CPU utilization: 70-90%

Region 3: Heavy Swapping (95-100% usage)
├─ Frequent swapping  
├─ Significant slowdown
└─ CPU utilization: 30-60%

Region 4: Thrashing (>100% demand)
├─ Continuous swapping
├─ System nearly unusable
└─ CPU utilization: 5-20%

Practical Examples

Swapping Time Calculation

Scenario:

System with 4GB RAM, 500GB HDD (100MB/s), running 10 processes averaging 800MB each

System Analysis:

Total Memory Demand: 10 × 800MB = 8GB
Available Memory: 4GB  
Memory Shortage: 8GB - 4GB = 4GB

Swapping Requirements:
Processes in Memory: 4GB ÷ 800MB = 5 processes
Processes on Disk: 10 - 5 = 5 processes

Swap Operation Times:
Process Size: 800MB
Transfer Rate: 100MB/s
Swap Out Time: 800MB ÷ 100MB/s = 8 seconds
Swap In Time: 800MB ÷ 100MB/s = 8 seconds  
Context Switch: 2ms (negligible)

Total Context Switch Time:
If process needs to be swapped in: 8 seconds
If process already in memory: 2ms

Performance Impact:
Average swap frequency: 2 swaps per minute
Time spent swapping: 2 × 8s = 16s per minute
System overhead: 16/60 = 26.7%
Effective performance: 100% - 26.7% = 73.3%

Dynamic Relocation Example

Program: Simple calculator with base-limit register implementation

Initial Load (Base = 2000, Limit = 1000)
Program Instructions:
100: LOAD  R1, 500    ; Load variable
104: ADD   R1, R2     ; Add numbers  
108: STORE 600, R1    ; Store result
112: HALT

Address Translation:
Logical 500 → Physical 2500 ✓
Logical 600 → Physical 2600 ✓
All accesses within limit (1000) ✓
After Relocation (Base = 5000, Limit = 1000)
Same Program Instructions:
100: LOAD  R1, 500    ; Load variable
104: ADD   R1, R2     ; Add numbers
108: STORE 600, R1    ; Store result  
112: HALT

New Address Translation:
Logical 500 → Physical 5500 ✓
Logical 600 → Physical 5600 ✓
Program runs without modification!
Protection Example
Invalid Access Attempt:
Instruction tries to access logical address 1200
Check: 1200 < 1000 (limit) → FALSE
Result: PROTECTION FAULT generated
Action: OS terminates process or handles exception

Hardware Implementation:
if (logical_address >= limit_register) {
    generate_interrupt(PROTECTION_VIOLATION);
    return ERROR;
}
physical_address = logical_address + base_register;
return access_memory(physical_address);

Session Summary

Key Concepts
  • Swapping: Moving processes between memory and storage
  • Dynamic Relocation: Runtime address translation
  • Base-Limit Registers: Simple relocation and protection
  • Address Binding: Mapping logical to physical addresses
  • Performance Impact: Swap time affects system throughput
  • Thrashing: Excessive swapping degrades performance
Design Trade-offs
  • Memory vs Performance: More RAM reduces swapping
  • Storage Speed: SSDs improve swap performance
  • Complexity vs Flexibility: Advanced techniques cost more
  • Hardware Support: MMU required for efficiency
  • Multiprogramming: Balance between throughput and response
Module 4 Complete!

This concludes Module 4 on Memory Management. We've covered contiguous allocation, paging, segmentation, and swapping techniques. Next: Module 5 will explore I/O and File Management, including virtual memory fundamentals and file system concepts.