Memory Management in Operating Systems

Understanding Virtual Memory, MMU, Segmentation, and Paging

Memory Addressing

Logical Address Space

The set of all logical addresses generated by a program's machine code. Also known as virtual address space, it represents how a program views memory organization, independent of the actual physical memory available.

The logical address space is allocated to the process by the OS. The process is made to think like it is residing in the main memory but in reality it is just in a virtual memory allocated to it.

The CPU generates the logical address while the execution of the program and to locate that in the main memory, MMU translates the logical address into physical address.


Physical Memory

The actual RAM (Random Access Memory) hardware in the computer. It stores currently running programs, their data, and the operating system. Physical addresses directly correspond to memory hardware locations.

All the segments or the pages of the program need not be on the main memory everytime.
Only the needed portions are loaded onto the main memory and then swapped in and out of the memory according to the requirements.


Memory Management Unit (MMU)

The MMU is a hardware component that handles all memory operations between the CPU and physical memory. Its primary functions include:

  • Translation of logical addresses to physical addresses
  • Memory protection and access control
  • Cache management


Memory Address Translation

Segmentation

A memory management scheme in which the program is divided into segments (code, data, stack, heap) of varying sizes.

Now those segments are loaded onto the RAM, and they can be loaded non-contiguously.

The OS maintains a Segment Table that holds the information of the program's segments, that is, the base address and the limit of the address alloted to it.

Address Translation

Logical Address = <Segment Number, Offset>

  • Segment Number: Index into segment table
  • Offset: Distance from segment base
  • Physical Address = Base Address + Offset


Paging

Divides physical memory into fixed-size blocks called frames and logical memory into blocks of the same size called pages.

The OS maintains a Page Table containing the page index, frame number, valid/invalid bit etc.

Address Translation

Logical Address = <Page Number, Offset>

  • Page Number: Index into page table
  • Offset: Distance within page
  • Physical Address = Concatenate ("Frame Number" (2-bit oX) + "Offset" (3-bit oX))

Key Differences

Feature Segmentation Paging
Size Variable length Fixed size
Fragmentation External fragmentation Internal fragmentation
Memory Utilization Better for large segments Better for random allocations
Complexity More complex allocation Simpler allocation