Memory Management Basics 1
Basic Memory Management Concepts Address spaces MAX sys Physical address space — The address space supported by the hardware Ø Starting at address 0, going to address MAX sys MAX prog Logical/virtual address space — A process ’ s Program view of its own memory P Ø Starting at address 0, going to address MAX prog 0 But where do addresses come from? MOV r0, @0xfffa620e 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 Library Routines Routines 0 100 1100 prog P P : : : : : : push ... : : push ... : : : inc SP, 4 foo() inc SP, x jmp 175 jmp 1175 jmp 75 : jmp _foo : : : : : ... ... ... 75 175 1175 end P foo: ... Compilation Assembly Linking 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 MAX sys MEMORY EXCEPTION Logical Physical no Program 1500 Addresses Addresses P ’ s CPU ≤ + yes physical � address � space 1000 500 1000 Instructions MAX prog Limit Base Program Register Register P ’ s logical � address � space 0 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 MAX Ø Unused memory between units of allocation Ø E.g, two fixed tables for 2, but a party of 4 Internal fragmentation Ø Unused memory within a unit of allocation Program Ø E.g., a party of 3 at Q ’ s a table for 4 PAS Program Code � ( “ text ” ) Execution Stack Data Execution Stack Program R ’ s PAS 0 8
Simple Memory Management Schemes Dynamic allocation of partitions MAX Program Simple approach: P 1 Ø Allocate a partition when a process is admitted into the system Ø Allocate a contiguous memory partition to the process Program P 2 OS keeps track of... Full-blocks P 5 Empty-blocks ( “ holes ” ) Program Allocation strategies First-fit P 3 Best-fit Worst-fit Program P 4 0 9
First Fit Allocation To allocate n bytes, use the first available free block such 1K bytes that the block size is larger than n. 2K bytes 2K bytes To allocate 400 bytes, we use the 1st free block 500 bytes 500 bytes available 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 Disadvantages Simple Slow allocation ◆ ◆ Tends to produce larger External fragmentation ◆ ◆ free blocks toward the end of the address space 11
Best Fit Allocation To allocate n bytes, use the smallest available free block 1K bytes 1K bytes such that the block size is larger than n. 2K bytes 2K bytes To allocate 400 bytes, we use the 3rd free block 500 bytes available (smallest) 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 External fragmentation Works well when most ◆ ◆ allocations are of small size Slow de-allocation ◆ Relatively simple 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 1K bytes 1K bytes such that the block size is larger than n. 2K bytes To allocate 400 bytes, we use the 2nd free block 500 bytes available (largest) 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 Disadvantages Works best if allocations Slow de-allocation ◆ ◆ are of medium sizes 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 MAX Program Compaction P 1 Ø Relocate programs to coalesce holes Swapping Program Ø Preempt processes & reclaim their memory P 2 Program Ready Running P 3 ready ? Waiting queue Program Suspended P 4 suspended 0 queue semaphore/condition queues 17
Memory Management Sharing Between Processes 2 n -1 Heap Schemes so far have considered only a single address space per process Ø A single name space per process Run-Time Program � Ø No sharing P ’ s Stack VAS Program How can one share code and data between Data programs without paging? Program Text 0 Program P ’ s 18 VAS
Multiple Name Spaces Example — Protection/Fault isolation & sharing 2 n -1 2 n 4 -1 Heap Heap 2 n 3 -1 0 Run-Time Run-Time Stack Stack 2 n 2 -1 0 2 n 6 -1 Program Program Data Data Libraries 0 2 n 1 -1 0 Program Program 2 n 5 -1 Text Text User 0 0 Code 0 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 know size of object, so 32 bits for offset? n 0 n 2 0 n 1 0 s addr s addr Segment + Address register scheme Single address scheme 20
Implementing Segmentation Base + Limit register scheme Physical Memory Add a segment table containing base & Program limit register values P CPU 1500 MEMORY Program EXCEPTION s o P ’ s Segment no n 0 32 0 1000 ≤ + yes Logical Addresses Limit Base 500 1000 Register Register base 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
Recommend
More recommend