Chapter 8: Main Memory¶
Main Concepts¶
Main memory provides the CPU with fast access to instructions and data.
The operating system (OS) manages memory allocation between processes.
Memory management ensures: - Efficient utilization of available memory - Process isolation and protection - Fast address translation
Background: Logical vs. Physical Address Space¶
- Logical Address (Virtual Address)
Generated by the CPU during program execution.
Used by a process as if it has its own continuous memory space.
- Physical Address
The actual location in main memory (RAM).
Determined after translation from the logical address.
- Address Binding
The process of mapping logical to physical addresses.
Occurs at one of three stages: - Compile-time binding: If memory location known at compile time. - Load-time binding: Performed when program is loaded. - Execution-time binding: Requires hardware support (MMU).
- Memory-Management Unit (MMU)
Hardware that translates logical addresses to physical addresses at runtime.
Allows process relocation and protection.
Swapping¶
Swapping moves processes between main memory and a backing store (e.g., disk).
Used to free memory for other processes.
- Key Points
Entire processes are swapped out/in.
Swap time is proportional to process size and disk transfer speed.
Can cause high I/O overhead.
- Swap Space
A dedicated area on disk used for temporarily holding swapped-out processes.
Contiguous Memory Allocation¶
Each process is allocated one continuous block of physical memory.
- Memory Partitioning Methods
Fixed Partitioning: - Memory divided into fixed-sized blocks. - Can cause internal fragmentation (unused space inside allocated region).
Dynamic Partitioning: - Partitions created as needed. - Can cause external fragmentation (free space scattered between blocks).
- Allocation Strategies
First Fit: Allocate first block large enough.
Best Fit: Allocate smallest suitable block.
Worst Fit: Allocate largest available block.
- Compaction
A technique to reduce external fragmentation by moving processes together.
Paging¶
Solves the problem of external fragmentation.
Divides physical memory into fixed-sized frames.
Divides logical memory into pages of the same size.
- Address Translation
Logical address = (page number, offset)
Page number used to index the page table.
Page table maps logical pages to physical frames.
- Advantages
No external fragmentation.
Easy process relocation.
Efficient memory use.
- Disadvantages
Requires additional memory for the page table.
Can cause internal fragmentation (unused space in frames).
- Hardware Support
Translation Lookaside Buffer (TLB): Cache for fast page-table lookups.
Segmentation¶
Divides memory into variable-sized segments based on program structure (e.g., code, data, stack).
- Logical Address Structure
(segment number, offset)
- Segment Table
Each entry has: - Base address: Start of the segment in physical memory. - Limit: Length of the segment.
- Advantages
Reflects program’s logical organization.
Supports protection and sharing easily.
- Disadvantages
Can cause external fragmentation.
More complex memory management than paging.
Summary¶
Technique |
Fragmentation Type / Notes |
|---|---|
Contiguous Allocation |
Internal and External Fragmentation |
Paging |
Internal Fragmentation Only |
Segmentation |
External Fragmentation Only |
Swapping |
Performance Overhead (Disk I/O) |
- Key Takeaways
Logical and physical addresses are distinct.
Paging and segmentation provide flexibility.
Swapping enables multitasking at the cost of performance.
The OS balances efficiency, speed, and protection in memory management.