University of Washington Wouldn’t it be nice… If we never had to free memory? Do you free objects in Java? 1.

Slides:



Advertisements
Similar presentations
Dynamic Memory Allocation II
Advertisements

Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
Instructors: Seth Copen Goldstein, Franz Franchetti, Greg Kesden
Garbage Collection Introduction and Overview Christian Schulte Excerpted from presentation by Christian Schulte Programming Systems Lab Universität des.
1. Dynamic Memory Allocation. Dynamic Memory Allocation Programmers use dynamic memory allocators (such as malloc ) to acquire VM at run time.  For data.
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.
1 Binghamton University Dynamic Memory Allocation: Advanced Concepts CS220: Computer Systems II.
Garbage Collection. Memory Management So Far ● Some items are preallocated and persist throughout the program: ● What are they? Some are allocated on.
Dynamic Memory Allocation Topics Simple explicit allocators Data structures Mechanisms Policies.
Some topics in Memory Management
University of Washington Today Dynamic memory allocation  Size of data structures may only be known at run time  Need to allocate space on the heap 
1 Dynamic Memory Allocation: Advanced Concepts Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron Joke of the day: Why did the.
Mark and Sweep Algorithm Reference Counting Memory Related PitFalls
CS 536 Spring Automatic Memory Management Lecture 24.
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.
Carnegie Mellon 1 Dynamic Memory Allocation: Advanced Concepts : Introduction to Computer Systems 18 th Lecture, Oct. 26, 2010 Instructors: Randy.
Carnegie Mellon Introduction to Computer Systems /18-243, spring th Lecture, Mar. 31 st Instructors: Gregory Kesden and Markus Püschel.
Memory Management Professor Yihjia Tsai Tamkang University.
Dynamic Memory Allocation II Nov 7, 2002 Topics Explicit doubly-linked free lists Segregated free lists Garbage collection Memory-related perils and pitfalls.
Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
Dynamic Memory Allocation II November 7, 2007 Topics Explicit doubly-linked free lists Segregated free lists Garbage collection Review of pointers Memory-related.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Garbage collection (& Midterm Topics) David Walker COS 320.
Dynamic Memory Allocation II March 27, 2008 Topics Explicit doubly-linked free lists Segregated free lists Garbage collection Review of pointers Memory-related.
University of Washington Memory Allocation III The Hardware/Software Interface CSE351 Winter 2013.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
Explicit Free Lists Use data space for link pointers –Typically doubly linked –Still need boundary tags for coalescing –It is important to realize that.
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.
Dynamic Memory Allocation II
Garbage Collection and Memory Management CS 480/680 – Comparative Languages.
Carnegie Mellon /18-243: Introduction to Computer Systems Instructors: Bill Nace and Gregory Kesden (c) All Rights Reserved. All work.
Dynamic Memory Allocation II Topics Explicit doubly-linked free lists Segregated free lists Garbage collection.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Dynamic Memory Allocation II April 1, 2004 Topics Explicit doubly-linked free lists Segregated free lists Garbage collection Memory-related perils and.
Dynamic Memory Allocation II November 4, 2004 Topics Explicit doubly-linked free lists Segregated free lists Garbage collection Memory-related perils and.
Introduction to Garbage Collection. Garbage Collection It automatically reclaims memory occupied by objects that are no longer in use It frees the programmer.
CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.
University of Washington Today More memory allocation!
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
1 Advanced Topics. 2 Outline Garbage collection Memory-related perils and pitfalls Suggested reading: 9.10, 9.11.
Dynamic Memory Allocation II March 27, 2008 Topics Explicit doubly-linked free lists Segregated free lists Garbage collection Review of pointers Memory-related.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Dynamic Memory Allocation: Advanced Concepts :
Instructor: Fatma CORUT ERGİN
Garbage Collection What is garbage and how can we deal with it?
Dynamic Memory Allocation
Instructors: Roger Dannenberg and Greg Ganger
Memory Allocation III CSE 351 Spring 2017
Dynamic Memory Allocation II Nov 7, 2000
Memory Management: Dynamic Allocation
Dynamic Memory Allocation
Concepts of programming languages
Memory Allocation III CSE 351 Autumn 2017
Dynamic Memory Allocation: Advanced Concepts /18-213/14-513/15-513: Introduction to Computer Systems 20th Lecture, November 2, 2017.
Memory Allocation III CSE 351 Autumn 2016
Memory Allocation III CSE 351 Winter 2018
Memory Allocation III CSE 351 Summer 2018
Memory Allocation III CSE 351 Spring 2017
CS703 - Advanced Operating Systems
Memory Allocation III CSE 351 Spring 2017
Memory Allocation III CSE 351 Autumn 2018
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

University of Washington Wouldn’t it be nice… If we never had to free memory? Do you free objects in Java? 1

University of Washington Garbage Collection (GC) (Automatic Memory Management / Implicit Memory Allocation) Garbage collection: automatic reclamation of heap-allocated storage—application never explicitly frees memory. Common in implementations of functional languages, scripting languages, and modern object oriented languages:  Lisp, Racket, Erlang, ML, Haskell, Scala, Java, C#, Perl, Ruby, Python, Lua, JavaScript, Dart, Mathematica, MATLAB, many more… Variants (“conservative” garbage collectors) exist for C and C++  However, cannot necessarily collect all garbage void foo() { int* p = (int *)malloc(128); return; /* p block is now garbage */ } 2

University of Washington Garbage Collection How does the memory allocator know when memory can be freed?  In general, we cannot know what is going to be used in the future since it depends on conditionals (halting problem, etc.) 3

University of Washington Garbage Collection How does the memory allocator know when memory can be freed?  In general, we cannot know what is going to be used in the future since it depends on conditionals (halting problem, etc.)  But, we can tell that certain blocks cannot be used if there are no pointers to them So the memory allocator needs to know what is a pointer and what is not – how can it do this? We’ll make some assumptions about pointers:  Memory allocator can distinguish pointers from non-pointers  All pointers point to the start of a block in the heap  Application cannot hide pointers (e.g., by coercing them to an int, and then back again) 4

University of Washington Classical GC Algorithms Mark-and-sweep collection (McCarthy, 1960)  Does not move blocks (unless you also “compact”) Reference counting (Collins, 1960)  Does not move blocks (not discussed) Copying collection (Minsky, 1963)  Moves blocks (not discussed) Generational Collectors (Lieberman and Hewitt, 1983)  Most allocations become garbage very soon, so focus reclamation work on zones of memory recently allocated. For more information:  Jones, Hosking, and Moss, The Garbage Collection Handbook: The Art of Automatic Memory Management, CRC Press,  Jones and Lin, Garbage Collection: Algorithms for Automatic Dynamic Memory, John Wiley & Sons,

University of Washington Memory as a Graph We view memory as a directed graph  Each allocated heap block is a node in the graph  Each pointer is an edge in the graph  Locations not in the heap that contain pointers into the heap are called root nodes (e.g. registers, locations on the stack, global variables) Root nodes Heap nodes Not-reachable (garbage) reachable A node (block) is reachable if there is a path from any root to that node Non-reachable nodes are garbage (cannot be needed by the application) 6

University of Washington Mark and Sweep Collecting Can build on top of malloc/free package  Allocate using malloc until you “run out of space” When out of space:  Use extra mark bit in the head of each block  Mark: Start at roots and set mark bit on each reachable block  Sweep: Scan all blocks and free blocks that are not marked Before mark root After mark Mark bit set After sweep free 7

University of Washington Assumptions For a Simple Implementation Application can use functions such as:  new(n) : returns pointer to new block with all locations cleared  read(b,i): read location i of block b into register  b[i]  write(b,i,v): write v into location i of block b  b[i] = v Each block will have a header word  b[-1] Functions used by the garbage collector:  is_ptr(p): determines whether p is a pointer to a block  length(p): returns length of block pointed to by p, not including header  get_roots(): returns all the roots 8

University of Washington Mark ptr mark(ptr p) { // p: some word in a heap block if (!is_ptr(p)) return; // do nothing if not pointer if (markBitSet(p)) return; // check if already marked setMarkBit(p); // set the mark bit for (i=0; i < length(p); i++) // recursively call mark on mark(p[i]); // all words in the block return; } Mark using depth-first traversal of the memory graph 9 After mark Mark bit set Before mark root

University of Washington Sweep Sweep using lengths to find next block ptr sweep(ptr p, ptr end) { // ptrs to start & end of heap while (p < end) { // while not at end of heap if markBitSet(p) // check if block is marked clearMarkBit(p); // if so, reset mark bit else if (allocateBitSet(p)) // if not marked, but allocated free(p); // free the block p += length(p); // adjust pointer to next block } 10 After mark Mark bit set After sweep free

University of Washington Conservative Mark & Sweep in C Would mark & sweep work in C?  is_ptr() (previous slide) determines if a word is a pointer by checking if it points to an allocated block of memory  But in C, pointers can point into the middle of allocated blocks (not so in Java)  Makes it tricky to find all allocated blocks in mark phase  There are ways to solve/avoid this problem in C, but the resulting garbage collector is conservative:  Every reachable node correctly identified as reachable, but some unreachable nodes might be incorrectly marked as reachable header ptr 11

University of Washington Bonus: Reference Counting Different GC strategy than mark-sweep. Store reference count in object/block header:  number of existing references (pointers) to this object/block new(n) : returns pointer to new block with refcount = 0 if reference count > 0:  object/block is reachable else:  object/block is unreachable Does it collect as much garbage as mark-sweep?  No. Cycles… In practice, other algorithms are more commonly used. Example: see 2 nd half of section slides from spring 2012, also posted for our section on August 15:

University of Washington Reference Counting code rewrite 13 x = y; x holds ref; y is ref write(x,y); x could be any LHS: *ptr, arr[3], st->field, local var, etc. x holds non-ref; y is non-ref x = y;

University of Washington Reference Counting algorithm write(lhs,rhs):  addRef(rhs)  deleteRef(lhs)  lhs = rhs addRef(r):  if r != null: r.refcount++ deleteRef(r):  if r != null:  r.refcount--  if (r.refcount) == 0: –for reference field r2 in r: »deleteRef(r2) –free(r) 14 This order is important. Why?