CSCI 201L Jeffrey Miller PhD httpwwwscfusceducsci201 USC CSCI 201L Outline USC CSCI 201L 2 8 Garbage Collection Garbage Collection Java doesnt require much memory management ID: 434120
Download Presentation The PPT/PDF document "Garbage Collection" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Garbage Collection
CSCI 201Principles of Software Development
Jeffrey Miller, Ph.D.
jeffrey.miller@usc.eduSlide2
Outline
Garbage CollectionSlide3
Garbage Collection
Java doesn’t require much memory managementC++ has
malloc,
free
,
delete
Since Java does not have pointers (or these
key words), another mechanism was needed to clean up memory after it was usedWhen a memory location is no longer referenced, the Garbage Collector will come around and delete that reference in memoryGarbage collection looks at memory and identifies which locations are still referenced by variables and which are notA memory location is a candidate for garbage collection when it no longer is referenced by any part of the program
USC CSCI 201L
3/8
Garbage CollectionSlide4
Garbage Collection Example
When are the memory locations pointed to by each of the following variables candidates for garbage collection?
1 public class Garbage {
2 private
ArrayList
num
;3 private static String val
;
4 public Garbage(ArrayList
num
) {
5
this.num = num;6 val = “hello”;7 }8 public static void meth() {9 Garbage g = new Garbage(new ArrayList());10 g.num = new ArrayList();11 Garbage.val = “hi”;12 }13 public static void main(String [] args) {14 Integer number = new Integer(3);15 for (int i=0; i < number; i++) {16 System.out.print(i);17 }18 meth();19 System.out.println(number);20 }21 }
USC CSCI 201L
4/8
Garbage Collection
Memory
Usage
Program
Execution
Static
Variables
Typical Memory Usage of
Variables during Program ExecutionSlide5
Garbage Collection Steps
Step 1 – MarkingThe garbage collector determines which pieces of memory are in use and which are not
USC CSCI 201L
5
/8
Garbage Collection
Image Source: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.htmlSlide6
Garbage Collection Steps
Step 2 – Normal DeletionGarbage collector removes unreferenced objects leaving referenced objects and pointers to free space
USC CSCI 201L
6
/8
Garbage Collection
Image Source: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.htmlSlide7
Garbage Collection Steps
Step 2a – Deletion with CompactingGarbage collector deletes unreferenced objects and compacts the remaining referenced objects
USC CSCI 201L
7
/8
Garbage Collection
Image Source: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.htmlSlide8
Invoking Garbage Collection
Programmers are not able to explicitly invoke the garbage collectorWe 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/8Garbage Collector