8
Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP://WWW-SCF.USC.EDU/ ~CSCI201 USC CSCI 201L

Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Embed Size (px)

Citation preview

Page 1: Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Garbage CollectionCSCI 201L

Jeffrey Miller, Ph.D.

HTTP://WWW-SCF.USC.EDU/~CSCI201

USC CSCI 201L

Page 2: Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Outline

USC CSCI 201L 2/8

▪ Garbage Collection

Page 3: Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Garbage Collection

▪ Java doesn’t require much memory management› C++ has malloc, free, delete, etc.

▪ Since Java does not have these key words, another mechanism was needed to clean up memory after it was used› When a variable goes out of scope, the Garbage Collector

will come around and delete that reference in memory

▪ Garbage collection looks at the heap and identifies which objects are in use and which are not› An object is a candidate for garbage collection when it no

longer is referenced by any part of your program

USC CSCI 201L 3/8Garbage Collection

Page 4: Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Garbage Collection Example

▪ When are the following variables candidates for garbage collection?

1 public class Garbage {2 private int num;3 private static int val;4 public Garbage(int num) {5 num++;6 this.num = num;7 val = 10;8 }9 public static void meth() {10 Garbage g = new Garbage(number);11 g.num = 10;12 Garbage.val = 11;13 }14 public static void main(String [] args) {15 int number = 3;16 for (int i=0; i < number; i++) {17 number++;18 }19 meth();20 System.out.println(number);21 }22 }

USC CSCI 201L 4/8Garbage Collection

Page 5: Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Garbage Collection Steps

▪ Step 1 – Marking› The garbage collector determines which pieces of memory are in use and

which are not

USC CSCI 201L 5/8Garbage Collection

Image Source: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

Page 6: Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Garbage Collection Steps

▪ Step 2 – Normal Deletion› Garbage collector removes unreferenced objects leaving referenced objects

and pointers to free space

USC CSCI 201L 6/8Garbage Collection

Image Source: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

Page 7: Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Garbage Collection Steps

▪ Step 2a – Deletion with Compacting› Garbage collector deletes unreferenced objects and compacts the remaining

referenced objects

USC CSCI 201L 7/8Garbage Collection

Image Source: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

Page 8: Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Invoking Garbage Collection

▪ Programmers are not able to explicitly invoke the garbage collector

▪ We can make a suggestion to the JVM to run the garbage collector with

System.gc();

▪ This does not mean that the garbage collector will be run immediately though

▪ More garbage collection information can be found at http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

USC CSCI 201L 8/8Garbage Collector