Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Heap Management.

Slides:



Advertisements
Similar presentations
Part IV: Memory Management
Advertisements

Dynamic 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.
Kernel memory allocation
Lecture 10: Heap Management CS 540 GMU Spring 2009.
File Systems.
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
Malloc Recitation Section K (Kevin Su) November 5 th, 2012.
Hastings Purify: Fast Detection of Memory Leaks and Access Errors.
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.
Memory Management Memory Areas and their use Memory Manager Tasks:
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 (continued) CS-3013 C-term Memory Management CS-3013 Operating Systems C-term 2008 (Slides include materials from Operating System.
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.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
File System Implementation Chapter 12. File system Organization Application programs Application programs Logical file system Logical file system manages.
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #6C Pointers,
OSes: 11. FS Impl. 1 Operating Systems v Objectives –discuss file storage and access on secondary storage (a hard disk) Certificate Program in Software.
Storage Management - Chap 10 MANAGING A STORAGE HIERARCHY on-chip --> main memory --> 750ps - 8ns ns. 128kb - 16mb 2gb -1 tb. RATIO 1 10 hard disk.
CS 149: Operating Systems March 3 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
1 Dynamic Memory Allocation: Basic Concepts. 2 Today Basic concepts Implicit free lists.
CS 153 Design of Operating Systems Spring 2015 Lecture 21: File Systems.
CS 241 Discussion Section (11/17/2011). Outline Review of MP7 MP8 Overview Simple Code Examples (Bad before the Good) Theory behind MP8.
CE Operating Systems Lecture 17 File systems – interface and implementation.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Memory Management Overview.
1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.
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)
Dynamic Memory Allocation II
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Dynamic Memory Allocation: Basic Concepts :
University of Washington Today More memory allocation!
CS 241 Discussion Section (2/9/2012). MP2 continued Implement malloc, free, calloc and realloc Reuse free memory – Sequential fit – Segregated fit.
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 I: Dynamic Storage Allocation Oct 8, 1998 Topics User-level view Policies Mechanisms class14.ppt Introduction to Computer Systems.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
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.
Section 10: Memory Allocation Topics
Memory Management Memory Areas and their use Memory Manager Tasks:
CS 326 Programming Languages, Concepts and Implementation
Memory Management I: Dynamic Storage Allocation Oct 7, 1999
Memory Management Memory Areas and their use Memory Manager Tasks:
Dynamic Memory Allocation: Basic Concepts CS220: Computer Systems II
Chapter 11: File System Implementation
Optimizing Malloc and Free
Memory Management I: Dynamic Storage Allocation March 2, 2000
CS Introduction to Operating Systems
Simulated Pointers.
Chapter 9: Virtual-Memory Management
Simulated Pointers.
CS703 - Advanced Operating Systems
Memory Management Overview
Optimizing Dynamic Memory Management
Operating System Chapter 7. Memory Management
Malloc Lab CSCI 380: Operating Systems
Memory Management Memory Areas and their use Memory Manager Tasks:
CS703 - Advanced Operating Systems
Chapter 8 & 9 Main Memory and Virtual Memory
Presentation transcript:

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Heap Management

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Abstract I call malloc… the system quickly, and correctly determines if it can satisfy my request, and returns a pointer to the memory. I call free… the system quickly returns.

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Concrete The system heap management routines must trade-off between: Efficiency – how fast are malloc and free requests honored Correctness – If a chunk exists in heap memory, you will get it Sustainability – If the system grants my request now, will that cause it to refuse valid requests later (e.g. fragmentation) Result: Sophisticated Heap Management Algorithms

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Leaky Abstract: Why we learn concrete Identify bugs which have bizarre side effects Bugs can corrupt system information When that happens, what are the results? When that happens, what might the causes be? Optimize malloc/free invocations e.g. order of invocation may not make a difference to program logic, but may make a big difference to performance! Leverage knowledge for debugging Use system tools to identify logic errors

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Implementation Detail : Prefixes Need to keep some information about chunks of memory on the heap For instance, how long is this chunk of memory Accepted practice… keep 4 bytes BEFORE the memory for book- keeping 20 int * numbers=(int *)malloc(4*sizeof(int)); numbers prefix

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Implementation Detail: Alignment For most hardware, it is faster to retrieve 4 bytes which start at an address divisible by 4 rather than 4 bytes at an odd address malloc doesn’t know how you are going to use your data, so aligns your data on an address divisible by 4 (or 8 or 16) 0x0804c004 0x0804c003

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Heap Management V1: Simple Free List Prefix for allocated blocks… length of block (or pointer to next) System keeps a list of free heap memory Each entry contains : Start address and Length of free block malloc calls are satisfied by finding the first entry on the free list which is at least as large as the requested size When malloc finds a free entry, requests are taken from the front of the free block, and the free entry is updated with a new address/length. Free calls create a new free list entry at the end of the free list

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Simple Free List Example Free List LocationLength 0x08c bytes “chunks” m1=malloc(20); /* 20=0x14 */

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Simple Free List Example 18 Free List LocationLength 0x08c m1=malloc(20); /* m1= 0x08c06004 */ m2=malloc(12);

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Simple Free List Example 1810 Free List LocationLength 0x08c m1=malloc(20); /* m1= 0x08c06004 */ m2=malloc(12); /* m2 = 0x08c0601c */ free(m1);

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Simple Free List Example 10 Free List LocationLength 0x08c x08c m1=malloc(20); m2=malloc(12); /* m2 = 0x08c0601c */ free(m1); m1=malloc(40); /* 40=0x28 */

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Simple Free List Example 102c Free List LocationLength 0x08c x08c m1=malloc(20); m2=malloc(12); /* m2 = 0x08c0601c */ free(m1); m1=malloc(40); /* m1= 0x08c0602c */ m3=malloc(12);

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Simple Free List Example 10 2c Free List LocationLength 0x08c x08c m1=malloc(20); m2=malloc(12); /* m2 = 0x08c0601c */ free(m1); m1=malloc(40); /* m1= 0x08c0602c */ m3=malloc(12); /* m3= 0x08c06004 */ free(m2);

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Simple Free List Example 102c Free List LocationLength 0x08c x08c x08c m1=malloc(20); m2=malloc(12); free(m1); m1=malloc(40); /* m1= 0x08c0602c */ m3=malloc(12); /* m3= 0x08c06004 */ free(m2); m2=malloc(16); /* 16=0x10 */

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Simple Free List Advantages Very fast free malloc speed depends on size of free list Good for many requests of the same size Disadvantages Adjacent free blocks never consolidated FRAGMENTATION! (Sustainability) Free list size grows over time malloc performance gets worse over time Space for free list + prefix

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Free List: Consolidating Blocks Requires free list sorted by start address Period “sort/merge” (expensive, unpredictable performance) or Keep free list sorted by address, merge on free Increases expense of free Reduces randomness in malloc search for free block

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Free List Consolidation Example 102c Free List LocationLength 0x08c x08c x08c

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Free List Consolidation Example 102c Free List LocationLength 0x08c x08c m1=malloc(20); m2=malloc(12); free(m1); m1=malloc(40); /* m1= 0x08c0602c */ m3=malloc(12); /* m3= 0x08c06004 */ free(m2); m2=malloc(16); /* 16=0x10 */

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Free List with Binning Keep multiple free lists for different free block sizes malloc request goes directly to correct bin free entry may move to different bin when malloc subtracts from it Consolidate when bin is full

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Free List with Binning Advantages Short search for malloc, even when free list is large Consolidate before space is required – enables “background” consolidation Disadvantages More work for malloc move entry to new bin More work for free find right bin Consolidation needs to occur across bins

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Heap Management V2: Linked Blocks Blocksize always multiple of 4 Prefix: Blocksize (or pointer to next) Low order bit used as flag: 1=“used”, 0=“free” malloc walks block list until it find the first free block with size large enough to satisfy request Splits block into used request size and free remainder free – unsets used bit and merges block with subsequent free blocks

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Simple Linked Block Example 60 m1=malloc(20); /* 20=0x14 */

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Simple Linked Block Example 1948 m1=malloc(20); /* m1 = 0x08c06004 */ m2=malloc(12);

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Simple Linked Block Example m1=malloc(20); /* m1 = 0x08c06004 */ m2=malloc(12); /* m2 = 0x08c0601c */ free(m1);

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Simple Linked Block Example m1=malloc(20); m2=malloc(12); /* m2 = 0x08c0601c */ free(m1); m1=malloc(40); /* 40=0x28 */

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Simple Linked Block Example 18112d0c m1=malloc(20); m2=malloc(12); /* m2 = 0x08c0601c */ free(m1); m1=malloc(40); /* m1 = 0x08c0602c */ m3=malloc(12);

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Simple Linked Block Example d0c m1=malloc(20); m2=malloc(12); /* m2 = 0x08c0601c */ free(m1); m1=malloc(40); /* m1 = 0x08c0602c */ m3=malloc(12); /* m3 = 0x08c06004 */ free(m2);

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Simple Linked Block Example d0c m1=malloc(20); m2=malloc(12); free(m1); m1=malloc(40); /* m1 = 0x08c0602c */ m3=malloc(12); /* m3 = 0x08c06004 */ free(m2); m2=malloc(16);

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Simple Linked Block Advantages malloc is relatively quick depends on size of heap free is quick Solves half the consolidation problem No space for free list required Disadvantages Still fragments Need to walk through both allocated and free blocks on malloc Need to consolidate with previous free block!

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Linked Block Consolidation: v1 On free, find preceding block Start at beginning of heap Walk forward, keeping track of where you came from If this block is block being freed and preceding block is free merge preceding block with this block If next block is free merge this block with next block Makes free performance dependent on size of heap!

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Linked Block Consolidation: V2 Put a suffix at the end of each block with the length of the block Word before this block’s prefix is previous block’s suffix Use previous block’s suffix to get back to previous block’s prefix Consolidate with previous block if free

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Linked Block Suffix Example 60 m1=malloc(20); /* 20=0x14 */

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Linked Block Suffix Example 1d1c44 m1=malloc(20); /* m1 = 0x08c06004 */ m2=malloc(12);

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Linked Block Suffix Example 1d1c m1=malloc(20); /* m1 = 0x08c06004 */ m2=malloc(12); /* m2 = 0x08c06020 */ free(m1);

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Linked Block Suffix Example 1c m1=malloc(20); m2=malloc(12); /* m2 = 0x08c06020 */ free(m1); m1=malloc(40); /* 40=0x28 */

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Linked Block Suffix Example 1c m1=malloc(20); m2=malloc(12); /* m2 = 0x08c06020 */ free(m1); m1=malloc(40); /* m1= 0x08c06038 */ m3=malloc(12);

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Linked Block Suffix Example m1=malloc(20); m2=malloc(12); /* m2 = 0x08c06020 */ free(m1); m1=malloc(40); /* m1= 0x08c06038 */ m3=malloc(12); /* m3 = 0x08c06004 */ free(m2);

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Linked Block Suffix Example 15141c 3130 m1=malloc(20); m2=malloc(12); /* m2 = 0x08c06020 */ free(m1); m1=malloc(40); /* m1= 0x08c06038 */ m3=malloc(12); /* m3 = 0x08c06004 */ free(m2); m2=malloc(16);

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Linked Block Suffix Example ??3130 m1=malloc(20); m2=malloc(12); /* m2 = 0x08c06020 */ free(m1); m1=malloc(40); /* m1= 0x08c06038 */ m3=malloc(12); /* m3 = 0x08c06004 */ free(m2); m2=malloc(16); /* Remainder block must be 2 words! */

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Linked Block Suffix Example 15141d1c3130 m1=malloc(20); m2=malloc(12); /* m2 = 0x08c06020 */ free(m1); m1=malloc(40); /* m1= 0x08c06038 */ m3=malloc(12); /* m3 = 0x08c06004 */ free(m2); m2=malloc(16); /* m2 = 0x08c06020 */

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Linked Block Suffix Example m1=malloc(20); m2=malloc(12); /* m2 = 0x08c06020 */ free(m1); m1=malloc(40); /* m1= 0x08c06038 */ m3=malloc(12); /* m3 = 0x08c06004 */ free(m2); m2=malloc(16); /* m2 = 0x08c06020 */

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Linked Block Suffix Advantages Enables consolidation of preceding and following blocks Reduces fragmentation free occurs in linear time Disadvantages Doubles overhead 8 bytes per block instead of 4

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Other Considerations Error Resistance What happens when you overwrite past malloc’ed area? Can errors be detected? Can errors be corrected? Debug Capabilities Can debug tool determine used/allocated blocks? Can programmer detect corrupted prefix/suffix in debugger?

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Debugging Storage Manager Keeps constant tags around prefix/suffix When tag value changes, tags are likely to be corrupted! Keeps return address of last malloc and free in tag / free list If a problem occurred, which memory is it? Enables walk through heap to verify current state is valid Significant extra cost – performance and memory Used only when bug is suspected