Memory Management: Address Spaces, Translation, and Allocation

Address Space

Address space: The illusion of infinite memory presented to a program, representing a process’s memory. Virtual memory creates this illusion, extending RAM to the physical drive, allowing larger programs to run by swapping inactive components to disk.

Memory Allocation

Malloc – Allocates memory and returns a pointer to the block without initializing it.

Calloc – Allocates and initializes memory to zero.

Realloc – Resizes previously allocated blocks, trimming or expanding as needed without data loss.

Dangers of buffer overflow: Corrupts valid data, crashes programs.

Address Translation

Address translation maps virtual addresses to physical memory using the Memory Management Unit (MMU). The MMU converts CPU-generated virtual addresses to physical addresses, enabling efficient RAM use. This can be static or dynamic (on-demand).

Translation Methods

Base and Bounds

Defines memory limits with a base starting point and bounds end point. Advantages: Simple, secure. Disadvantages: Internal fragmentation, expensive.

Segmentation

Divides memory into logical segments. Advantages: Aligns with program structure, facilitates protection. Disadvantages: External fragmentation, management overhead.

Paging

Uses fixed-size pages. Advantages: Eliminates external fragmentation, supports virtual memory. Disadvantages: Internal fragmentation, potential overhead.

MMU Support

Base & Bounds: MMU adds base register to offset, ensuring it’s within bounds.

Segmentation: MMU uses segment tables to map segment numbers and offsets to physical addresses.

Paging: MMU uses page tables to convert virtual page numbers to frame numbers.

Heap Management

Heap management involves dynamic memory allocation, management, and deallocation within a program’s runtime. Functions like malloc, calloc, and realloc are used for allocation, and free for deallocation.

Allocation Strategies

  • First fit: Allocates the first available block.
  • Best fit: Allocates the smallest suitable block.
  • Worst fit: Allocates the largest block.

Memory tracking levels: Hardware, OS, and application.

Segmentation Practice

  1. Largest segment size = 2offset.
  2. Segment location: First two bits indicate the segment (e.g., 11 = stack).
  3. Physical address = Base + Offset.

Address Translation: Paging

Paging involves three levels: pages, page table, and page directory.

Page Table

  1. Maximum addressable bytes = 2virtual address size
  2. Number of pages = 2virtual address size / byte page size
  3. VPN bits = Exponent from step 2
  4. Offset bits = Denominator bit from step 2
  5. Page table size = Number of pages * byte PTE size
  6. Page table pages = Result from step 5 / page size in bytes
  7. PTEs per page = Page size in bytes / PTE in byte

Page Directory

  1. PDEs in Page Directory = Pages (as a power of 2)
  2. PDI bits in VPN = Exponent from step 8
  3. Bytes for Page Directory = Result from step 8 * byte PTE size (as a power of 2)
  4. Page directory size / page size

k = 210 M = 220 G = 230 T = 240