Lecture 7 Page 1 CS 111 Summer 2013 Dynamic Domain Allocation A concept covered in a previous lecture We’ll just review it here Domains are regions of.

Slides:



Advertisements
Similar presentations
Part IV: Memory Management
Advertisements

Dynamic Memory Management
Chapter 6: Memory Management
Note on malloc() and slab allocation CS-502 (EMC) Fall A Note on malloc() and Slab Allocation CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
1 Optimizing Malloc and Free Professor Jennifer Rexford COS 217 Reading: Section 8.7 in K&R book
Fixed/Variable Partitioning
CS 311 – Lecture 21 Outline Memory management in UNIX
OS Fall’02 Memory Management Operating Systems Fall 2002.
Memory Management Chapter 4. Memory hierarchy Programmers want a lot of fast, non- volatile memory But, here is what we have:
Memory Management Memory Areas and their use Memory Manager Tasks:
CS 104 Introduction to Computer Science and Graphics Problems
1 Optimizing Malloc and Free Professor Jennifer Rexford
Memory Management A memory manager should take care of allocating memory when needed by programs release memory that is no longer used to the heap. Memory.
Memory Management 1 CS502 Spring 2006 Memory Management CS-502 Spring 2006.
CS-3013 & CS-502, Summer 2006 Memory Management1 CS-3013 & CS-502 Summer 2006.
Memory Allocation CS Introduction to Operating Systems.
The memory allocation problem Define the memory allocation problem Memory organization and memory allocation schemes.
Real-Time Concepts for Embedded Systems Author: Qing Li with Caroline Yao ISBN: CMPBooks.
Dynamic Memory Allocation Questions answered in this lecture: When is a stack appropriate? When is a heap? What are best-fit, first-fit, worst-fit, and.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 32 Paging Read Ch. 9.4.
Review of Memory Management, Virtual Memory CS448.
Dynamic Partition Allocation Allocate memory depending on requirements Partitions adjust depending on memory size Requires relocatable code –Works best.
1. Memory Manager 2 Memory Management In an environment that supports dynamic memory allocation, the memory manager must keep a record of the usage of.
Lecture 7 Page 1 CS 111 Summer 2015 Memory Management CS 111 Operating System Principles Peter Reiher.
Chapter 4 Memory Management.
Subject: Operating System.
CE Operating Systems Lecture 14 Memory management.
1 Memory Management Basics. 2 Program P Basic Memory Management Concepts Address spaces Physical address space — The address space supported by the hardware.
1 Advanced Memory Management Techniques  static vs. dynamic kernel memory allocation  resource map allocation  power-of-two free list allocation  buddy.
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
Lecture 10 Page 1 CS 111 Online Another Option Fixed partition allocations result in internal fragmentation – Processes don’t use all of the fixed partition.
Lecture 10 Page 1 CS 111 Summer 2013 File Systems Control Structures A file is a named collection of information Primary roles of file system: – To store.
Lecture 7 Page 1 CS 111 Summer 2013 Another Option Fixed partition allocations result in internal fragmentation – Processes don’t use all of the fixed.
Informationsteknologi Wednesday, October 3, 2007Computer Systems/Operating Systems - Class 121 Today’s class Memory management Virtual memory.
CS6502 Operating Systems - Dr. J. Garrido Memory Management – Part 1 Class Will Start Momentarily… Lecture 8b CS6502 Operating Systems Dr. Jose M. Garrido.
Lecture 10 Page 1 CS 111 Spring 2015 Memory Management CS 111 Operating Systems Peter Reiher.
Lecture 10 Page 1 CS 111 Fall 2015 Memory Management CS 111 Operating Systems Peter Reiher.
CS 241 Discussion Section (12/1/2011). Tradeoffs When do you: – Expand Increase total memory usage – Split Make smaller chunks (avoid internal fragmentation)
COMP091 – Operating Systems 1 Memory Management. Memory Management Terms Physical address –Actual address as seen by memory unit Logical address –Address.
Memory Management Chapter 5 Advanced Operating System.
Lecture 10 Page 1 CS 111 Online Memory Management CS 111 On-Line MS Program Operating Systems Peter Reiher.
Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”
Lecture 12 Page 1 CS 111 Winter 2014 Memory Management CS 111 Operating Systems Peter Reiher.
Chapter 17 Free-Space Management
Outline What is memory management about? Memory management strategies:
Memory Management Memory Areas and their use Memory Manager Tasks:
Memory Allocation The main memory must accommodate both:
Dynamic Domain Allocation
Chapter 9 – Real Memory Organization and Management
Memory Management Memory Areas and their use Memory Manager Tasks:
Main Memory Management
Module IV Memory Organization.
Chapter 8: Main Memory.
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy 11/12/2018.
Optimizing Malloc and Free
CS Introduction to Operating Systems
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy Chapter 8 11/24/2018.
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy Chapter 9 12/1/2018.
So far… Text RO …. printf() RW link printf Linking, loading
Main Memory Background Swapping Contiguous Allocation Paging
CS399 New Beginnings Jonathan Walpole.
Chapter 8: Memory management
Outline Module 1 and 2 dealt with processes, scheduling and synchronization Next two modules will deal with memory and storage Processes require data to.
Optimizing Dynamic Memory Management
Operating System Chapter 7. Memory Management
Memory Management (1).
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy Chapter 9 4/5/2019.
Memory Management Memory Areas and their use Memory Manager Tasks:
Operating Systems: Internals and Design Principles, 6/E
Presentation transcript:

Lecture 7 Page 1 CS 111 Summer 2013 Dynamic Domain Allocation A concept covered in a previous lecture We’ll just review it here Domains are regions of memory made available to a process – Variable sized, usually any size requested – Each domain is contiguous in memory addresses – Domains have access permissions for the process – Potentially shared between processes Each process could have multiple domains – With different sizes and characteristics

Lecture 7 Page 2 CS 111 Summer 2013 Problems With Domains Not relocatable – Once a process has a domain, you can’t easily move its contents elsewhere Not easily expandable Impossible to support applications with larger address spaces than physical memory – Also can’t support several applications whose total needs are greater than physical memory Also subject to fragmentation

Lecture 7 Page 3 CS 111 Summer 2013 Relocation and Expansion Domains are tied to particular address ranges – At least during an execution Can’t just move the contents of a domain to another set of addresses – All the pointers in the contents will be wrong – And generally you don’t know which memory locations contain pointers Hard to expand because there may not be space “nearby”

Lecture 7 Page 4 CS 111 Summer 2013 The Expansion Problem Domains are allocated on request Processes may ask for new ones later But domains that have been given are fixed – Can’t be moved somewhere else in memory Memory management system might have allocated all the space after a given domain In which case, it can’t be expanded

Lecture 7 Page 5 CS 111 Summer 2013 Illustrating the Problem PAPA PBPB PCPC Now Process B wants to expand its domain size PBPB But if we do that, Process B steps on Process C’s memory We can’t move C’s domain out of the way And we can’t move B’s domain to a free area We’re stuck, and must deny an expansion request that we have enough memory to handle

Lecture 7 Page 6 CS 111 Summer 2013 Address Spaces Bigger Than Physical Memory If a process needs that much memory, how could you possibly support it? Two possibilities: 1.It’s not going to use all the memory it’s asked for, or at least not all simultaneously 2.Maybe we can use something other than physical memory to store some of it Domains are not friendly to either option

Lecture 7 Page 7 CS 111 Summer 2013 How To Keep Track of Variable Sized Domains? Start with one large “heap” of memory Maintain a free list – Systems data structure to keep track of pieces of unallocated memory When a process requests more memory: – Find a large enough chunk of memory – Carve off a piece of the requested size – Put the remainder back on a free list When a process frees memory – Put it back on the free list

Lecture 7 Page 8 CS 111 Summer 2013 Managing the Free List Fixed sized blocks are easy to track – A bit map indicating which blocks are free Variable chunks require more information – A linked list of descriptors, one per chunk – Each descriptor lists the size of the chunk and whether it is free – Each has a pointer to the next chunk on list – Descriptors often kept at front of each chunk Allocated memory may have descriptors too

Lecture 7 Page 9 CS 111 Summer 2013 The Free List head FREEFREE LENLEN NEXTNEXT USEDUSED LENLEN NEXTNEXT FREEFREE LENLEN NEXTNEXT USEDUSED LENLEN NEXTNEXT FREEFREE LENLEN NEXTNEXT … List might contain all memory fragments …or only fragments that are free

Lecture 7 Page 10 CS 111 Summer 2013 Free Chunk Carving USEDUSED LENLEN NEXTNEXT USEDUSED NEXTNEXT FREEFREE LENLEN NEXTNEXT … LENLEN NEXTNEXT FREEFREE LENLEN USEDUSED 1. Find a large enough free chunk 2. Reduce its len to requested size 3.Create a new header for residual chunk 4. Insert the new chunk into the list 5. Mark the carved piece as in use

Lecture 7 Page 11 CS 111 Summer 2013 Variable Domain and Fragmentation Variable sized domains not as subject to internal fragmentation – Unless requestor asked for more than he will use – Which is actually pretty common – But at least memory manager gave him no more than he requested Unlike fixed sized partitions, though, subject to another kind of fragmentation – External fragmentation

Lecture 7 Page 12 CS 111 Summer 2013 External Fragmentation PAPA PBPB PCPC PAPA PCPC PDPD PEPE PCPC PDPD PEPE PFPF We gradually build up small, unusable memory chunks scattered through memory

Lecture 7 Page 13 CS 111 Summer 2013 External Fragmentation: Causes and Effects Each allocation creates left-over chunks – Over time they become smaller and smaller The small left-over fragments are useless – They are too small to satisfy any request – A second form of fragmentation waste Solutions: – Try not to create tiny fragments – Try to recombine fragments into big chunks

Lecture 7 Page 14 CS 111 Summer 2013 How To Avoid Creating Small Fragments? Be smart about which free chunk of memory you use to satisfy a request But being smart costs time Some choices: – Best fit – Worst fit – First fit – Next fit

Lecture 7 Page 15 CS 111 Summer 2013 Best Fit Search for the “best fit” chunk – Smallest size greater than or equal to requested size Advantages: – Might find a perfect fit Disadvantages: – Have to search entire list every time – Quickly creates very small fragments

Lecture 7 Page 16 CS 111 Summer 2013 Worst Fit Search for the “worst fit” chunk – Largest size greater than or equal to requested size Advantages: – Tends to create very large fragments … for a while at least Disadvantages: – Still have to search entire list every time

Lecture 7 Page 17 CS 111 Summer 2013 First Fit Take first chunk you find that is big enough Advantages: – Very short searches – Creates random sized fragments Disadvantages: – The first chunks quickly fragment – Searches become longer – Ultimately it fragments as badly as best fit

Lecture 7 Page 18 CS 111 Summer 2013 Next Fit head FREEFREE LENLEN NEXTNEXT USEDUSED LENLEN NEXTNEXT FREEFREE LENLEN NEXTNEXT USEDUSED LENLEN NEXTNEXT FREEFREE LENLEN NEXTNEXT … After each search, set guess pointer to chunk after the one we chose. guess pointer That is the point at which we will begin our next search.

Lecture 7 Page 19 CS 111 Summer 2013 Next Fit Properties Some advantages of each approach – Short searches (maybe shorter than first fit) – Spreads out fragmentation (like worst fit) But more fragmentation than best fit Guess pointers are a general technique – Think of them as a lazy (non-coherent) cache – If they are right, they save a lot of time – If they are wrong, the algorithm still works – They can be used in a wide range of problems

Lecture 7 Page 20 CS 111 Summer 2013 Coalescing Domains All variable sized domain allocation algorithms have external fragmentation – Some get it faster, some spread it out We need a way to reassemble fragments – Check neighbors whenever a chunk is freed – Recombine free neighbors whenever possible – Free list can be designed to make this easier E.g., where are the neighbors of this chunk? Counters forces of external fragmentation

Lecture 7 Page 21 CS 111 Summer 2013 Free Chunk Coalescing head FREEFREE LENLEN NEXTNEXT USEDUSED LENLEN NEXTNEXT FREEFREE LENLEN NEXTNEXT USEDUSED LENLEN NEXTNEXT FREEFREE LENLEN NEXTNEXT … Previous chunk is free, so coalesce backwards. Next chunk is also free, so coalesce forwards. FREEFREE FREE

Lecture 7 Page 22 CS 111 Summer 2013 Fragmentation and Coalescing Opposing processes that operate in parallel – Which of the two processes will dominate? What fraction of space is typically allocated? – Coalescing works better with more free space How fast is allocated memory turned over? – Chunks held for long time cannot be coalesced How variable are requested chunk sizes? – High variability increases fragmentation rate How long will the program execute? – Fragmentation, like rust, gets worse with time

Lecture 7 Page 23 CS 111 Summer 2013 Coalescing and Free List Implementation To coalesce, we must know whether the previous and next chunks are also free If the neighbors are guaranteed to be in the free list, we can look at them and see if they are free If allocated chunks are not in the free list, we must look at the free chunks before and after us – And see if they are our contiguous neighbors – This suggests that the free list must be maintained in address order

Lecture 7 Page 24 CS 111 Summer 2013 Variable Sized Domain Summary Eliminates internal fragmentation – Each chunk is custom made for requestor Implementation is more expensive – Long searches of complex free lists – Carving and coalescing External fragmentation is inevitable – Coalescing can counteract the fragmentation Must we choose the lesser of two evils?