1 Optimizing Malloc and Free Professor Jennifer Rexford

Slides:



Advertisements
Similar presentations
Dynamic Memory Management
Advertisements

Chapter 6: Memory Management
Chapter 12. Kernel Memory Allocation
Lecture 10: Heap Management CS 540 GMU Spring 2009.
File Systems.
Memory Management Chapter 7.
KERNEL MEMORY ALLOCATION Unix Internals, Uresh Vahalia Sowmya Ponugoti CMSC 691X.
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.
Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable supply of ready processes to.
Allocating Memory.
CPSC 388 – Compiler Design and Construction
Lab 3: Malloc Lab. “What do we need to do?”  Due 11/26  One more assignment after this one  Partnering  Non-Honors students may work with one other.
1 CSE 380 Computer Operating Systems Instructor: Insup Lee University of Pennsylvania, Fall 2002 Lecture Note: Memory Management.
Memory Management Chapter 4. Memory hierarchy Programmers want a lot of fast, non- volatile memory But, here is what we have:
Memory Management Chapter 7. Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated efficiently to pack as.
Memory Management Memory Areas and their use Memory Manager Tasks:
Memory Management Chapter 7 B.Ramamurthy. Memory Management Subdividing memory to accommodate multiple processes Memory needs to allocated efficiently.
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 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 Management Chapter 5.
Computer Organization and Architecture
Memory Management Five Requirements for Memory Management to satisfy: –Relocation Users generally don’t know where they will be placed in main memory May.
1 Inner Workings of Malloc and Free Professor Jennifer Rexford COS 217.
1 Dynamic Memory Management. 2 What’s worse?  50% of instructions/data are cache misses (fetched from RAM)  1% of instructions/data are page faults.
1 Lecture 8: Memory Mangement Operating System I Spring 2008.
Memory Allocation CS Introduction to Operating Systems.
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 Memory Management Memory Management COSC513 – Spring 2004 Student Name: Nan Qiao Student ID#: Professor: Dr. Morteza Anvari.
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.
Cosc 2150: Computer Organization Chapter 6, Part 2 Virtual Memory.
March 16 & 21, Csci 2111: Data and File Structures Week 9, Lectures 1 & 2 Indexed Sequential File Access and Prefix B+ Trees.
Subject: Operating System.
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
1 Advanced Memory Management Techniques  static vs. dynamic kernel memory allocation  resource map allocation  power-of-two free list allocation  buddy.
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.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Memory Management Overview.
Consider Starting with 160 k of memory do: Starting with 160 k of memory do: Allocate p1 (50 k) Allocate p1 (50 k) Allocate p2 (30 k) Allocate p2 (30 k)
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging.
Dynamic Memory Allocation II
Memory Management OS Fazal Rehman Shamil. swapping Swapping concept comes in terms of process scheduling. Swapping is basically implemented by Medium.
University of Washington Today More memory allocation!
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.
CSE 351 Dynamic Memory Allocation 1. Dynamic Memory Dynamic memory is memory that is “requested” at run- time Solves two fundamental dilemmas: How can.
Dynamic Memory Management Jennifer Rexford 1. 2 Goals of this Lecture Dynamic memory management techniques Garbage collection by the run-time system (Java)
Memory Management Chapter 5 Advanced Operating System.
Memory Management What if pgm mem > main mem ?. Memory Management What if pgm mem > main mem ? Overlays – program controlled.
Storage Management Different-sized Items. Light blue indicates allocated items Heap Memory with Different-sized Items.
Chapter 17 Free-Space Management
Section 10: Memory Allocation Topics
Memory Management Memory Areas and their use Memory Manager Tasks:
Dynamic Domain Allocation
Memory Management Memory Areas and their use Memory Manager Tasks:
PA1 is out Best by Feb , 10:00 pm Enjoy early
Optimizing Malloc and Free
CS Introduction to Operating Systems
Memory Management Overview
Optimizing Dynamic Memory Management
Memory Management (1).
Memory Management Memory Areas and their use Memory Manager Tasks:
Presentation transcript:

1 Optimizing Malloc and Free Professor Jennifer Rexford

2 Goals of This Lecture Brief review of K&R implementation  Circular linked list of free blocks, with pointer and size in header –Malloc: first-fit algorithm, with splitting –Free: coalescing with adjacent blocks, if they are free  Limitations –Fragmentation of memory due to first-fit strategy –Linear time to scan the list during malloc and free Optimizations related to assignment #6  Placement choice, splitting, and coalescing  Faster free –Size information in both header and footer –Next and previous free-list pointers in header and footer  Faster malloc –Separate free list for free blocks of different sizes –One bin per block size, or one bin for a range of sizes

3 Free Block: Pointer, Size, Data Free block in memory  Pointer to the next block  Size of the block  User data p (address returned to the user) user data size header

4 Free List: Circular Linked List Free blocks, linked together  Example: circular linked list Keep list in order of increasing addresses  Makes it easier to coalesce adjacent free blocks In use In use In use Free list

5 Malloc: First-Fit Algorithm Start at the beginning of the list Sequence through the list  Keep a pointer to the previous element Stop when reaching first block that is big enough  Patch up the list  Return a pointer to the user ppprevp

6 Malloc: First Case, A Perfect Fit Suppose the first fit is a perfect fit  Remove the block from the list  Link the previous free block with the next free block  Return the current to the user (skipping header) pprev p+1

7 Malloc: Second Case: Big Block Suppose the block is bigger than requested  Divide the free block into two blocks  Keep first (now smaller) block in the free list  Allocate the second block to the user pp

8 Free User passes a pointer to the memory block  void free(void *ap); Free function inserts block into the list  Identify the start of entry  Find the location in the free list  Add to the list, coalescing entries, if needed apbp

9 Free: Finding Location to Insert Start at the beginning Sequence through the list Stop at last entry before the to-be-freed element In use FREE ME In use Free list bpp

10 Free: Handling Corner Cases Check for wrap-around in memory  To-be-freed block is before first entry in the free list, or  To-be-freed block is after the last entry in the free list In use FREE ME In use Free list bpp

11 Free: Inserting Into Free List New element to add to free list Insert in between previous and next entries But, there may be opportunities to coalesce bp pp->s.ptr

12 Coalescing With Neighbors Scanning the list finds the location for inserting  Pointer to to-be-freed element: bp  Pointer to previous element in free list: p Coalescing into larger free blocks  Check if contiguous to upper and lower neighbors In use FREE ME In use Free list bpp lowerupper

13 Coalesce With Upper Neighbor Check if next part of memory is in the free list If so, make into one bigger block Else, simply point to the next free element bp upper pp->s.ptr p

14 Coalesce With Lower Neighbor Check if previous part of memory is in the free list If so, make into one bigger block bpp lower p->s.ptr p

15 Strengths of K&R Malloc and Free Advantages  Simplicity of the code Optimizations to malloc()  Splitting large free block to avoid wasting space Optimization to free()  Roving free-list pointer is left at the last place a block was allocated  Coalescing contiguous free blocks to reduce fragmentation p bp upper pp->s.ptr

16 Weaknesses of K&R Malloc and Free Inefficient use of memory: fragmentation  Best-fit policy can leave lots of “holes” of free blocks in memory Long execution times: linear-time overhead  Malloc scans the free list to find a big-enough block  Free scans the free list to find where to insert a block Accessing a wide range of memory addresses in free list  Can lead to large amount of paging to/from the disk In use In use In use Free list

17 Improvements: Placement Placement: reducing fragmentation  Deciding which free block to use to satisfy a malloc() request  K&R uses “first fit” (really, “next fit”) –Example: malloc(8) would choose the 20-byte block  Alternative: “best fit” or “good fit” to avoid wasting space –Example: malloc(8) would choose the 8-byte block In use In use In use Free list

18 Improvements: Splitting Splitting: avoiding wasted memory  Subdividing a large free block, and giving part to the user  K&R malloc() does splitting whenever the free block is too big –Example: malloc(14) splits the 20-byte block  Alternative: selective splitting, only when the savings is big enough –Example: malloc(14) allocates the entire 20-byte block In use In use In use Free list

19 Improvements: Coalescing Coalescing: reducing fragmentation  Combining contiguous free blocks into a larger free blocks  K&R does coalescing in free() whenever possible –Example: combine free block with lower and upper neighbors  Alternative: deferred coalescing, done only intermittently –Example: wait, and coalesce many blocks at a time later In use FREE ME In use Free list bpp lowerupper

20 Improvements: Faster Free Performance problems with K&R free()  Scanning the free list to know where to insert  Keeping track of the “previous” node to do the insertion Doubly-linked, non-circular list  Header –Size of the block (in # of units) –Flag indicating whether the block is free or in use –If free, a pointer to the next free block  Footer –Size of the block (in # of units) –If free, a pointer to the previous free block headhead footfoot

21 Size: Finding Next Block Go quickly to next block in memory  Start with the user’s data portion of the block  Go backwards to the head of the block –Easy, since you know the size of the header  Go forward to the head of the next block –Easy, since you know the size of the current block

22 Size: Finding Previous Block Go quickly to previous chunk in memory  Start with the user’s data portion of the block  Go backwards to the head of the block –Easy, since you know the size of the header  Go backwards to the footer of the previous block –Easy, since you know the size of the footer  Go backwards to the header of the previous block –Easy, since you know the size from the footer

23 Pointers: Next Free Block Go quickly to next free block in memory  Start with the user’s data portion of the block  Go backwards to the head of the block –Easy, since you know the size of the header  Go forwards to the next free block –Easy, since you have the next free pointer

24 Pointers: Previous Free Block Go quickly to previous free block in memory  Start with the user’s data portion of the block  Go backwards to the head of the block –Easy, since you know the size of the header  Go forwards to the footer of the block –Easy, since you know the block size from the header  Go backwards to the previous free block –Easy, since you have the previous free pointer

25 Efficient Free Before: K&R  Scan the free list till you find the place to insert –Needed to see if you can coalesce adjacent blocks  Expensive for loop with several pointer comparisons After: with header/footer and doubly-linked list  Coalescing with the previous block in memory –Check if previous block in memory is also free –If so, coalesce  Coalescing with the next block in memory the same way  Add the new, larger block to the front of the linked list

26 But Malloc is Still Slow… Still need to scan the free list  To find the first, or best, block that fits Root of the problem  Free blocks have a wide range of sizes Solution: binning  Separate free lists by block size  Implemented as an array of free-list pointers

27 Binning Strategies: Exact Fit Have a bin for each block size, up to a limit  Advantages: no search for requests up to that size  Disadvantages: many bins, each storing a pointer Except for a final bin for all larger free blocks  For allocating larger amounts of memory  For splitting to create smaller blocks, when needed >

28 Binning Strategies: Range Have a bin cover a range of sizes, up to a limit  Advantages: fewer bins  Disadvantages: need to search for a big enough block Except for a final bin for all larger free chunks  For allocating larger amounts of memory  For splitting to create smaller blocks, when needed >

29 Suggestions for Assignment #6 Debugging memory management code is hard  A bug in your code might stomp on the headers or footers  … making it very hard to understand where you are in memory Suggestion: debug carefully as you go along  Write little bits of code at a time, and test as you go  Use assertion checks very liberally to catch mistakes early  Use functions to apply higher-level checks on your list –E.g,. all free-list blocks are marked as free –E.g., each block pointer is within the heap range –E.g., the block size in header and footer are the same Suggestion: draw lots and lots of pictures

30 Conclusions K&R malloc and free have limitations  Fragmentation of the free space –Due to the first-first strategy  Linear time for malloc and free –Due to the need to scan the free list Optimizations  Faster free –Headers and footers –Size information and doubly-linked free list  Faster malloc –Multiple free lists, one per size (or range of sizes)

31 Backup Slides

32 Stupid Programmer Tricks Inside the malloc library if (size < 32) size = 32; else if (size > 2048) size = 4096 * ((size+4095)/4096); else if (size & (size-1)) { find next larger power-of-two }

33 Stupid Programmer Tricks Inside the malloc library Why 4096?  Use mmap() instead of sbrk() Mmap (memory map) – originally intended to “map” a file into virtual address space  Often better than malloc+read. Why?  If no file specified, mapping becomes “anonymous” – temporary  Map/unmap at finer granularity (within reason)  Recycling – unmapped pages might get used by next sbrk()