Paging vs Segmentation: Key Differences and When to Use Each
paging divides memory into fixed-size blocks called pages, while segmentation divides memory into variable-sized segments based on logical divisions. Paging avoids external fragmentation but can cause internal fragmentation, whereas segmentation matches program structure but may suffer external fragmentation.Quick Comparison
This table summarizes the main differences between paging and segmentation in memory management.
| Factor | Paging | Segmentation |
|---|---|---|
| Memory Division | Fixed-size pages | Variable-size segments |
| Addressing | Uses page number and offset | Uses segment number and offset |
| Fragmentation | Internal fragmentation possible | External fragmentation possible |
| Logical View | No relation to program structure | Matches logical program units |
| Protection & Sharing | Limited by page granularity | Easier to protect/share segments |
| Complexity | Simpler hardware support | More complex due to variable sizes |
Key Differences
Paging breaks physical memory into equal-sized blocks called pages and maps them to fixed-size frames in physical memory. This makes memory allocation simple and avoids external fragmentation, but can waste space inside pages if the data does not fill them completely, causing internal fragmentation.
Segmentation divides memory into segments that correspond to logical parts of a program, like functions or data arrays. These segments vary in size, which fits the program's structure better and allows easier sharing and protection of segments. However, because segments are variable-sized, it can cause external fragmentation where free memory is split into small unusable pieces.
Paging uses a two-part address (page number and offset) to locate data, while segmentation uses a segment number and offset. Paging is simpler to implement in hardware, while segmentation requires more complex management to handle variable sizes and fragmentation.
Code Comparison
Example of how paging translates a logical address to a physical address using page number and offset.
def paging_address_translation(logical_address, page_size, page_table): page_number = logical_address // page_size offset = logical_address % page_size frame_number = page_table.get(page_number, None) if frame_number is None: return None # Page fault physical_address = frame_number * page_size + offset return physical_address # Example usage page_table = {0: 5, 1: 9, 2: 3} # Maps page numbers to frame numbers logical_address = 1026 # Suppose page size is 1024 bytes page_size = 1024 physical_address = paging_address_translation(logical_address, page_size, page_table) print(physical_address)
Segmentation Equivalent
Example of how segmentation translates a logical address using segment number and offset.
def segmentation_address_translation(logical_address, segment_table): segment_number, offset = logical_address segment = segment_table.get(segment_number, None) if segment is None or offset >= segment['length']: return None # Segment fault physical_address = segment['base'] + offset return physical_address # Example usage segment_table = { 0: {'base': 1000, 'length': 500}, 1: {'base': 2000, 'length': 1000}, 2: {'base': 4000, 'length': 1500} } logical_address = (1, 300) # Segment 1, offset 300 physical_address = segmentation_address_translation(logical_address, segment_table) print(physical_address)
When to Use Which
Choose paging when you want simple, uniform memory management that avoids external fragmentation and when hardware support for fixed-size pages is available. It is ideal for systems where memory protection and sharing at a fine granularity are less critical.
Choose segmentation when you need to reflect the logical structure of programs in memory, such as separating code, data, and stack segments. It is better when you want easier sharing and protection of logical units but can handle the complexity of managing variable-sized memory blocks and external fragmentation.