1 / 44

Chapter 11: Memory Management

Chapter 11: Memory Management. Prof. Steven A. Demurjian, Sr. † Computer Science & Engineering Department The University of Connecticut 191 Auditorium Road, Box U-155 Storrs, CT 06269-3155. steve@engr.uconn.edu http://www.engr.uconn.edu/~steve (860) 486 - 4818.

fell
Download Presentation

Chapter 11: 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. Chapter 11: Memory Management Prof. Steven A. Demurjian, Sr. † Computer Science & Engineering Department The University of Connecticut 191 Auditorium Road, Box U-155 Storrs, CT 06269-3155 steve@engr.uconn.edu http://www.engr.uconn.edu/~steve (860) 486 - 4818 † These slides have been modified from a set of originals by Dr. Gary Nutt.

  2. Purpose of this Chapter • What is Memory Management? • Allocating Primary Memory to Processes • Managing Sharing of Primary Memory • Minimizing Memory Access Time • What are Basic Issues in Memory Manager Design? • What are Classic Allocation Strategies? • Evolution of Memory Management to … • Swapping • Virtual Memory • Caching

  3. Memory Manager • Management of Primary and Secondary Memory • Automatic Movement Between Two • From Secondary to Primary to Start or Restart • From Primary to Secondary to Suspend • Capturing the “Executable” State to Secondary Memory for Ease of Restart • Strategies for Determining “When” Movement Will Occur • Involves Management of • Memory Hierarchy of Hardware • Creation of Address Space for “Process” • Mapping “Process” During Allocation

  4. Memory Manager • Requirements • Minimize Primary Memory Access Time • Registers vs. Cache vs. Primary Memory • Maximize Primary Memory Size • Virtual Memory “Appearance” of Larger Memory • Primary Memory Must Be Cost-Effective • Doubling of Capacity has Had Greatest Impact • Today’s Memory Manager: • Allocates Primary Memory to Processes • Maps Process Address Space to Primary Memory • Minimizes Access Time Using Cost-effective Memory Configuration

  5. Recall: OS Functionality and Processing Process Process Process Abstract Computing Environment Synchronization Process Description File Manager Protection Deadlock Process Manager Device Manager Memory Manager Resource Manager Resource Manager Scheduler Resource Manager Devices Memory CPU Other H/W

  6. Address Space vs. Primary Memory Primary Memory Process Address Space Mapped to object other than memory

  7. Building the Address Space Source Code Library code Other Objects Secondary Memory Translation Executable Memory Process Address Space Loader Process Address Space • Adjust Addresses • Translation Time • Load Time Compose Elements Allocate Executable Memory Space

  8. Memory Hierarchies (Outdated Capacities) CPU Registers Executable Memory Cache Memory Primary Memory Rotating Magnetic Memory Secondary Memory Optical Memory Sequentially Accessed Memory 500 bytes/1 clock cycle 10MB/1-2 clock cycle 250MB/1-4 clock cycle 10GB/5-50 micro s 15GB 25 micro sec to 1 second 5GB seconds

  9. Memory Hierarchies CPU Registers Executable Memory Cache Memory Primary Memory Rotating Magnetic Memory Secondary Memory Optical Memory Sequentially Accessed Memory

  10. Managing the Hierarchy • Move Across Executable-Secondary Memory Boundary (or Lower) Requires I/O Operation • Upward Moves are Copy Operations • Require Allocation in Upper Memory • Image Exists in Both Memories • Updates are First Applied to Upper Memory • Downward Move is (Usually) Destructive • Deallocate Upper Memory • Updates Image in Secondary Memory • Virtual Memory Automates Movement • Between Secondary and Primary • Primary and Cache

  11. Recall: Consistency/Address Management • Scheduling and Resource Management Requires • Ability to Start, Suspend, Restart Process • Decision Based on • Timesharing of CPU • Priorities of “Executing” Processes • Memory Manager May Need to … • Remove Executable from Primary Memory • Save Executable to Secondary Memory • De-allocate Primary Memory for Use by Another Process • Load and Active “New” Process • Repeat Above Steps to Timeshare

  12. Recall: Where are Consistency Problems? • Consider “Executing Process” • Currently Loaded in Primary Memory • Copy Located in Secondary Memory • What Happens When Local Variable in Executing Primary Memory Copy is Changed? • Is Secondary Storage Automatically Updated? • Who Does Update? Process or Memory Manager? • What Further Complicates this Problem?

  13. Another View of Memory Hierarchy R1 R2 Rn • Can “Same” Datum Have Inconsistent Values? • Modify Ri and Not M(P) • Modify Ri/MP and Not M(S) • What if Process is Swapped Out? • How Does Data Remain Consistent? Are Levels of MemoryHierarchy Missing? PC M(R) IR CACHE Primary Memory M(P) What is Direction of Access? Controller Buffers Secondary Memory M(S)

  14. Swapping and Memory Hierarchy R1 R2 Rn • Swapping: Movement of Process Between Primary/Secondary Mem • What Necessitates Swapping? • M(P) is Full • New Process Pn Starts • One or More Processes Must be Moved for Pn to Execute • Which Process(es) are Moved? • P(current) - Active Process • Quantum Just Finished • Swap Process/State to M(S) • Why is P(current) Swapped? • Longest Wait for Next Quantum • Assumes Equal Priorities M(R) PC IR CACHE Primary Memory M(P) Controller Buffers Secondary Memory M(S)

  15. Recall: Compile Time Process “.o” Files from Modules Across Prog. Languages Unix: C, Fortran, Ada, C++, ... “a.out” Can Combine “.o” Regardless of Origins Source Modules Translator Relocatable Modules Absolute Program Primary Memory Link Editor Executable Program Loader

  16. Unix Style Memory Layout for Process • Storage Organization for a Process • “Code” Referenced by PC • Global/Local Variables • Other Variables • Heap for Dynamic Memory Allocation • For Activation Records • Still Other Variables Text Segment Initialized Data Segment Un-initialized Data Segment Heap Storage Stack Segment Environment Variables, etc.

  17. Recall Key Compilation Concepts (see Section 11.1.2 for Full Details) • Compile: Produce Relocatable Object Module • Static Variables Allocated at Compile • Non-Static Variables via Run Time Stack • Dynamic Variables via Heap • Recall Activation Record in C (next slide) • Link Time: Produce Absolute (or Load) Module • Collect and Combine Individual Relocatable Modules • Linkage Editor: Internal Organization of Processes Address Space • Absolute Module at Location 0000 • All Addresses w.r.t. Base Location

  18. Recall: An Activation Record in C • Actual Parameters Supplied by Caller • Needed to Restart Caller • Local Variables for Scope • If Callee Calls Another Procedure/Function Etc. Incoming Param 2 Incoming Param 1 Saved State Info Local Variables Temporary Storage Outgoing Parameters

  19. Recall Key Compilation Concepts (see Section 11.1.2 for Full Details) • Loader: Produce Executable Image for Primary Memory • Readjust Addresses for Execution • Offset from Location 0000 Must be Changed • Once Partition for Process Execution has Been Chosen • Bind Addresses to Physical Memory Locations • Referencing Both Instructions and Data • Dynamic Memory for Data Structures • Each Address Space of Process Contains “Unused” Memory • User Programs Utilize for Dynamic Memory Allocation (e.g., malloc and new)

  20. From Code to Loaded Program ORIGINAL SOURCE static int gVar; . . . int proc_a (int arg){ . . . gVar = 7; put_record(gVar); . . . } RELOCATABLE OBJECT MODULE 0000 . . . . . . 0008 entry proc_a . . . 0036 [Space for gVar] . . . 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 (optional symbol table) 0799 (last location in module)

  21. From Code to Loaded Program THE ABSOLUTE PROGRAM 0000 (Other Modules) . . . 1008 entry proc_a . . . 1036 [Space for gVar] . . . 1220 load =7, R1 1224 store R1, 1036 1228 push 1036 1232 call 2334 1399 (end of proc_a) . . . (Other Modules) 2334 entry put_record . . . 2670 (opt. symbol table) 2999 (last loc. in module) PROGRAM AT LOCATION 4000 0000 (Other Processes) 4000 (Other Modules) . . . 5008 entry proc_a . . . 5036 [Space for gVar] . . . 5220 load =7, R1 5224 store R1, 5036 5228 push 5036 5232 call 6334 5399 (end of proc_a) . . . (Other Modules) 6334 entry put_record . . . 6670 (opt. symbol table) 6999 (last loc. in module) 7000 (Other Processes)

  22. Memory Allocation Operating System Process 3 Process 0 Process 2 Process 1 Unused In Use • Process that Transitions Absolute Program for Process to Allocated Primary Memory • Memory Manager Employs Space-Multiplexed Sharing • Figure at Right Employs Four-Way Multiprogramming • Divides Memory into at Most Four Blocks + OS Block • Results in Unused Space • Key Problem Regardless of Memory Allocation Strategy: • Fragmentation!

  23. Fixed vs. DynamicMemory Allocation • Fixed Memory Allocation • Divide Memory into N Fixed Regions • One Region for OS • Other Regions for User Processes • Memory Manager Must Know Maximum Memory Required by Each Process • Dynamic Memory Allocation • Also, N Fixed Regions • Process Can “Change” Memory Requirements • Ability to Dynamically Move Process to Another Region if Requirements Change • Both Techniques Seek to Minimize Fragmentation, Achieve Fairness, Manage Active Processes, etc.

  24. Fixed-Partition Memory Operating System pi needs ni units Region 0 N0 pi ni Region 1 N1 N2 Region 2 Region 3 N3 • N Fixed Sized Regions of Varying Sizes • Provide • Small Regions for Small Processes • Larger Regions for Larger Processes • Process Loaded into Any Empty Region with Enough Space • Region Larger than Process • Internal Fragmentation • Allocation Strategies Differ in Addressing Fragmentation

  25. Fixed-Partition Memory -- Best-Fit • Assume Each Region Maintains Queue of Processes Competing for Partition • “Large” Process Queue • “Small” Process Queue • “Medium” Process Queue • Etc… • Loader Prepares Process for Execution, Allocator … • Puts Process in Queue for Region • Minimizes Ni - Process Size Operating System Region 0 N0 Region 1 N1 pi N2 Region 2 Region 3 N3 Internal Fragmentation

  26. Fixed-Partition Memory -- Best-Fit • Defines the “Best Fit” Strategy • What are Potential Problems? • Popular Sized Queues Can Overfill • Lack of Fairness • Large, Medium “Popular” • Small Job Queue “Empty” • Later Small Jobs Execute Before Earlier Medium/Large • Not Suited to Timesharing Systems Operating System Region 0 N0 Region 1 N1 pi N2 Region 2 Region 3 N3 Internal Fragmentation

  27. Variable-Partition Memory Strategies Operating System Operating System Process 0 Process 1 Process 2 Process 3 Process 4 • OS Flexible in Placing Processes in Memory • Allocate Memory in Blocks (1K, 2K, etc.) to Minimize Internal Fragmentation • Live with External Fragmentation at “End” of Memory • Reallocate Memory of Finished Processes According to Various Strategies

  28. Snapshot of Variable Partition Memory Loader Must Adjust Every Address in Every Absolute Module When Placed in Memory Compaction Moves Program in Memory External Fragmentation Operating System Process 0 Operating System Operating System Operating System Process 6 Process 0 Process 0 Process 2 Process 1 Process 6 Process 2 Process 2 Process 5 Process 4 Process 5 Process 3 Process 4 Process 4

  29. Cost of Moving Programs load R1, 0x02010 3F013010 Program Originally Loaded at 0x01000 3F016010 Program Reloaded at 0x04000 • Must run Loader Over Program Again! • Recalculate Real Addresses from Absolute!

  30. Dynamic Memory Allocation • Common to Use Dynamically Allocated Memory • Process Wants to Change the Size of its Address Space • Smaller  Creates an External Fragment • Larger  Have to Move/Relocate the Program • Allocate Primary Memory “Holes” According to • Best Fit • Worst Fit • First Fit • Next Fit • What are Alternative Strategies? • How to they Differ?

  31. Dynamic Memory Allocation -- Best-Fit • Same as Fixed Approach • Allocator Places Process in Smallest Block of Available Memory • Suppose • 12K Process • 6K, 14K, 19K, 11K, 13K Blocks • Which Block would Allocator Choose? Operating System Region 0 N0 Region 1 N1 pi N2 Region 2 Region 3 N3 Internal Fragmentation

  32. Dynamic Memory -- Worst-Fit Operating System pi Region 0 N0 Region 1 N1 N2 Region 2 Region 3 N3 • Worst Fit: Choose Largest Memory Block Available for pi • Create Largest Hole Which Then Available for Allocation • Suppose • 12K Process • 6K, 14K, 19K, 11K, 13K Blocks • Which Block would Allocator Choose? • What is Result of Choice? • Split Region to Create New Region for Process to Execute

  33. Dynamic Memory -- First-Fit Operating System pi Region 0 N0 Region 1 N1 N2 Region 2 Region 3 N3 • First Fit: Avoid Traversal of Free List by Choosing First Available Block • Free List Could be Long in Practices • Reduce Search Choice • Suppose • 12K Process • 6K, 14K, 19K, 11K, 13K • Which Block would Allocator Choose? • Fragment Blocks at Front End of List - End of List Unused

  34. Dynamic Memory -- Next-Fit Operating System Region 0 N0 pi Region 1 N1 Pi+1 N2 Region 2 Region 3 N3 • Next Fit: Employ Circular Free List and Search from Current Position Forward • Add Fairness to Choice and Evenly Distribute Processes Across Memory • Suppose • 12K Process • List is 6K, 14K, 19K, 11K, 13K with Ptr Before 11K • Which Block would Allocator Choose?

  35. Fixed vs. DynamicMemory Allocation • Fixed Memory Allocation • What are Pros and Cons of Various Strategies? • Dynamic Memory Allocation • What are Pros and Cons of Various Strategies? • Under What Situations would Utilize Strategies? • Does Application Domain Influence Strategy? • What would Use for Embedded Automotive Example? • How would you Compare/Contrast Fixed vs. Dynamic Allocation?

  36. Swapping • Suppose There is High Demand for Executable Memory • Many Processes Need to Execute • Assume Time Sharing/Time Quantum • What if All Processes Won’t Fit Into M(P)? • Equitable Policy Might Be to Time-Multiplex Processes into the Space-Multiplexed Memory • Means that Process Can Have its Address Space Unloaded Even if it Still Needs Memory • Can Happen When it is Blocked (I.e.,Waiting for I/O to Finish) • Also - If User Idle on Unix, Could Swap out Netscape, Mail, Editor, etc.

  37. Swapping • Swapping Requires the Ability to Remove and then Restart Executable Program at Same Point • Remove Means, for Process Pi • Swap Pi from M(P) to M(S) • Allow Process Pi to be Idle • Prior to Pi’s Quantum, Swap Pi from M(S) to M(P) • Key: Keep CPU Busy - No Waiting for Either Swap! • What is Available to Assist in this Activity? CPU CACHE Primary Memory M(P) Controller Buffers Secondary Memory M(S)

  38. Dynamic Address Relocation • Don’t Map Entire Absolute Program to Real Addresses in Primary Memory • Calculate Addresses at Runtime as Needed • Referred to as Dynamic Relocation • Achievable in Hardware • Defer Relocation Until Runtime • Hardware Intercepts Addresses and Adds Relocation Value Prior to Sending to Memory • Hardware Relocation Register • This Approach Simplifies the Swapping of Executable Process by • Reducing Cost of Moving Programs • Easing Movement in/out of Memory

  39. Dynamic Address Relocation + CPU Relative Address Relocation Register load R1, 0x02010 MAR Primary Memory 3F012010 • Program loaded at 0x01000  Relocation Register = 0x01000 • Program loaded at 0x04000  Relocation Register = 0x04000

  40. Dynamic Address Relocation in Practice + MAR Primary Memory CPU Relative Address Code Register Stack Register Data Register

  41. Runtime Bound Checking + <  CPU Relative Address Relocation Register Limit Register MAR Interrupt

  42. Memory Manager Strategies • Fixed-Partition Used Only in Batch Systems • Variable-Partition Used Everywhere (Except in Virtual Memory) • Swapping Systems • Popularized in Timesharing • Relies on Dynamic Address Relocation • Now Dated • Virtual Memory • Paging -- Mainstream in Contemporary Systems • Segmentation -- the Future

  43. Virtual Memory: A First Look • Realization that Not all Process’s Executable Image Needed at All Times • Class Library with 500 Classes in 5 Hierarchies • At any Point, only Subset will be Used • Why Keep All Classes in Memory? • Locality of Access w.r.t. Data and Instructions • If 1240 Address then 1244, 1248, …, not 9000 • Spatial Reference Locality • Emergence when • Memory Costs High and Programs Large • Strategy to Allow Large Programs (256K) to Execute in Small Primary Memory (128K) • Why is it Still Relevant Today?

  44. Concluding Remarks/Looking Ahead • Basics & Issues of Memory Management • Role and Functions of Memory Manager • Memory Allocation Strategies • Swapping and Virtual Memory • Interesting Exercise 2 and 8 in Section 11.6 • Ex 2 - Impact of Strategy on Free-List • Ex 8 - Justification for Swapping of Process • Looking Ahead to … • Virtual Memory/Paging (Chapter 12) • File Management (Chapter 13) • NT/Java Projects on IPC, VM, etc.

More Related