Garbage Collection Introduction and Overview Christian Schulte Excerpted from presentation by Christian Schulte Programming Systems Lab Universität des.

Slides:



Advertisements
Similar presentations
Garbage Collection Introduction and Overview Christian Schulte Programming Systems Lab Universität des Saarlandes, Germany
Advertisements

Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Dynamic Memory Management
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
Memory allocation in computer science, is the act of allocating memory to a program for its usage, typically for storing variables, code or data. Memory.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al.
Memory Management Tom Roeder CS fa. Motivation Recall unmanaged code eg C: { double* A = malloc(sizeof(double)*M*N); for(int i = 0; i < M*N; i++)
Run-time organization  Data representation  Storage organization: –stack –heap –garbage collection Programming Languages 3 © 2012 David A Watt,
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
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.
Ceg860 (Prasad)L9GC1 Memory Management Garbage Collection.
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.
CS 1114: Data Structures – memory allocation Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Run-Time Storage Organization
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
C and Data Structures Baojian Hua
Garbage collection (& Midterm Topics) David Walker COS 320.
Linked lists and memory allocation Prof. Noah Snavely CS1114
Reference Counters Associate a counter with each heap item Whenever a heap item is created, such as by a new or malloc instruction, initialize the counter.
Memory Layout C and Data Structures Baojian Hua
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
1 Thread Synchronization (Un)Fairness in the.NET Common Language Runtime If you are building an application that absolutely requires fair thread synchronization,
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.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Memory management (CTM 2.5) Carlos Varela RPI April 6, 2015.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Storage Bindings Allocation is the process by which the memory cell or collection of memory cells is assigned to a variable. These cells are taken from.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
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.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Compilation (Semester A, 2013/14) Lecture 13b: Memory Management Noam Rinetzky Slides credit: Eran Yahav 1.
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.
Runtime System CS 153: Compilers. Runtime System Runtime system: all the stuff that the language implicitly assumes and that is not described in the program.
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
University of Washington Wouldn’t it be nice… If we never had to free memory? Do you free objects in Java? 1.
GARBAGE COLLECTION IN AN UNCOOPERATIVE ENVIRONMENT Hans-Juergen Boehm Computer Science Dept. Rice University, Houston Mark Wieser Xerox Corporation, Palo.
Dynamic Memory Allocation II Topics Explicit doubly-linked free lists Segregated free lists 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)
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.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Memory Management What if pgm mem > main mem ?. Memory Management What if pgm mem > main mem ? Overlays – program controlled.
Object Lifetime and Pointers
Garbage Collection What is garbage and how can we deal with it?
Dynamic Memory Allocation
Storage Management.
Concepts of programming languages
Automatic Memory Management
Storage.
Memory Management and Garbage Collection Hal Perkins Autumn 2011
Memory Management Kathryn McKinley.
Closure Representations in Higher-Order Programming Languages
Dynamic Memory.
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

Garbage Collection Introduction and Overview Christian Schulte Excerpted from presentation by Christian Schulte Programming Systems Lab Universität des Saarlandes, Germany

Garbage Collection… …is concerned with the automatic reclamation of dynamically allocated memory after its last use by a program

Garbage collection… Dynamically allocated memory Dynamically allocated memory Last use by a program Last use by a program Examples for automatic reclamation Examples for automatic reclamation

Kinds of Memory Allocation static int i; void foo(void) { int j; int* p = (int*) malloc(…); }

Static Allocation By compiler (in text area) By compiler (in text area) Available through entire runtime Available through entire runtime Fixed size Fixed size static int i; void foo(void) { int j; int* p = (int*) malloc(…); }

Automatic Allocation Upon procedure call (on stack) Upon procedure call (on stack) Available during execution of call Available during execution of call Fixed size Fixed size static int i; void foo(void) { int j; int* p = (int*) malloc(…); }

Dynamic Allocation Dynamically allocated at runtime (on heap) Dynamically allocated at runtime (on heap) Available until explicitly deallocated Available until explicitly deallocated Dynamically varying size Dynamically varying size static int i; void foo(void) { int j; int* p = (int*) malloc(…); }

Dynamically Allocated Memory Also: heap-allocated memory Also: heap-allocated memory Allocation: malloc, new, … Allocation: malloc, new, … –before first usage Deallocation: free, delete, dispose, … Deallocation: free, delete, dispose, … –after last usage Needed for Needed for –C++, Java: objects –SML:datatypes, procedures –anything that outlives procedure call

Getting it Wrong Forget to free (memory leak) Forget to free (memory leak) –program eventually runs out of memory –long running programs: OSs. servers, … Free to early (dangling pointer) Free to early (dangling pointer) –lucky: illegal access detected by OS –horror: memory reused, in simultaneous use programs can behave arbitrarily crashes might happen much later Estimates of effort Estimates of effort –Up to 40%! [Rovner, 1985]

Nodes and Pointers Node n Node n –Memory block, cell Pointer p Pointer p –Link to node –Node access: *p Children children(n) Children children(n) –set of pointers to nodes referred by n n p

Mutator Abstraction of program Abstraction of program –introduces new nodes with pointer –redirects pointers, creating garbage

Nodes referred to by several pointers Nodes referred to by several pointers Makes manual deallocation hard Makes manual deallocation hard –local decision impossible –respect other pointers to node Cycles instance of sharing Cycles instance of sharing Shared Nodes

Last Use by a Program Question: When is node M not any longer used by program? Question: When is node M not any longer used by program? –Let P be any program not using M –New program sketch: Execute P; Use M; –Hence: M used  P terminates –We are doomed: halting problem! So “last use” undecidable! So “last use” undecidable!

Safe Approximation Decidable and also simple Decidable and also simple What means safe? What means safe? –only unused nodes freed What means approximation? What means approximation? –some unused nodes might not be freed Idea Idea –nodes that can be accessed by mutator

Reachable Nodes Reachable from root set Reachable from root set –processor registers –static variables –automatic variables (stack) Reachable from reachable nodes Reachable from reachable nodes root

Summary: Reachable Nodes A node n is reachable, iff A node n is reachable, iff –n is element of the root set, or –n is element of children(m) and m is reachable Reachable node also called “live” Reachable node also called “live”

Mark and Sweep Compute set of reachable nodes Compute set of reachable nodes Free nodes known to be not reachable Free nodes known to be not reachable

Reachability: Safe Approximation Safe Safe –access to not reachable node impossible –depends on language semantics –but C/C++? later… Approximation Approximation –reachable node might never be accessed –programmer must know about this! –have you been aware of this?

Example Garbage Collectors Mark-Sweep Mark-Sweep Others Others –Mark-Compact –Reference Counting –Copying –see Chapter 1&2 of [Lins&Jones,96]

The Mark-Sweep Collector Compute reachable nodes: Mark Compute reachable nodes: Mark –tracing garbage collector Free not reachable nodes: Sweep Free not reachable nodes: Sweep Run when out of memory: Allocation Run when out of memory: Allocation First used with LISP [McCarthy, 1960] First used with LISP [McCarthy, 1960]

Allocation node* new() { if (free_pool is empty) mark_sweep(); …

Allocation node* new() { if (free_pool is empty) mark_sweep(); return allocate(); }

The Garbage Collector void mark_sweep() { for (r in roots) mark(r); …

The Garbage Collector void mark_sweep() { for (r in roots) mark(r); … all live nodes marked

Recursive Marking void mark(node* n) { if (!is_marked(n)) { set_mark(n); … }

Recursive Marking void mark(node* n) { if (!is_marked(n)) { set_mark(n); … } nodes reachable from n marked

Recursive Marking void mark(node* n) { if (!is_marked(n)) { set_mark(n); for (m in children(n)) mark(m); } i-th recursion: nodes on path with length i marked

The Garbage Collector void mark_sweep() { for (r in roots) mark(r); sweep(); …

The Garbage Collector void mark_sweep() { for (r in roots) mark(r); sweep(); … all nodes on heap live

The Garbage Collector void mark_sweep() { for (r in roots) mark(r); sweep(); … all nodes on heap live and not marked

Eager Sweep void sweep() { node* n = heap_bottom; while (n < heap_top) { … }

Eager Sweep void sweep() { node* n = heap_bottom; while (n < heap_top) { if (is_marked(n)) clear_mark(n); else free(n); n += sizeof(*n); }

The Garbage Collector void mark_sweep() { for (r in roots) mark(r); sweep(); if (free_pool is empty) abort(“Memory exhausted”); }

Assumptions Nodes can be marked Nodes can be marked Size of nodes known Size of nodes known Heap contiguous Heap contiguous Memory for recursion available Memory for recursion available Child fields known! Child fields known!

Assumptions: Realistic Nodes can be marked Nodes can be marked Size of nodes known Size of nodes known Heap contiguous Heap contiguous Memory for recursion available Memory for recursion available Child fields known Child fields known

Assumptions: Conservative Nodes can be marked Nodes can be marked Size of nodes known Size of nodes known Heap contiguous Heap contiguous Memory for recursion available Memory for recursion available Child fields known Child fields known

Mark-Sweep Properties Covers cycles and sharing Covers cycles and sharing Time depends on Time depends on –live nodes (mark) –live and garbage nodes (sweep) Computation must be stopped Computation must be stopped –non-interruptible stop/start collector –long pause Nodes remain unchanged (as not moved) Nodes remain unchanged (as not moved) Heap remains fragmented Heap remains fragmented

Software Engineering Issues Design goal in SE: Design goal in SE: decompose systems in orthogonal components Clashes with letting each component do its memory management Clashes with letting each component do its memory management liveness is global property leads to “local leaks” lacking power of modern gc methods

Typical Cost Early systems (LISP) Early systems (LISP) up to 40% [Steele,75] [Gabriel,85] “garbage collection is expensive” myth Well engineered system of today Well engineered system of today 10% of entire runtime [Wilson, 94]

Areas of Usage Programming languages and systems Programming languages and systems –Java, C#, Smalltalk, … –SML, Lisp, Scheme, Prolog, … –Perl, Python, PHP, JavaScript –Modula 3, Microsoft.NET Extensions Extensions –C, C++ (Conservative) Other systems Other systems –Adobe Photoshop –Unix filesystem –Many others in [Wilson, 1996]

Understanding Garbage Collection: Benefits Programming garbage collection Programming garbage collection –programming systems –operating systems Understand systems with garbage collection (e.g. Java) Understand systems with garbage collection (e.g. Java) –memory requirements of programs –performance aspects of programs –interfacing with garbage collection (finalization)

References Garbage Collection. Richard Jones and Rafael Lins, John Wiley & Sons, Garbage Collection. Richard Jones and Rafael Lins, John Wiley & Sons, Uniprocessor garbage collection techniques. Paul R. Wilson, ACM Computing Surveys. To appear. Uniprocessor garbage collection techniques. Paul R. Wilson, ACM Computing Surveys. To appear. Extended version of IWMM 92, St. Malo.