Chapter 4: Memory Management Part 1: Mechanisms for Managing Memory
Memory management � Basic memory management � Swapping � Virtual memory � Page replacement algorithms � Modeling page replacement algorithms � Design issues for paging systems � Implementation issues � Segmentation Chapter 4 CMPS 111, UC Santa Cruz 2
In an ideal world… � The ideal world has memory that is � Very large � Very fast � Non-volatile (doesn’t go away when power is turned off) � The real world has memory that is: � Very large � Very fast � Affordable! ⇒ Pick any two… � Memory management goal: make the real world look as much like the ideal world as possible Chapter 4 CMPS 111, UC Santa Cruz 3
Memory hierarchy � What is the memory hierarchy ? � Different levels of memory � Some are small & fast � Others are large & slow � What levels are usually included? � Cache: small amount of fast, expensive memory � L1 (level 1) cache: usually on the CPU chip � L2 & L3 cache: off-chip, made of SRAM � Main memory: medium-speed, medium price memory (DRAM) � Disk: many gigabytes of slow, cheap, non-volatile storage � Memory manager handles the memory hierarchy Chapter 4 CMPS 111, UC Santa Cruz 4
Basic memory management � Components include � Operating system (perhaps with device drivers) � Single process � Goal: lay these out in memory � Memory protection may not be an issue (only one program) � Flexibility may still be useful (allow OS changes, etc.) � No swapping or paging 0xFFFF 0xFFFF Device drivers Operating system (ROM) User program (ROM) (RAM) User program (RAM) User program Operating system (RAM) Operating system (RAM) (RAM) 0 0 Chapter 4 CMPS 111, UC Santa Cruz 5
Fixed partitions: multiple programs � Fixed memory partitions � Divide memory into fixed spaces � Assign a process to a space when it’s free � Mechanisms � Separate input queues for each partition � Single input queue: better ability to optimize CPU usage 900K 900K Partition 4 Partition 4 700K 700K Partition 3 Partition 3 600K 600K Partition 2 Partition 2 500K 500K Partition 1 Partition 1 100K 100K OS OS 0 0 Chapter 4 CMPS 111, UC Santa Cruz 6
How many programs is enough? � Several memory partitions (fixed or variable size) � Lots of processes wanting to use the CPU � Tradeoff � More processes utilize the CPU better � Fewer processes use less memory (cheaper!) � How many processes do we need to keep the CPU fully utilized? � This will help determine how much memory we need � Is this still relevant with memory costing $150/GB? Chapter 4 CMPS 111, UC Santa Cruz 7
Modeling multiprogramming � More I/O wait means less processor utilization � At 20% I/O wait, 3–4 processes fully utilize CPU � At 80% I/O wait, even 10 processes aren’t enough � This means that the OS should have more processes if they’re I/O bound � More processes => memory management & protection more important! Chapter 4 CMPS 111, UC Santa Cruz 8
Multiprogrammed system performance � Arrival and work requirements of 4 jobs � CPU utilization for 1–4 jobs with 80% I/O wait � Sequence of events as jobs arrive and finish � Numbers show amount of CPU time jobs get in each interval � More processes => better utilization, less time per process Job Arrival CPU time needed 1 2 3 4 1 10:00 4 CPU idle 0.80 0.64 0.51 0.41 2 10:10 3 CPU busy 0.20 0.36 0.49 0.59 3 10:15 2 CPU/process 0.20 0.18 0.16 0.15 4 10:20 2 0 Time 10 15 20 22 27.6 28.2 31.7 1 2 3 4 Chapter 4 CMPS 111, UC Santa Cruz 9
Memory and multiprogramming � Memory needs two things for multiprogramming � Relocation � Protection � The OS cannot be certain where a program will be loaded in memory � Variables and procedures can’t use absolute locations in memory � Several ways to guarantee this � The OS must keep processes’ memory separate � Protect a process from other processes reading or modifying its own memory � Protect a process from modifying its own memory in undesirable ways (such as writing to program code) Chapter 4 CMPS 111, UC Santa Cruz 10
Base and limit registers 0xFFFF Special CPU registers: base & � 0x2000 limit Limit � Access to the registers limited to Process system mode partition � Registers contain � Base: start of the process’s Base memory partition 0x9000 � Limit: length of the process’s memory partition Address generation � � Physical address: location in OS actual memory 0 � Logical address: location from the process’s point of view � Physical address = base + logical Logical address: 0x1204 address Physical address: � Logical address larger than limit 0x1204+0x9000 = 0xa204 => error Chapter 4 CMPS 111, UC Santa Cruz 11
Swapping C C C C C B B B B A A A A D D D OS OS OS OS OS OS OS � Memory allocation changes as � Processes come into memory � Processes leave memory � Swapped to disk � Complete execution � Gray regions are unused memory Chapter 4 CMPS 111, UC Santa Cruz 12
Swapping: leaving room to grow � Need to allow for programs Stack to grow Room for � Allocate more memory for Process B to grow data B � Larger stack Data � Handled by allocating more Code space than is necessary at Stack the start Room for � Inefficient: wastes memory Process A to grow that’s not currently in use A Data � What if the process requests too much memory? Code OS Chapter 4 CMPS 111, UC Santa Cruz 13
Tracking memory usage: bitmaps Keep track of free / allocated memory regions with a bitmap � � One bit in map corresponds to a fixed-size region of memory � Bitmap is a constant size for a given amount of memory regardless of how much is allocated at a particular time Chunk size determines efficiency � � At 1 bit per 4KB chunk, we need just 256 bits (32 bytes) per MB of memory � For smaller chunks, we need more memory for the bitmap � Can be difficult to find large contiguous free areas in bitmap A B C D 8 16 24 32 Memory regions 11111100 00111000 01111111 Bitmap 11111000 Chapter 4 CMPS 111, UC Santa Cruz 14
Tracking memory usage: linked lists Keep track of free / allocated memory regions with a linked list � � Each entry in the list corresponds to a contiguous region of memory � Entry can indicate either allocated or free (and, optionally, owning process) � May have separate lists for free and allocated areas Efficient if chunks are large � � Fixed-size representation for each region � More regions => more space needed for free lists A B C D 8 16 24 32 Memory regions A 0 6 - 6 4 B 10 3 - 13 4 C 17 9 D 26 3 - 29 3 Chapter 4 CMPS 111, UC Santa Cruz 15
Allocating memory Search through region list to find a large enough space � Suppose there are several choices: which one to use? � � First fit: the first suitable hole on the list � Next fit: the first suitable after the previously allocated hole � Best fit: the smallest hole that is larger than the desired region (wastes least space?) � Worst fit: the largest available hole (leaves largest fragment) Option: maintain separate queues for different-size holes � Allocate 20 blocks first fit Allocate 13 blocks best fit Allocate 12 blocks next fit Allocate 15 blocks worst fit 1 5 18 - 6 5 - 19 14 - 52 25 - 102 30 - 135 16 - 202 10 - 302 20 - 350 30 - 411 19 - 510 3 15 Chapter 4 CMPS 111, UC Santa Cruz 16
Freeing memory � Allocation structures must be updated when memory is freed � Easy with bitmaps: just set the appropriate bits in the bitmap � Linked lists: modify adjacent elements as needed � Merge adjacent free regions into a single region � May involve merging two regions with the just-freed area A X B A B A X A X B B X Chapter 4 CMPS 111, UC Santa Cruz 17
Limitations of swapping � Problems with swapping � Process must fit into physical memory (impossible to run larger processes) � Memory becomes fragmented � External fragmentation: lots of small free areas � Compaction needed to reassemble larger free areas � Processes are either in memory or on disk: half and half doesn’t do any good � Overlays solved the first problem � Bring in pieces of the process over time (typically data) � Still doesn’t solve the problem of fragmentation or partially resident processes Chapter 4 CMPS 111, UC Santa Cruz 18
Virtual memory � Basic idea: allow the OS to hand out more memory than exists on the system � Keep recently used stuff in physical memory � Move less recently used stuff to disk � Keep all of this hidden from processes � Processes still see an address space from 0 – max address � Movement of information to and from disk handled by the OS without process help � Virtual memory (VM) especially helpful in multiprogrammed system � CPU schedules process B while process A waits for its memory to be retrieved from disk Chapter 4 CMPS 111, UC Santa Cruz 19
Recommend
More recommend