Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 21: Garbage Collection."— Presentation transcript:

1 David Evans http://www.cs.virginia.edu/evans CS201j: Engineering Software University of Virginia Computer Science Lecture 21: Garbage Collection

2 13 November 2003CS 201J Fall 20032 Menu Stack and Heap Mark and Sweep Stop and Copy Reference Counting Java’s Garbage Collector

3 13 November 2003CS 201J Fall 20033 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

4 13 November 2003CS 201J Fall 20034 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 13 November 2003CS 201J Fall 20035 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”

6 13 November 2003CS 201J Fall 20036 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?

7 13 November 2003CS 201J Fall 20037 Mark and Sweep

8 13 November 2003CS 201J Fall 20038 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.

9 13 November 2003CS 201J Fall 20039 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 (/*@non_null@*/ Species s) { if (getIndex (s) < 0) els.add (s); }

10 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 (/*@non_null@*/ Species s) { if (getIndex (s) < 0) els.add (s); }

11 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 (/*@non_null@*/ Species s) { if (getIndex (s) < 0) els.add (s); } After els.add (s)…

12 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 (/*@non_null@*/ Species s) { if (getIndex (s) < 0) els.add (s); } SpeciesSet.insert returns…

13 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 (/*@non_null@*/ Species s) { if (getIndex (s) < 0) els.add (s); } Finish while loop… Top of Stack

14 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 (/*@non_null@*/ Species s) { if (getIndex (s) < 0) els.add (s); } Garbage Collection Top of Stack

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 13 November 2003CS 201J Fall 200324 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

25 13 November 2003CS 201J Fall 200325 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

26 13 November 2003CS 201J Fall 200326 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

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

28 13 November 2003CS 201J Fall 200328 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

29 13 November 2003CS 201J Fall 200329 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

30 13 November 2003CS 201J Fall 200330 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

31 13 November 2003CS 201J Fall 200331 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

32 13 November 2003CS 201J Fall 200332 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

33 13 November 2003CS 201J Fall 200333 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

34 13 November 2003CS 201J Fall 200334 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

35 13 November 2003CS 201J Fall 200335 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!

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


Download ppt "David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 21: Garbage Collection."

Similar presentations


Ads by Google