Lecture 10: Heap Management CS 540 GMU Spring 2009.

Slides:



Advertisements
Similar presentations
Dynamic Memory Management
Advertisements

Introduction to Memory Management. 2 General Structure of Run-Time Memory.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
Garbage Collection What is garbage and how can we deal with it?
Garbage Collection Introduction and Overview Christian Schulte Excerpted from presentation by Christian Schulte Programming Systems Lab Universität des.
Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support.
Compiler construction in4020 – lecture 12 Koen Langendoen Delft University of Technology The Netherlands.
5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al.
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
CS 536 Spring Automatic Memory Management Lecture 24.
CSC321: Programming Languages 11-1 Programming Languages Tucker and Noonan Chapter 11: Memory Management 11.1 The Heap 11.2 Implementation of Dynamic Arrays.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Memory Management. History Run-time management of dynamic memory is a necessary activity for modern programming languages Lisp of the 1960’s was one of.
CS 1114: Data Structures – memory allocation Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Memory Management Professor Yihjia Tsai Tamkang University.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
CS 536 Spring Run-time organization Lecture 19.
1 Storage Allocation Operating System Hebrew University Spring 2007.
Runtime The optimized program is ready to run … What sorts of facilities are available at runtime.
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
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.
Garbage collection (& Midterm Topics) David Walker COS 320.
Linked lists and memory allocation Prof. Noah Snavely CS1114
Run-time Environment and Program Organization
 2004 Deitel & Associates, Inc. All rights reserved. Chapter 9 – Real Memory Organization and Management Outline 9.1 Introduction 9.2Memory Organization.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
Memory Allocation CS Introduction to Operating Systems.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
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.
Chapter 4 Memory Management.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Memory Management II: Dynamic Storage Allocation Mar 7, 2000 Topics Segregated free lists –Buddy system Garbage collection –Mark and Sweep –Copying –Reference.
1 Lecture 22 Garbage Collection Mark and Sweep, Stop and Copy, Reference Counting Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction.
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
Memory Management -Memory allocation -Garbage collection.
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)
Runtime The optimized program is ready to run … What sorts of facilities are available at runtime.
Spring, 2011 –– Computational Thinking – Dennis Kafura – CS 2984 Data Structures Mapping complex structures to linear memory.
Simulated Pointers Limitations Of C++ Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
CS61C L07 More Memory Management (1) Garcia, Spring 2010 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.
 2004 Deitel & Associates, Inc. All rights reserved. Chapter 9 – Real Memory Organization and Management Outline 9.1 Introduction 9.2Memory Organization.
Memory Management What if pgm mem > main mem ?. Memory Management What if pgm mem > main mem ? Overlays – program controlled.
Garbage Collection What is garbage and how can we deal with it?
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Dynamic Memory Allocation
Storage Management.
Chapter 9 – Real Memory Organization and Management
Concepts of programming languages
Automatic Memory Management
Optimizing Malloc and Free
CS Introduction to Operating Systems
Simulated Pointers.
Smart Pointers.
Memory Management and Garbage Collection Hal Perkins Autumn 2011
Simulated Pointers.
Memory Management Kathryn McKinley.
Memory Allocation CS 217.
Chapter 12 Memory Management
Operating System Chapter 7. Memory Management
Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Automating Memory Management
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

Lecture 10: Heap Management CS 540 GMU Spring 2009

Process Address Space Each process has its own address space: –Text section (text segment) contains the executable code –Data section (data segment) contains the global variables –Stack contains temporary data (local variables, return addresses..) –Heap, which contains memory that is dynamically allocated at run-time. text data heap stack

Heap Management Heap is used to allocate space for dynamic objects May be managed by the user or by the runtime system In a perfect world: livedead not deleted  ---- deleted---- 

User Heap Management User library manages memory; programmer decides when and where to allocate and deallocate void* malloc(longn) void free(void*addr) Library calls OS for more pages when necessary How does malloc/free work? Blocks of unused memory stored on a freelist malloc: search free list for usable memory block free: put block onto the head of the freelist

User Heap Management Drawbacks malloc is not free: we might have to do a search to find a big enough block As program runs, the heap fragments, leaving many small, unusable pieces Have to have a way to reclaim and merge blocks when freed. Memory management always has a cost. We want to minimize costs and, these days, maximize reliability

User Heap Management Pro: performance Con: safety livedead not deleted  memory leak deleteddangling reference 

Automatic Heap Management Garbage collection – reclamation of chunks of storage holding objects that can no longer be accessed by a program Originated with Lisp New languages tend to have automatic management Less error prone but performance penalty Assumptions: type info is available at runtime (how large a block is, where are pointers), pointers are always to start of block

Automatic Heap Management Issues: How much does this increase the runtime of a program? Space – need to manage free/used space Pause time – incremental vs. ‘stop the world’ Influences on data locality Different types/sizes of objects

Locating Garbage STACKSTACK r1 r2

Locating Garbage r1 r2 STACKSTACK

Object reachability The set of reachable objects changes as a program executes- Object allocations Parameters & return values Reference assignments Stack-based variables

Reference Counting Keep a count of pointers to each object –performance overhead each time the reachable set can change –storage overhead since each object needs a count field Zero references  garbage that can be removed (applied transitively) > zero references  not garbage??

Reference Counting STACKSTACK r1 r

What if r1 is removed? STACKSTACK r1 r

Trace Collecting When the heap starts to fill, pause the program and reclaim ‘Stop the world’ – pauses can be significant Lots of versions –Mark & sweep –Mark & compact –…

Mark & Sweep Basic idea: maintain a linked list, called the free list, that contains all of the heap blocks that can be allocated if the program requests a heap block but the free list is empty, invoke the garbage collector –starting from the pointers in the stack, data area and registers*, trace and mark all reachable heap blocks –sweep and collect all unmarked heap blocks, putting each one back on the free list (merging as appropriate) –sweep again, unmarking all heap blocks First developed by McCarthy in 1960 for Lisp * detecting what is a pointer is easier said than done

Mark & Sweep Issues: How to detect pointers? Need to keep a bit that we can use for the algorithm in every object In Lisp, the program execution would pause while the system did garbage collecting

Mark & Compact Basic idea (Fig 7.27 in text): To garbage collect 1.Starting from the pointers in the stack, data area and registers, find and mark all reachable heap blocks 2.Scan the marked nodes and compute a new address for each, based on where it should be relocated to have all of the blocks contiguous 3.Move each block to its new location, updating any internal pointers into the heap based on step 2. Update any program & stack variables based on the new assignments as well.

Generational Collection Generational collection –Idea: An object that survives its first round of garbage collection is likely to survive later (i.e. objects tend to die young)

A Generational algorithm Memory is divided into N partitions New objects are always allocated into partition 0 When partition 0 fills, it is garbage collected (via some technique). Anything that survives is moved to partition 1 (leaving 0 empty). Keep doing this – eventually partition i (> 0) will fill. Once it fills, garbage collect it and put surviving elements into partition i+1.

Generational Garbage Collection