© 2004, D. J. Foreman 1 Memory Management
© 2004, D. J. Foreman 2 Building a Module -1 Compiler ■ generates references for function addresses may be inside current file may be external - in a separate compile ■ addresses are 'marked' in compiler output - the "object" file Linker or Link Editor ■ Resolves addresses (references) ■ Makes them relative to start of module ■ Saves relocatable module
© 2004, D. J. Foreman 3 Building a Module -2 Loader ■ Builds a table of addresses (ref table) References Definitions ■ replaces every relative address with sum of actual load address + relative address ■ gives control to the module ■ original relocatable file is still on disk This is called "static binding" of the logical address to the physical address
© 2004, D. J. Foreman 4 Building a Module -3 Dynamic memory - malloc, etc ■ Linker examines all calls to malloc ■ Adds up requests (as much as possible) ■ Adds space for stack data Loader assigns space for both ■ Heap space allocated from one end of block ■ Stack space allocated from other end ■ Grow toward middle Ask O/S for more, when space is gone
© 2004, D. J. Foreman 5 Strategies Fixed-size partition ■ Divide all RAM into N blocks ■ Program size must be < block size ■ Internal fragments exist - cannot be used Variable-size partition ■ Blocks defined as needed Must keep track of blocks External fragmentation occurs and gets worse – Smaller holes left, as processes fill old holes – Favors smaller and smaller processes Eventually must move processes
© 2004, D. J. Foreman 6 Managing fragments Move programs - compaction Allocation strategies ■ Best fit - search for block with closest size ■ Worst fit - find biggest block - leaves largest hole when program terminates ■ First fit - use first hole in list, from front ■ Next fit - circular list, just use next entry in list Part of block points to next ■ Helps manage 'garbage collection' - the process of building big blocks from little ones
© 2004, D. J. Foreman 7 Current Strategies All use variable-sized partitions Most use fixed sized blocks within the partition - will be called pages Simplifies list mgmt Other considerations: ■ Program segments don't (usually) change size ■ If pgm seg changes size, pgm must be moved ■ Loader must re-bind - increases overhead
© 2004, D. J. Foreman 8 Dynamic Binding All addresses are relative to "0" Adjust all addresses as in static binding Repeat when moving program Can be done with h/w relocation register Simple relocation done easily h/w adjusts every memory reference Relocation value added as needed
© 2004, D. J. Foreman 9 Improvements Multiple relocation registers ■ Code segment register - during fetch cycle ■ Stack segment register - for stack instructions ■ Data segment register - during execute cycle Automatic relocation-register management ■ Compiler generates code to update register when references cross segment boundary ■ No auto-help for Assembler programs Programs with data segments > register capability Programs that change segment registers
© 2004, D. J. Foreman 10 Making it work Trusted s/w for register manipulation ■ Privileged instructions ■ Compiler generates a segment trap ■ NOT a violation trap ■ O/S changes the relocation registers ■ O/S changes the Program Counter Also privileged ■ Program proceeds
© 2004, D. J. Foreman 11 Isolation Need to ensure access protection "Limit register" added ■ Contains length of segment ■ Compute: segment register + relative address ■ If relative address <= limit then proceed ■ Else segment fault (protection error)
© 2004, D. J. Foreman 12 Swapping Swapping removes the entire address space Cost analysis: ■ If a process has S memory space units ■ And a disk block holds D memory units ■ K=Ceil (S/D) disk writes required to save ■ And K reads to restore it later = 2*K overhead ■ If P i is blocked for T time, cost = T*S ■ T must be > X (assume 1 I/O = 1 Time unit)
© 2004, D. J. Foreman 13 Another strategy Swapping removes an entire process What if we only remove specific blocks? Must recognize spatial locality Sometimes called locality of reference And temporal locality We can remove/restore selected pages
© 2004, D. J. Foreman 14 Virtual Memory Mgmt Responsibilities: ■ Determine locality ■ Keep referenced pages in memory Problems: ■ Partitioning ■ Scatter-loading ■ Re-binding ■ Architecture ■ Construction ■ Actual
© 2004, D. J. Foreman 15 Shared Memory Multiprocessors need to share Increases bus load, reduces speed Speedup via cache, but new problems: ■ Stale data (coherency) ■ Expense Interconnection networks: ■ More speed ■ More expense Must detect changes everywhere Must force cache update synch