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
- Largest segment size = 2offset.
- Segment location: First two bits indicate the segment (e.g., 11 = stack).
- Physical address = Base + Offset.
Address Translation: Paging
Paging involves three levels: pages, page table, and page directory.
Page Table
- Maximum addressable bytes = 2virtual address size
- Number of pages = 2virtual address size / byte page size
- VPN bits = Exponent from step 2
- Offset bits = Denominator bit from step 2
- Page table size = Number of pages * byte PTE size
- Page table pages = Result from step 5 / page size in bytes
- PTEs per page = Page size in bytes / PTE in byte
Page Directory
- PDEs in Page Directory = Pages (as a power of 2)
- PDI bits in VPN = Exponent from step 8
- Bytes for Page Directory = Result from step 8 * byte PTE size (as a power of 2)
- Page directory size / page size
k = 210 M = 220 G = 230 T = 240