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