David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 17: Garbage Collection.

Slides:



Advertisements
Similar presentations
Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Advertisements

Dynamic Memory Management
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
Fall 2010 UVa David Evans cs2220: Engineering Software Class 24: Garbage Collection flickr cc: kiksbalayon.
Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support.
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,
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 21: Garbage Collection.
Joel Winstead CS201j: Engineering Software University of Virginia Computer Science Lecture 16: Pointers and Memory Management.
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.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
CS 536 Spring Run-time organization Lecture 19.
Run-Time Storage Organization
Run time vs. Compile time
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
An Adaptive, Region-based Allocator for Java Feng Qian & Laurie Hendren 2002.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
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.
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 22: Low-Level Programming in.
1 Overview Assignment 6: hints  Living with a garbage collector Assignment 5: solution  Garbage collection.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 25 There.
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.
Lecture 10 : Introduction to Java Virtual Machine
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
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.
Basic Semantics Associating meaning with language entities.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.
Garbage Collection and Classloading Java Garbage Collectors  Eden Space  Surviver Space  Tenured Gen  Perm Gen  Garbage Collection Notes Classloading.
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.
Finalizers, this reference and static Sangeetha Parthasarathy 06/13/2001.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 9: Designing Exceptionally.
Smart Pointers. Dumb Pointers Pointers Necessary – Dynamic memory – Sharing memory.
® July 21, 2004GC Summer School1 Cycles to Recycle: Copy GC Without Stopping the World The Sapphire Collector Richard L. Hudson J. Eliot B. Moss Originally.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
CS 152: Programming Language Paradigms April 16 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 12: Automating Memory Management
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Object Lifetime and Pointers
Core Java Garbage Collection LEVEL – PRACTITIONER.
Topic: Java Garbage Collection
Dynamic Memory Allocation
Run-time organization
CS 153: Concepts of Compiler Design November 28 Class Meeting
Concepts of programming languages
Java Byte Codes (0xCAFEBABE) cs205: engineering software
Smart Pointers.
Strategies for automatic memory management
Lecture 18: Deep C Garbage Collection CS201j: Engineering Software
Created By: Asst. Prof. Ashish Shah, J.M.Patel College, Goregoan West
Class 24: Garbage Collection
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Automating Memory Management
Reference Counting.
CMPE 152: Compiler Design May 2 Class Meeting
Presentation transcript:

David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 17: Garbage Collection

5 November 2002CS 201J Fall Menu Stack and Heap Mark and Sweep Stop and Copy Reference Counting Java’s Garbage Collector

5 November 2002CS 201J Fall Stack and Heap Review public class Strings { public static void test () { StringBuffer sb = new StringBuffer ("hello"); } static public void main (String args[]) { test (); } sb java.lang.StringBuffer “hello” Stack Heap When do the stack and heap look like this? A B 1 2 3

5 November 2002CS 201J Fall Stack and Heap Review public class Strings { public static void test () { StringBuffer sb = new StringBuffer ("hello"); } static public void main (String args[]) { test (); } sb java.lang.StringBuffer “hello” Stack Heap B 2

5 November 2002CS 201J Fall Garbage Heap public class Strings { public static void test () { StringBuffer sb = new StringBuffer ("hello"); } static public void main (String args[]) { while (true) test (); } “hello” Stack Heap “hello”

5 November 2002CS 201J Fall Garbage Collection System needs to reclaim storage on the heap used by garbage objects How can it identify garbage objects? How come we don’t need to garbage collect the stack?

5 November 2002CS 201J Fall Mark and Sweep

5 November 2002CS 201J Fall Mark and Sweep John McCarthy, 1960 (first LISP implementation) Start with a set of root references Mark every object you can reach from those references Sweep up the unmarked objects What are the root references? References on the stack.

5 November 2002CS 201J Fall public class Phylogeny { static public void main (String args[]) { SpeciesSet ss = new SpeciesSet (); … (open file for reading) while (…not end of file…) { Species current = new Species (…name from file…, …genome from file…); ss.insert (current); } public class SpeciesSet { private Vector els; public void insert Species s) { if (getIndex (s) < 0) els.add (s); }

5 November 2002CS 201J Fall Stack Top of Stack Bottom of Stack Phylogeny.main “Duck” “CATAG” root: Species name: genome: String[]: args ss: SpeciesSet “in.spc” els: current: Species “Goat” “CAGTG” name: genome: “Elf” “CGGTG” name: genome: “Frog” “CGATG” name: genome: SpeciesSet.inser t this: SpeciesSet s: Species public class Phylogeny { static public void main (String args[]) { SpeciesSet ss = new SpeciesSet (); while (…not end of file…) { Species current = new Species (…name…, …genome…); ss.insert (current); } } public class SpeciesSet { private Vector els; public void insert Species s) { if (getIndex (s) < 0) els.add (s); }

5 November 2002CS 201J Fall Stack Top of Stack Bottom of Stack Phylogeny.main “Duck” “CATAG” root: Species name: genome: String[]: args ss: SpeciesSet “in.spc” els: current: Species “Goat” “CAGTG” name: genome: “Elf” “CGGTG” name: genome: “Frog” “CGATG” name: genome: SpeciesSet.inser t this: SpeciesSet s: Species public class Phylogeny { static public void main (String args[]) { SpeciesSet ss = new SpeciesSet (); while (…not end of file…) { Species current = new Species (…name…, …genome…); ss.insert (current); } } public class SpeciesSet { private Vector els; public void insert Species s) { if (getIndex (s) < 0) els.add (s); } After els.add (s)…

5 November 2002CS 201J Fall Stack Top of Stack Bottom of Stack Phylogeny.main “Duck” “CATAG” root: Species name: genome: String[]: args ss: SpeciesSet “in.spc” els: current: Species “Goat” “CAGTG” name: genome: “Elf” “CGGTG” name: genome: “Frog” “CGATG” name: genome: SpeciesSet.inser t this: SpeciesSet s: Species public class Phylogeny { static public void main (String args[]) { SpeciesSet ss = new SpeciesSet (); while (…not end of file…) { Species current = new Species (…name…, …genome…); ss.insert (current); } } public class SpeciesSet { private Vector els; public void insert Species s) { if (getIndex (s) < 0) els.add (s); } SpeciesSet.insert returns…

5 November 2002CS 201J Fall Stack Bottom of Stack Phylogeny.main “Duck” “CATAG” root: Species name: genome: String[]: args ss: SpeciesSet “in.spc” els: current: Species “Goat” “CAGTG” name: genome: “Elf” “CGGTG” name: genome: “Frog” “CGATG” name: genome: public class Phylogeny { static public void main (String args[]) { SpeciesSet ss = new SpeciesSet (); while (…not end of file…) { Species current = new Species (…name…, …genome…); ss.insert (current); } } public class SpeciesSet { private Vector els; public void insert Species s) { if (getIndex (s) < 0) els.add (s); } Finish while loop… Top of Stack

5 November 2002CS 201J Fall Stack Bottom of Stack Phylogeny.main “Duck” “CATAG” root: Species name: genome: String[]: args ss: SpeciesSet “in.spc” els: “Goat” “CAGTG” name: genome: “Elf” “CGGTG” name: genome: “Frog” “CGATG” name: genome: public class Phylogeny { static public void main (String args[]) { SpeciesSet ss = new SpeciesSet (); while (…not end of file…) { Species current = new Species (…name…, …genome…); ss.insert (current); } } public class SpeciesSet { private Vector els; public void insert Species s) { if (getIndex (s) < 0) els.add (s); } Garbage Collection Top of Stack

5 November 2002CS 201J Fall Stack Bottom of Stack Phylogeny.main “Duck” “CATAG” root: Species name: genome: String[]: args ss: SpeciesSet “in.spc” els: “Goat” “CAGTG” name: genome: “Elf” “CGGTG” name: genome: “Frog” “CGATG” name: genome: Garbage Collection Top of Stack Initialize Mark and Sweeper: active = all objects on stack

5 November 2002CS 201J Fall Stack Bottom of Stack Phylogeny.main “Duck” “CATAG” root: Species name: genome: String[]: args ss: SpeciesSet “in.spc” els: “Goat” “CAGTG” name: genome: “Elf” “CGGTG” name: genome: “Frog” “CGATG” name: genome: Garbage Collection Top of Stack active = all objects on stack while (!active.isEmpty ()) newactive = { } foreach (Object a in active) mark a as reachable (non-garbage) foreach (Object o that a points to) if o is not marked newactive = newactive U { o } active = newactive

5 November 2002CS 201J Fall Stack Bottom of Stack Phylogeny.main “Duck” “CATAG” root: Species name: genome: String[]: args ss: SpeciesSet “in.spc” els: “Goat” “CAGTG” name: genome: “Elf” “CGGTG” name: genome: “Frog” “CGATG” name: genome: Garbage Collection Top of Stack active = all objects on stack while (!active.isEmpty ()) newactive = { } foreach (Object a in active) mark a as reachable (non-garbage) foreach (Object o that a points to) if o is not marked newactive = newactive U { o } active = newactive

5 November 2002CS 201J Fall Stack Bottom of Stack Phylogeny.main “Duck” “CATAG” root: Species name: genome: String[]: args ss: SpeciesSet “in.spc” els: “Goat” “CAGTG” name: genome: “Elf” “CGGTG” name: genome: “Frog” “CGATG” name: genome: Garbage Collection Top of Stack active = all objects on stack while (!active.isEmpty ()) newactive = { } foreach (Object a in active) mark a as reachable (non-garbage) foreach (Object o that a points to) if o is not marked newactive = newactive U { o } active = newactive

5 November 2002CS 201J Fall Stack Bottom of Stack Phylogeny.main “Duck” “CATAG” root: Species name: genome: String[]: args ss: SpeciesSet “in.spc” els: “Goat” “CAGTG” name: genome: “Elf” “CGGTG” name: genome: “Frog” “CGATG” name: genome: Garbage Collection Top of Stack active = all objects on stack while (!active.isEmpty ()) newactive = { } foreach (Object a in active) mark a as reachable (non-garbage) foreach (Object o that a points to) if o is not marked newactive = newactive U { o } active = newactive

5 November 2002CS 201J Fall Stack Bottom of Stack Phylogeny.main “Duck” “CATAG” root: Species name: genome: String[]: args ss: SpeciesSet “in.spc” els: “Goat” “CAGTG” name: genome: “Elf” “CGGTG” name: genome: “Frog” “CGATG” name: genome: Garbage Collection Top of Stack active = all objects on stack while (!active.isEmpty ()) newactive = { } foreach (Object a in active) mark a as reachable (non-garbage) foreach (Object o that a points to) if o is not marked newactive = newactive U { o } active = newactive

5 November 2002CS 201J Fall Stack Bottom of Stack Phylogeny.main “Duck” “CATAG” root: Species name: genome: String[]: args ss: SpeciesSet “in.spc” els: “Goat” “CAGTG” name: genome: “Elf” “CGGTG” name: genome: “Frog” “CGATG” name: genome: Garbage Collection Top of Stack active = all objects on stack while (!active.isEmpty ()) newactive = { } foreach (Object a in active) mark a as reachable foreach (Object o that a points to) if o is not marked newactive = newactive U { o } active = newactive sweep () // remove unmarked objects on heap

5 November 2002CS 201J Fall Stack Bottom of Stack Phylogeny.main “Duck” “CATAG” root: Species name: genome: String[]: args ss: SpeciesSet “in.spc” els: “Goat” “CAGTG” name: genome: “Elf” “CGGTG” name: genome: “Frog” “CGATG” name: genome: After main returns… Top of Stack active = all objects on stack while (!active.isEmpty ()) newactive = { } foreach (Object a in active) mark a as reachable foreach (Object o that a points to) if o is not marked newactive = newactive U { o } active = newactive sweep () // remove unmarked objects on heap

5 November 2002CS 201J Fall Stack Bottom of Stack “Duck” “CATAG” name: genome: “in.spc” els: “Goat” “CAGTG” name: genome: “Elf” “CGGTG” name: genome: “Frog” “CGATG” name: genome: Garbage Collection Top of Stack active = all objects on stack while (!active.isEmpty ()) newactive = { } foreach (Object a in active) mark a as reachable foreach (Object o that a points to) if o is not marked newactive = newactive U { o } active = newactive sweep () // remove unmarked objects on heap

5 November 2002CS 201J Fall Problems with Mark and Sweep Fragmentation: free space and alive objects will be mixed –Harder to allocate space for new objects –Poor locality means bad memory performance Caches make it quick to load nearby memory Multiple Threads –One stack per thread, one heap shared by all threads –All threads must stop for garbage collection

5 November 2002CS 201J Fall Stop and Copy Solves fragmentation problem Copy all reachable objects to a new memory area After copying, reclaim the whole old heap Disadvantages: –More complicated: need to change stack and internal object pointers to new heap –Need to save enough memory to copy –Expensive if most objects are not garbage

5 November 2002CS 201J Fall Generational Collectors Observation: –Many objects are short-lived Temporary objects that get garbage collected right away –Other objects are long-lived Data that lives for the duration of execution Separate storage into regions –Short term: collect frequently –Long term: collect infrequently Stop and copy, but move copies into longer-lived areas

5 November 2002CS 201J Fall Reference Counting What if each object kept track of the number of references to it? If the object has zero references, it is garbage!

5 November 2002CS 201J Fall Referencing Counting T x = new T (); The x object has one reference y = x; The object x references has 1 more ref The object y pre references has 1 less ref }Leave scope where x is declared The object x references has 1 less ref

5 November 2002CS 201J Fall Reference Counting class Recycle { private String name; private Vector pals; public Recycle (String name) { this.name = name; pals = new Vector (); } public void addPal (Recycle r) { pals.addElement (r); } } public class Garbage { static public void main (String args[]) { Recycle alice = new Recycle ("alice"); Recycle bob = new Recycle ("bob"); bob.addPal (alice); alice = new Recycle ("coleen"); bob = new Recycle ("dave"); } “Alice” name: pals: refs: 1 “Bob” name: pals: refs: 1 2

5 November 2002CS 201J Fall Reference Counting class Recycle { private String name; private Vector pals; public Recycle (String name) { this.name = name; pals = new Vector (); } public void addPal (Recycle r) { pals.addElement (r); } } public class Garbage { static public void main (String args[]) { Recycle alice = new Recycle ("alice"); Recycle bob = new Recycle ("bob"); bob.addPal (alice); alice = new Recycle ("coleen"); bob = new Recycle ("dave"); } “Alice” name: pals: refs: 1 “Bob” name: pals: refs: 1 2 “Coleen” name: pals: refs: 1

5 November 2002CS 201J Fall Reference Counting class Recycle { private String name; private Vector pals; public Recycle (String name) { this.name = name; pals = new Vector (); } public void addPal (Recycle r) { pals.addElement (r); } } public class Garbage { static public void main (String args[]) { Recycle alice = new Recycle ("alice"); Recycle bob = new Recycle ("bob"); bob.addPal (alice); alice = new Recycle ("coleen"); bob = new Recycle ("dave"); } “Alice” name: pals: refs: 1 “Bob” name: pals: refs: 1 0 0

5 November 2002CS 201J Fall Circular References class Recycle { private String name; private Vector pals; public Recycle (String name) { this.name = name; pals = new Vector (); } public void addPal (Recycle r) { pals.addElement (r); } } public class Garbage { static public void main (String args[]) { Recycle alice = new Recycle ("alice"); Recycle bob = new Recycle ("bob"); bob.addPal (alice); alice.addPal (bob); alice = null; bob = null; } “Alice” name: pals: refs: 1 “Bob” name: pals: refs: 1 2 2

5 November 2002CS 201J Fall Reference Counting Summary Advantages –Can clean up garbage right away when the last reference is lost –No need to stop other threads! Disadvantages –Need to store and maintain reference count –Some garbage is left to fester (circular references) –Memory fragmentation

5 November 2002CS 201J Fall Java’s Garbage Collector Mark and Sweep collector Before collecting an object, it will call finalize Can call garbage collector directly: System.gc () protected void finalize() throws Throwable

5 November 2002CS 201J Fall Garbage in, Garbage out? class Recycle { private String name; private Vector pals; public Recycle (String name) { this.name = name; pals = new Vector (); } public void addPal (Recycle r) { pals.addElement (r); } protected void finalize () { System.err.println (name + " is garbage!"); } } public class Garbage { static public void main (String args[]) { Recycle alice = new Recycle ("alice"); Recycle bob = new Recycle ("bob"); bob.addPal (alice); alice = new Recycle ("coleen"); System.gc (); bob = new Recycle ("dave"); System.gc (); } > java Garbage First collection: Second collection: alice is garbage! bob is garbage!

5 November 2002CS 201J Fall Testing Strategy The applicant's source code and documentation shall be reviewed to verify that the software conforms to the documentation and that the documentation is sufficient to enable the Division to design and conduct all tests at any level of the software structure to verify that the software meets the requirements and objectives of its design specification. Either the division staff or the ITA, which will conduct software qualification tests, shall witness compilation of the source code into baseline object code. The baseline object code and the object code tested must be identical. The object code tested and certified must be identical to the object code released. A copy of the baseline object code will be retained by the Division and used to verify that the baseline code, tested code, and the release code are identical. Once the Division has conducted a review of system software and documentation, test plans shall be designed to exercise all system functions controlled by software under nominal load and data conditions and throughout the range of conditions for which performance is claimed. The first phase of Florida Software Qualification Testing is designed to evaluate the system software’s functionality, and to establish baselines for the system being tested. … Florida Voting Systems Standard,

5 November 2002CS 201J Fall Election Day How much do you trust the programs in the voting machines to record your vote correctly? How much do you trust the programs that tabulate the votes to count them correctly? Hint #1: These programs were probably written in C. Hint #2: These programs were probably not written by Libertarians. Hint #3: The authors of these programs didn’t pass CS201J and weren’t able to get more interesting, high-paying programming jobs.

5 November 2002CS 201J Fall Charge In Java: be happy you have a garbage collector to clean up for you In C: need to deallocate storage explicitly –Why is it hard to write a garbage collector for C? (next class) In the real world: clean up after yourself and others! Garbage Collectors (COAX, Seoul, 18 June 2002)