1 / 72

Memory Management

Memory Management. File Manager. Process, Thread & Resource Manager. Memory Manager. Device Manager. Processor(s). Main Memory. Devices. Basic OS Organization. Operating System. Computer Hardware. Less Frequently Used Information. More Frequently Used Information.

cpacheco
Download Presentation

Memory Management

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Memory Management

  2. File Manager Process, Thread & Resource Manager Memory Manager Device Manager Processor(s) Main Memory Devices Basic OS Organization Operating System Computer Hardware

  3. Less Frequently Used Information More Frequently Used Information Storage Hierarchies in the Office

  4. The Basic Memory Hierarchy CPU Registers Less Frequently Used Information Primary Memory (Executable Memory) e.g. RAM More Frequently Used Information Secondary Memory e.g. Disk or Tape von Neumann architecture

  5. Memory System • Primary memory • Holds programs and data while they are being used by the CPU • Referenced by byte; fast access; volatile • Secondary memory • Collection of storage devices • Referenced by block; slow access; nonvolatile

  6. Load Primary & Secondary Memory CPU • CPU can load/store • Ctl Unit executes code from this memory • Transient storage Primary Memory (Executable Memory) e.g. RAM Secondary Memory e.g. Disk or Tape • Access using I/O operations • Persistent storage Information can be loaded statically or dynamically

  7. Classical Memory Manager Tasks • Memory management technology has evolved • Early multiprogramming systems • Resource manager for space-multiplexed primary memory • As popularity of multiprogramming grew • Provide robust isolation mechanisms • Still later • Provide mechanisms for shared memory

  8. Memory Hierarchies – Dynamic Loading CPU Registers Primary (Executable) L1 Cache Memory L2 Cache Memory “Main” Memory Larger storage/lower cost Rotating Magnetic Memory Faster access/higher cost Optical Memory Secondary Sequentially Accessed Memory

  9. Exploiting the Hierarchy • Upward moves are (usually) copy operations • Require allocation in upper memory • Image exists in both higher & lower memories • Updates are first applied to upper memory • Downward move is (usually) destructive • Destroy image in upper memory • Update image in lower memory • Place frequently-used info high, infrequently-used info low in the hierarchy • Reconfigure as process changes phases

  10. Contemporary Memory Manager • Performs the classic functions required to manage primary memory • Attempts to efficiently use primary memory • Keep programs/data in primary memory only while they are being used by CPU • Store/restore data in secondary memory soon after it has been used or created • Exploits storage hierarchies • Virtual memory manager

  11. Requirements on Memory Designs • The primary memory access time must be as small as possible • The perceived primary memory must be as large as possible • The memory system must be cost effective

  12. Functions of Memory Manager • Allocate primary memory space to processes • Map the process address space into the allocated portion of the primary memory • Minimize access times using a cost-effective amount of primary memory • May use static or dynamic techniques

  13. Application Program VirtualAlloc() exec() VMQuery() shmalloc() VirtualFree() sbrk() VirtualLock() getrlimit() ZeroMemory() File Mgr Device Mgr Memory Mgr File Mgr Device Mgr Memory Mgr Process Mgr Process Mgr UNIX Windows Hardware External View of the Memory Manager

  14. Memory Manager • Only a small number of interface functions provided – usually calls to: • Request/release primary memory space • Load programs • Share blocks of memory • Provides following • Memory abstraction • Allocation/deallocation of memory • Memory isolation • Memory sharing

  15. Hardware Primary Memory Process Address Space Mapped to object other than memory Memory Abstraction • Process address space • Allows process to use an abstract set of addresses to reference physical primary memory

  16. Address Space • Program must be brought into memory and placed within a process for it to be executed • A program is a file on disk • CPU reads instructions from main memory and reads/writes data to main memory • Determined by the computer architecture • Address binding of instructions and data to memory addresses

  17. Library code Other objects Secondary memory Link Edit Primary memory Loader Process address space • Link time: Combine elements • Load time: • Allocate primary memory • Adjust addresses in address space (relocation) • Copy address space from secondary to primary memory Creating an Executable Program Source code C Reloc Object code • Compile time: Translate elements

  18. Bindings • Compiler • Binds static variables to storage locations relative to start of data segment • Binds automatic variables to storage locations relative to bottom of stack • Linker • Combines data segments and adjusts bindings accordingly • Same for stack

  19. Bindings – cont. • Loader • Binds logical addresses used by program with physical memory locations (address binding) • This type of binding is called static address binding • The last stage of address binding can be deferred to runtime  dynamic address binding

  20. A Sample Code Segment ... static int gVar; ... int proc_a(int arg){ ... gVar = 7; put_record(gVar); ... }

  21. A Sample Code Segment ... static int gVar; ... int proc_a(int arg){ ... gVar = 7; put_record(gVar); ... } • Compiler allocates space for gVar in data segment, saving address in symbol table (binds variable name) • Assignment statement is translated into instructions storing 7 in that location • For function call, the parameters are pushed on the stack, but function is external so compiler leaves information so linker can resolve the address

  22. The Relocatable Object module Code Segment RelativeAddress Generated Code 0000 ... ... 0008 entry proc_a ... 0220 load =7, R1 0224 store R1, 0036 0228 push 0036 0232 call ‘put_record’ ... 0400 External reference table ... 0404 ‘put_record’ 0232 ... 0500 External definition table ... 0540 ‘proc_a’ 0008 ... 0600 (symbol table) ... 0799 (last location in the code segment) Data Segment RelativeAddress Generated variable space ... 0036 [Space for gVar variable] ... 0049 (last location in the data segment) • Linker combines this code • with other modules • Causes relative addresses • to be adjusted • Fixes up the external • reference to function Generated object code

  23. The Absolute Program Code Segment RelativeAddress Generated Code 0000 (Other modules) ... 1008 entry proc_a ... 1220 load =7, R1 1224 store R1, 0136 1228 push 1036 1232 call 2334 ... 1399 (End of proc_a) ... (Other modules) 2334 entry put_record ... 2670 (optional symbol table) ... 2999 (last location in the code segment) Data Segment RelativeAddress Generated variable space ... 0136 [Space for gVar variable] ... 1000 (last location in the data segment) • Loader brings program • into memory • Need to relocate • New address bindings

  24. The Program Loaded at Location 4000 Relative Address Generated Code 0000 (Other process’s programs) 4000 (Other modules) ... 5008 entry proc_a ... 5036 [Space for gVar variable] ... 5220 load =7, R1 5224 store R1, 7136 5228 push 5036 5232 call 6334 ... 5399 (End of proc_a) ... (Other modules) 6334 entry put_record ... 6670 (optional symbol table) ... 6999 (last location in the code segment) 7000 (first location in the data segment) ... 7136 [Space for gVar variable] ... 8000 (Other process’s programs)

  25. Dynamic Memory • Static and automatic variables are assigned addresses in the data or stack segments at compile time • Dynamic memory allocation (e.g., new or malloc) is done at runtime • This is not handled by the memory manager • This merely binds parts of the process’s address space to dynamic data structures • Memory manager gets involved if the process runs out of address space

  26. Variations in program linking/loading

  27. Normal linking and loading

  28. Load-time dynamic linking

  29. Run-time dynamic linking

  30. Data Storage Allocation • Static variables • stored in programs data segment • Automatic variables • Stored on stack • Dynamically allocated space (new or malloc) • Taken from heap storage – no system call • Note: If heap disappears, kernel memory manager invoked to get more memory for the process

  31. C Style Memory Layout Low Address Text Segment Initialized Part Data Segment Uninitialized Part Data Segment Heap Storage Stack Segment Environment Variables, … High Address

  32. Program and Process Address Spaces Process Address Space Hardware Primary Memory Absolute Program Address Space 0 User Process Address Space 3 GB Supervisor Process Address Space 4 GB

  33. Overview of Memory Management Techniques • Memory allocation strategies • View the process address space and the primary memory as contiguous address space • Paging and segmentation based techniques • View the process address space and the primary memory as a set of pages / segments • Map an address in process space to a memory address • Virtual memory • Extension of paging/segmentation based techniques • To run a program, only the current pages/segments need to in primary memory

  34. Memory Allocation Strategies - There are two different levels in memory allocation

  35. Two levels of memory management

  36. Memory Management System Calls • In Unix, the system call is brk • Increase the amount of memory allocated to a process

  37. Malloc and New functions • They are user-level memory allocation functions, not system calls

  38. Memory Management

  39. Issues in a memory allocation algorithm • Memory layout / organization • how to divide the memory into blocks for allocation? • Fixed partition method: divide the memory once before any bytes are allocated. • Variable partition method: divide it up as you are allocating the memory. • Memory allocation • select which piece of memory to allocate to a request • Memory organization and memory allocation are close related • It is a very general problem • Variations of this problem occurs in many places. • For examples: disk space management

  40. Static Memory Allocation Operating System Unused In Use Process 3 Process 0 pi Process 2 Issue: Need a mechanism/policy for loading pi’s address space into primary memory Process 1

  41. Fixed-Partition Memory allocation • Statically divide the primary memory into fixed size regions • Regions can have different sizes or same sizes • A process / request can be allocated to any region that is large enough

  42. Fixed-Partition Memory allocation– cont. • Advantages • easy to implement. • Good when the sizes for memory requests are known. • Disadvantage: • cannot handle variable-size requests effectively. • Might need to use a large block to satisfy a request for small size. • Internal fragmentation – The difference between the request and the allocated region size; Space allocated to a process but is not used • It can be significant if the requests vary in size considerably

  43. Fixed-Partition Memory Mechanism Operating System pi needs ni units Region 0 N0 pi ni Region 1 N1 N2 Region 2 Region 3 N3

  44. Which free block to allocate • How to satisfy a request of size n from a list of free blocks • First-fit: Allocate the first hole that is big enough • Next-fit: Choose the next block that is large enough • Best-fit: Allocate the smallest hole that is big enough; must search entire list, unless ordered by size. Produces the smallest leftover hole. • Worst-fit: Allocate the largest hole; must also search entire list. Produces the largest leftover hole.

  45. Fixed-Partition Memory -- Best-Fit Operating System • Loader must adjust every address in the absolute module when placed in memory Region 0 N0 Region 1 N1 Internal Fragmentation pi N2 Region 2 Region 3 N3

  46. Fixed-Partition Memory -- Worst-Fit Operating System pi Region 0 N0 Region 1 N1 N2 Region 2 Region 3 N3

  47. Fixed-Partition Memory -- First-Fit Operating System pi Region 0 N0 Region 1 N1 N2 Region 2 Region 3 N3

  48. Fixed-Partition Memory -- Next-Fit Operating System Region 0 N0 pi Region 1 N1 Pi+1 N2 Region 2 Region 3 N3

  49. Variable partition memory allocation • Grant only the size requested • Example: • total 512 bytes: • allocate(r1, 100), allocate(r2, 200), allocate(r3, 200), • free(r2), • allocate(r4, 10), • free(r1), • allocate(r5, 200) • External Fragmentation • Memory is divided up into small blocks that none of them can be used to satisfy any requests.

  50. Issues in Variable partition memory allocation • Where are the free memory blocks? • Keeping trace of the memory blocks • List method and bitmap method • Which memory blocks to allocate? • There may exist multiple free memory blocks that can satisfy a request. Which block to use? • Fragmentation must be minimized • How to keep track of free and allocated memory blocks?

More Related