Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L."— Presentation transcript:

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

2 Outline USC CSCI 201L2/8 ▪G▪Garbage Collection

3 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 201L3/8 Garbage Collection

4 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 201L4/8 Garbage Collection

5 Garbage Collection Steps ▪Step 1 – Marking ›The garbage collector determines which pieces of memory are in use and which are not USC CSCI 201L5/8 Garbage Collection Image Source: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

6 Garbage Collection Steps ▪Step 2 – Normal Deletion ›Garbage collector removes unreferenced objects leaving referenced objects and pointers to free space USC CSCI 201L6/8 Garbage Collection Image Source: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

7 Garbage Collection Steps ▪Step 2a – Deletion with Compacting ›Garbage collector deletes unreferenced objects and compacts the remaining referenced objects USC CSCI 201L7/8 Garbage Collection Image Source: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

8 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 201L8/8 Garbage Collector


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

Similar presentations


Ads by Google