Basic Memory Management Concepts Address spaces Physical address space — The address space supported by the hardware ! MAXsys! Ø Starting at address 0, going to address MAXsys! Logical/virtual address space — A process’s view of its own memory ! Memory Management Basics MAXprog! Program P Ø Starting at address 0, going to address MAXprog 0! But where do addresses come from? MOV r0, @0xfffa620e! 0! 1 2 Basic Concepts Address generation ! ! Which is bigger, physical or virtual address space? The compilation pipeline 0 Ø A. Physical address space Ø B. Virtual address space Ø C. It depends on the system. 1000 Library Routines Library Routines 0 P:! :! push ...! inc SP, x! jmp _foo! :! foo: ... prog P! :! :! foo()! :! :! end P! Compilation 100 :! push ...! inc SP, 4! jmp 75! :! ... 75 Assembly 175 1100 :! :! :! jmp 175! :! ... Linking 1175 :! :! :! jmp 1175! :! ... Loading 3 Basic Concepts (Cont’d.) Program Relocation ! ! ! ! Address Translation Program issues virtual addresses Machine has physical addresses. If virtual == physical, then how can we have multiple programs resident concurrently? Instead, relocate virtual addresses to physical at run time. MAXsys! MEMORY! EXCEPTION! CPU Ø While we are relocating, also bounds check addresses for safety. ! 4 Logical Addresses Instructions MAXprog! I can relocate that program (safely) in two registers… Program P’s logical address space 0 5 no ≤ 500 yes + Physical Addresses 1000 1500!Program P’s physical address 1000! space Limit Base Register Register 0! 6 Evaluating Dynamic Allocation Techniques The fragmentation problem ! ! With base and bounds registers, the OS needs a hole in physical memory at least as big as the process. External fragmentation Ø Unused memory between units of allocation Ø E.g, two fixed tables for 2, but a party of 4 Ø A. True Ø B. False ! MAX! Internal fragmentation Ø Unused memory within a unit of allocation Ø E.g., a party of 3 at a table for 4 Program Code (“text”) Program Q’s PAS Execution Data Stack Execution Stack Program 0!R’s PAS 7 Simple Memory Management Schemes First Fit Allocation Dynamic allocation of partitions Simple approach: ! MAX! Program P1 To allocate n bytes, use the first available free block such that the block size is larger than n. Ø Allocate a partition when a process is admitted into the system Ø Allocate a contiguous memory partition to the process 8 Program P2 1K bytes OS keeps track of... Full-blocks Empty-blocks (“holes”) P5 2K bytes 2K bytes 500 bytes 500 bytes Program P3 Allocation strategies First-fit Best-fit Worst-fit To allocate 400 bytes, we use the 1st free block available Program P4 0! 9 Rationale & Implementation ! Simplicity of implementation ! Requires: Best Fit Allocation To allocate n bytes, use the smallest available free block such that the block size is larger than n. Ø Free block list sorted by address Ø Allocation requires a search for a suitable partition Ø De-allocation requires a check to see if the freed partition could be merged with adjacent free partitions (if any) Advantages Simple Tends to produce larger free blocks toward the end of the address space 10 1K bytes 1K bytes 2K bytes 2K bytes Disadvantages Slow allocation External fragmentation To allocate 400 bytes, we use the 3rd free block available (smallest) 11 500 bytes 12 Rationale & Implementation Worst Fit Allocation ! To avoid fragmenting big free blocks ! To minimize the size of external fragments produced ! Requires: To allocate n bytes, use the largest available free block such that the block size is larger than n. Ø Free block list sorted by size Ø Allocation requires search for a suitable partition Ø De-allocation requires search + merge with adjacent free partitions, if any ! 1K bytes 2K bytes Disadvantages Advantages Works well when most allocations are of small size Relatively simple 1K bytes External fragmentation Slow de-allocation Tends to produce many useless tiny fragments (not really great) To allocate 400 bytes, we use the 2nd free block available (largest) 500 bytes Doug Lea’s malloc “In most ways this malloc is a best-fit allocator” 13 Rationale & Implementation 14 Allocation strategies ! To avoid having too many tiny fragments ! Requires: ! Ø A. True Ø B. False Ø Free block list sorted by size Ø Allocation is fast (get the largest partition) Ø De-allocation requires merge with adjacent free partitions, if any, and then adjusting the free block list Advantages Disadvantages Works best if allocations are of medium sizes First fit, best fit and worst fit all suffer from external fragmentation. Slow de-allocation External fragmentation Tends to break large free blocks such that large partitions cannot be allocated 15 Dynamic Allocation of Partitions Memory Management Eliminating Fragmentation ! Sharing Between Processes Compaction MAX! Program P1 ! ! Swapping Ø Preempt processes & reclaim their memory Ready Suspended suspended queue 2n-1! Ø Relocate programs to coalesce holes ready queue 16 ? Ø A single name space per process Ø No sharing Program P2 How can one share code and data between programs without paging? Program P3 Running Heap Run-Time Program Stack P’s VAS Program Data Program Text Waiting Program P4 semaphore/condition queues Schemes so far have considered only a single address space per process 0! 17 0! Program P’s VAS 18 Supporting Multiple Name Spaces Multiple Name Spaces Segmentation Example — Protection/Fault isolation & sharing 2n-1! New concept: A segment — a memory “object” ! 2n -1! 4 Heap Ø A virtual address space Heap 2n -1! Run-Time Stack A process now addresses objects —a pair (s, addr) ! 0! 3 Ø s — segment number Ø addr — an offset within an object Run-Time Stack 2n -1! Program Data 2n -1! Program Program 0! Text Text 6 Libraries n2 0! 0 2 -1! User Code 0! 0! n1 s n5 0! know size of object, so 32 bits for offset? 2n -1! Program Data 1 Don’t 0! 2 0 n addr Segment + Address register scheme 0 s addr Single address scheme 19 Implementing Segmentation Program P ! n ! Segmentation allows sharing ! … but leads to poor memory utilization Ø We might not use much of a large segment, but we must keep the whole thing in memory (bad memory utilization). Ø Suffers from external fragmentation Ø Allocation/deallocation of arbitrary size segments is complex 1500! MEMORY! EXCEPTION! o 0 32 Are We Done? Add a segment table containing base & limit register values CPU s Memory Management Basics Physical Memory Base + Limit register scheme 20 Program P’s Segment 1000! no 0 Logical Addresses ≤ Limit Register 500 yes + ! How can we improve memory management? Ø Paging Base 1000 Register base limit s STBR 0! Segment Table 21 22
© Copyright 2026 Paperzz