User Memory Management

User Memory Management

Memory management Types of information information stored in memory n Kernel management (next lecture) n By role in program: n User-process memory management u Program instructions u internal to process u Constants: F segments F pi, maxnum, strings used by printf/scanf F static allocation u Variables: F dynamic allocation F Locals, globals, function parameters, dynamic storage (from F allocation methods malloc or new) F reclaiming: garbage collection F Initialized or uninitialized u external n By protection status (important for sharing data and code): F stages in address binding u Readable and writable: variables F static relocation u Read-only: code, constants F dynamic relocation n Addresses vs. data: • swapping u Must modify addresses if program is moved (relocation, garbage • compaction collection) F paging (next lecture but one) 1 2 Segments Internal dynamic memory allocation n Static allocation is not sufficient for heaps and stacks n Process’ memory is divided into logical segments (text, data, bss, u Need dynamic storage — programmer may not know how heap, stack) much memory will be needed when program runs u Some are read-only, others read-write F Use malloc or new to get what’s necessary when it’s u Some are known at compile time, others grow dynamically as necessary program runs F For complex data structures (e.g., trees), allocate space n Who assigns memory to segments? for nodes on demand u Compiler and assembler generate an object file (containing u OS doesn’t know in advance which procedures will be called code and data segments) from each source file (would be wasteful to allocate space for every variable in u Linker combines all the object files for a program into a single every procedure in advance) executable object file, which is complete and self-sufficient u OS must be able to handle recursive procedures u Loader (part of OS) loads an executable object file into memory n Dynamic memory requires two fundamental operations: at location(s) determined by the operating system u Allocate dynamic storage u Program (as it runs) uses and to dynamically new malloc u Free memory when it’s no longer needed allocate memory, gets space on stack during function calls n Methods vary for stack and heap 3 4 Memory allocation methods Internal fragmentation n Stack (hierarchical) u Good when allocation and freeing are somewhat predictable n eventually end up with many small holes u Typically used: (fragments), each too small to be useful F to pass parameters to procedures u fragmentation, and it leads to wasted F for allocating space for local variables inside a procedure memory F for tree traversal, expression evaluation, parsing, etc. u Fragmentation wasn’t a problem with u Use stack operations: push and pop stack allocation, since we always add/delete from top of stack u Keeps all free space together in a structured organization u Solution goal: reuse the space in the u Simple and efficient, but restricted holes in such a way as to keep the n Heap number of holes small, and their size large u Used when allocation and freeing are not predictable n Compared to stack: more general, less u used for arbitrary list structures, complex data organizations, efficient, more difficult to implement etc. u Use new (malloc) to allocate space, use delete or free to release space u System memory consists of allocated areas and free areas 5 6 (holes) Heap memory allocation Reclaiming memory n Heap-based dynamic memory allocation techniques n typically maintain a free list, which keeps track of all the holes When can memory be freed? u Whenever programmer says to n Algorithms to manage the free list: u u Best fit Any way to do so automatically? F F Keep linked list of free blocks Difficult if that item is shared (i.e., if there are multiple pointers to it) F Search the whole list at each allocation n Potential problems in reclamation F Choose the hole that comes the closest to matching the request u is finished size; any unused space becomes a new (smaller) hole Dangling pointers — have to make sure that everyone using it F When freeing memory, combine adjacent holes u Memory leak — must not “lose” memory by forgetting to free it F Any way to do this efficiently? when appropriate u First fit n Implementing automatic reclamation: F Scan the list for the first hole that is large enough, choose that hole u Reference counts F Otherwise, same as best fit F Used by file systems u Worst fit F OS keeps track of number of outstanding pointers to each F scan for the largest hole (hoping that the remaining hole will be memory item large enough to be useful) F When count goes to zero, free the memory u Which is better? Why?? 7 8 Garbage collection Stages of address binding n u Garbage collection binding - assigning memory addresses to program objects (variables, pointers, etc.) F Used in LISP, Java n physical address - the “hardware” address F Storage isn’t explicitly freed by a free operation; programmer of a memory word (0xb0000) just deletes the pointers and doesn’t worry about what it’s n pointing at relocatable address - relative address (14 bytes from the beginning of this module) F When OS needs more storage space, it recursively searches n absolute code - all addresses are physical through all the active pointers and reclaims memory that no one is using n relocatable code - all addresses are relative F Makes life easier for application programmer, but is difficult to n linking - preparing compiled program to run program the garbage collector u static - all lib-functions included in the F Often expensive — may use 20% of CPU time in systems that code use it u dynamic - lib-functions can be hooked • May spend as much as 50% of time allocating and up at load-time automatically freeing memory n loading - a program into memory u static - all at once u 9 dynamic - on demand 10 Linking Why is linking difficult n Functions of a linker: n When assembler assembles a file, it may find external references u Combine all files and libraries of a program — symbols it doesn’t know about (e.g., printf, scanf) u Regroup all the segments from each file together (one big data u Compiler just puts in an address of 0 when producing the object segment, etc.) code u Adjust addresses to match regrouping u Compiler records external symbols and their location (in object file) in a cross-reference list, and stores that list in the object file u Result is an executable program u Linker must resolve those external references as it links the n Contents of object files: files together u File header — size and starting address (in memory) of each n segment Compiler doesn’t know where program will go in memory (if u multiprogramming, always 0 for uniprogramming) Segments for code and initialized data u u Compiler just assumes program starts at 0 Symbol table (symbols, addresses) u u Compiler records relocation information (location of addresses External symbols (symbols, location) to be adjusted later), and stores it in the object file u Relocation information (symbols, location) n for details type man ld u Debugging information u For UNIX details, type man a.out 11 12 Loading Goals in memory usage for multiprogramming n The loader loads the completed program into memory where it can be executed u Loads code and initialized data segments into memory at n Transparency: specified location u Multiple processes must coexist in memory u Leaves space for uninitialized data (bss) u No process should be aware that the memory is shared u Returns value of start address to operating system u Each process should execute regardless of where it is n Alternatives in loading located in memory u Absolute loader — loads executable file at fixed location n Safety: u Relocatable loader — loads the program at an arbitrary u Processes must not be able to corrupt each other, or the OS memory location specified by OS u Protection mechanisms are used to enforce safety u Assembler and linker provide relocatable addreses, loader n Efficiency: translates them to physical addresses u The performance of the CPU and memory should not F When program is loaded, loader modifies all addresses degrade very much as a result of sharing by adding the real start location to those addresses 13 14 Static external relocation Static external relocation (cont.) n Problems with static relocation: u Safety — not satisfied — one process can access / corrupt n Put the OS in the highest another’s memory, can even corrupt OS’s memory main main memory u Processes can not change size (why…?) address memory address memory 2400 2400 n Compiler and linker u Processes can not move after beginning to run (why would they OS OS assume each process want to?) 2200 2200 1900 starts at address 0 u Used by MS-DOS, Windows, Mac OS n B At load time, the OS: n An alternative: dynamic relocation u Allocates the process u The basic idea is to change each memory address dynamically as 1200 1200 a segment of memory the process runs in which it fits u This translation is done by hardware — between the CPU and the completely A A memory is a memory management unit (MMU) (also called a u Adjusts the translation unit ) that converts virtual addresses to physical addresses in the addresses 0 0 processes to reflect F This translation happens for every memory reference the its assigned location process makes in memory 15 16 Dynamic relocation Dynamic relocation (cont.) n There are now two different views of the address space: u The physical address space — seen only by the OS — is as large as there is physical memory on the machine u The virtual

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    4 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us