Chapter 17 Free-Space Management

Slides:



Advertisements
Similar presentations
Dynamic Memory Management
Advertisements

Chapter 6: Memory Management
1 Optimizing Malloc and Free Professor Jennifer Rexford COS 217 Reading: Section 8.7 in K&R book
Fixed/Variable Partitioning
Memory Management Chapter 7. Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated efficiently to pack as.
7. Physical Memory 7.1 Preparing a Program for Execution
Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable supply of ready processes to.
Chapter 7 Memory Management
Chapter 7 Memory Management Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic, N.Z. ©2009, Prentice.
OS Fall’02 Memory Management Operating Systems Fall 2002.
Memory Management Chapter 7. Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated efficiently to pack as.
1 Optimizing Malloc and Free Professor Jennifer Rexford
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
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 Chapter 5.
1 Inner Workings of Malloc and Free Professor Jennifer Rexford COS 217.
Memory Allocation CS Introduction to Operating Systems.
The memory allocation problem Define the memory allocation problem Memory organization and memory allocation schemes.
Operating System Chapter 7. Memory Management Lynn Choi School of Electrical Engineering.
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.
Memory Management Chapter 7.
1 Address Translation Memory Allocation –Linked lists –Bit maps Options for managing memory –Base and Bound –Segmentation –Paging Paged page tables Inverted.
Operating Systems CMPSC 473 Virtual Memory Management (4) November – Lecture 22 Instructor: Bhuvan Urgaonkar.
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
Chapter 17 Free-Space Management Chien-Chung Shen CIS, UD
CS 241 Discussion Section (11/17/2011). Outline Review of MP7 MP8 Overview Simple Code Examples (Bad before the Good) Theory behind MP8.
CS 241 Section Week #9 (11/05/09). Topics MP6 Overview Memory Management Virtual Memory Page Tables.
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
Memory Management -Memory allocation -Garbage collection.
Informationsteknologi Wednesday, October 3, 2007Computer Systems/Operating Systems - Class 121 Today’s class Memory management Virtual memory.
CS 241 Discussion Section (12/1/2011). Tradeoffs When do you: – Expand Increase total memory usage – Split Make smaller chunks (avoid internal fragmentation)
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.
2010INT Operating Systems, School of Information Technology, Griffith University – Gold Coast Copyright © William Stallings /2 Memory Management.
Main Memory CSSE 332 Operating Systems Rose-Hulman Institute of Technology.
CompSci 143A1 Part II: Memory Management Chapter 7: Physical Memory Chapter 8: Virtual Memory Chapter 9: Sharing Data and Code in Main Memory Spring, 2013.
Memory Management One of the most important OS jobs.
Memory Management Chapter 7.
Chapter 7 Memory Management
Memory Management Chapter 7.
Memory Management By: Piyush Agarwal ( ) Akashdeep ( )
Requirements, Partitioning, paging, and segmentation
Day 19 Memory Management.
Day 19 Memory Management.
Memory Allocation The main memory must accommodate both:
Dynamic Domain Allocation
Dynamic Memory Allocation
Chien-Chung Shen CIS/UD
PA1 is out Best by Feb , 10:00 pm Enjoy early
Main Memory Management
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
Chapter3 Memory Management Techniques
Main Memory Background Swapping Contiguous Allocation Paging
CS399 New Beginnings Jonathan Walpole.
Memory Blocks in a Buddy System - Buddy Systems (2)
Lecture 3: Main Memory.
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.
Dynamic Memory Allocation
Page Main Memory.
Presentation transcript:

Chapter 17 Free-Space Management Chien-Chung Shen CIS/UD cshen@udel.edu

Problem The problem of managing free space exists in both malloc library and OS itself Easy for fixed-sized units (e.g., paging) vs. hard for variable-sized units The problem: external fragmentation free space gets chopped into little pieces of different sizes and is thus fragmented subsequent requests (e.g., 15) may fail because there is no single contiguous space that can satisfy the request, even though the total amount of free space exceeds the size of the request minimize fragmentation

User-level Memory Allocation User-level memory allocation library with malloc() and free() user, when freeing the space, does not inform the library of its size; thus, the library must be able to figure out how big a chunk of memory is when handed just a pointer to it Heap Free list data structure used to manage free space in heap contains references to all of the free chunks of space in the managed region of memory

Low-level Mechanisms Splitting and coalescing Tracking the size of allocated regions Embedding free list inside the free space to keep track of what is free and what isn’t Growing the heap (via system call sbrk)

Splitting and Coalescing A (conceptual) free list contains a set of elements that describe the free space still remaining in the heap Request 1 byte splitting Free 10 bytes coalescing if the newly-freed space sits right next to one (or two, as in this example) existing free chunks, merge them into a single larger free chunk

Tracking Size of Allocated Regions How does free(void *ptr) know how how many bytes to free? typedef struct __header_t { int size; int magic; // integrity check } header_t; void free(void *ptr) { header_t *hptr = (void *)ptr - sizeof(header_t); ... } hptr->size + sizeof(header_t) ptr = malloc(20); when a user requests N bytes of memory, the library searches for a free chunk of size N plus the size of the header

Embedding A Free List (1) Build free list inside free space itself typedef struct __node_t { int size; struct __node_t *next; } node_t; // mmap() initializse 4KB of heap (free) space node_t *head = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0); head->size = 4096 – sizeof(node_t); head->next = NULL; Assuming 4096 bytes of free space

Embedding A Free List (2) Request 100 bytes find a chunk large enough split the chunk 16 * 1024 = 16384 16384 + 108 = 16492 (3980 = 4096 – 8 – 108)

Embedding A Free List (3) 3 allocated regions free(sptr) sptr is 16500 add free chunk back to free list by inserting at the head 16384 16384 + (108 * 3) = 16708

Embedding A Free List (4) Free two chunks How many chunks? 4 non-coalesced How can you do better? go through the list and coalesce (merge) neighboring chunks

Growing Heap When running out of heap, memory allocation library makes system call (e.g., sbrk()) to grow the heap To serve sbrk(), OS finds free physical pages maps them into the address space of the requesting process returns the value of the end of the new heap

Allocation Strategies Best fit - search through free list and return the smallest block of free memory that is bigger than the requested size Worst fit – opposite of best fit First fit – find the first block that is big enough and return the requested amount Next fit – not begin the first-fit search at the beginning of the list Their pros and cons

Buddy System Comprised of fixed and dynamic partitioning schemes Space available for allocation is treated as a single block Memory blocks are available of size 2K words, L ≤ K ≤ U, where 2L = smallest size block that is allocated 2U = largest size block that is allocated; generally 2U is the size of the entire memory available for allocation Both fixed and dynamic partitioning schemes have drawbacks. A fixed partitioning scheme limits the number of active processes and may use space inefficiently if there is a poor match between available partition sizes and process sizes. A dynamic partitioning scheme is more complex to maintain and includes the overhead of compaction. An interesting compromise is the buddy system ([KNUT97], [PETE77]). Memory blocks are available of size 2K words, L ≤ K ≤ U, where 2L = smallest size block that is allocated 2U = largest size block that is allocated; generally 2U is the size of the entire memory available for allocation To begin, the entire space available for allocation is treated as a single block of size 2U . If a request of size s such that 2U –1 < s 2U is made, then the entire block is allocated. Otherwise, the block is split into two equal buddies of size 2U –1 . If 2U –2 < s 2U –1 , then the request is allocated to one of the two buddies. Otherwise, one of the buddies is split in half again. This process continues until the smallest block greater than or equal to s is generated and allocated to the request. At any time, the buddy system maintains a list of holes (unallocated blocks) of each size 2 i . A hole may be removed from the ( i + 1) list by splitting it in half to create two buddies of size 2 i in the i list. Whenever a pair of buddies on the i list both become unallocated, they are removed from that list and coalesced into a single block on the ( i + 1) list.

Buddy Allocation search for free space recursively divides free space by two until a block that is big enough to accommodate the request is found (William Stallings: OS)

Buddy Allocation (William Stallings: OS)

Segregated Lists If a particular application has one (or a few) popular-sized request that it makes, keep a separate list just to manage objects of that size; all other requests are forwarded to a more general memory allocator benefit: fragmentation is much less of a concern; allocation and free requests can be served quite quickly Slab allocator