Paging vs Segmentation

3
0

Introduction

Paging and segmentation both help the operating system manage process memory, but they come from different viewpoints. Paging looks at memory as fixed-size units, while segmentation looks at memory as meaningful program parts.

The main difference is how memory is divided and mapped. Paging is more focused on physical memory management, while segmentation is closer to the programmer’s logical view of a program.

What Segmentation Means

Segmentation divides a process into logical parts called segments. These segments represent meaningful regions of a program.

Common segments include:

  • Code segment: Contains program instructions.

  • Data segment: Contains global and static data.

  • Heap segment: Stores dynamically allocated memory.

  • Stack segment: Stores function calls, local variables, and return information.

Segments are variable in size. For example, a code segment may be 10 KB, a heap segment may be 18 KB, and a stack segment may be 6 KB.

In pure segmentation, each segment needs one continuous physical memory block large enough to hold that segment.

What Paging Means

Paging divides virtual memory into fixed-size pages and physical memory into fixed-size frames. Page size and frame size are equal, so any page can fit into any free frame.

For example, if the page size is 4 KB, a 10 KB code region is divided into three pages. The first two pages are full, and the last page is partially used.

Pages do not need to be placed next to each other in RAM. One page may go into frame 2, another into frame 8, and another into frame 15. The page table stores this mapping.

Segmentation and Paging

Segmentation and Paging

Different Views of Memory

Segmentation follows the logical structure of the program. It keeps code, heap, stack, and data as separate meaningful units.

Paging ignores that logical meaning and divides memory into equal-sized blocks for easier allocation and mapping.

This is why segmentation is called a programmer’s logical view, while paging is mainly an OS and hardware memory-management view.

Address Format

Both paging and segmentation split an address into two parts, but the first part means something different.

In paging: Logical address = Page number + Offset

The page number is used to find the frame number from the page table. The offset remains unchanged.

Thus, Physical address = Frame number * Page size + Offset

In segmentation: Logical address = Segment number + Offset

The segment number is used to find the segment’s base and limit from the segment table. The offset is first checked against the limit.

  • If offset < limit: Physical address = Base + Offset

  • If offset ≥ limit: Invalid memory access

Paging replaces page number with frame number. Segmentation checks whether the offset is valid inside the segment, then adds it to the segment base.

Address Format

Address Format

Fragmentation Difference

Paging and segmentation have different fragmentation behavior.

Paging removes external fragmentation because all frames are the same size and any page can fit into any free frame. However, paging can have internal fragmentation when the last page is not fully used.

Segmentation can have external fragmentation because each segment needs one continuous physical block. If free memory is scattered into small holes, a large segment may not fit even when total free memory is enough.

For example, if free holes are 100 KB, 60 KB, 90 KB, and 150 KB, a segment requiring 160 KB cannot be allocated. Total free memory is enough, but no single hole is large enough.

Paging vs Segmentation

Aspect

Paging

Segmentation

Basic unit

Fixed-size pages and frames

Variable-size logical segments

View

OS and hardware memory-management view

Programmer’s logical view

Address format

Page number + offset

Segment number + offset

Mapping table

Page table maps page to frame

Segment table stores base and limit

Physical placement

Pages can be placed in non-contiguous frames

Each segment needs one continuous block

The most important comparison is simple: paging divides memory by equal size, while segmentation divides memory by meaning.

Example View

Suppose a process has 10 KB of code, 18 KB of heap, and 6 KB of stack.

Under segmentation, memory is viewed as three logical segments: code, heap, and stack. Each segment is placed in a continuous physical block of its required size.

Under paging with a 4 KB page size:

Region

Size

Pages Needed

Code

10 KB

3 pages

Heap

18 KB

5 pages

Stack

6 KB

2 pages

These pages can be placed in different physical frames. The OS uses the page table to remember where each page is stored.

Summary

Paging divides memory into fixed-size pages and frames. It allows non-contiguous physical allocation, avoids external fragmentation, and uses a page table for address translation.

Segmentation divides memory into logical, variable-size segments such as code, data, heap, and stack. It matches the program’s structure, but pure segmentation can suffer from external fragmentation because each segment needs a continuous physical block.

CS Core

Read Similar Blogs

Comments0