Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Important Question regarding Garbage Collection

 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is: Why is there no Garbage Collection after
b1 = null; ???

Result: Compiles and runs with:
C:\Java\EigeneJavaProgramme>java ObjectGarbageCollectionTest1b
Object1 Object2
Object2 Object2

WHY ist there now a Garbage Collection after
b1= null??

public class ObjectGarbageCollectionTest1b {
int i;
ObjectGarbageCollectionTest1b(int i) {this.i=i;} //Constructor
// String toString() {return "Object"+ i;}
public String toString() {return "Object"+ i;}

public static void main (String[] argv) {
ObjectGarbageCollectionTest1b b1 = new ObjectGarbageCollectionTest1b(8);
ObjectGarbageCollectionTest1b b2 = new ObjectGarbageCollectionTest1b(2);
b1 = null;
// b1 = b2;
System.out.println("Object1 "+b1.toString());
System.out.println("Object2 "+b2.toString());
}
}
Result: Compilation Error:
C:\Java\EigeneJavaProgramme>java ObjectGarbageCollectionTest1b
java.lang.NullPointerException
at ObjectGarbageCollectionTest1b.main(ObjectGarbageCollectionTest1b.java:14)
Exception in thread "main"
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First there is no way to force garbage collection and this is the most important thing to know about Java garbage collection.
The only way to see when your object is being garbage collected is to override the finalize() method and put a trace there. The following thread provides a solution:
http://www.coderanch.com/t/238828/java-programmer-SCJP/certification/GC
[ August 21, 2002: Message edited by: Valentin Crettaz ]
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tom:
Important thing to note here is that garbage
collection is about objects (such as Object8 and Object2
in your example) not about references (b1 and b2).
Initially, b1 was referencining Object8, then null, then Object2. In the end, b1 and b2
both referenced Object2. The object Object8 is not
being referenced by anything in the end and there
fore it is candidate for GC. It is possible that it is
GCed by the time program was printing. The
outcome of the print statements do not prove (
disprove) that Object8 is GCed.
Thanks
Barkat
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to know what the gc is doing just run the java interpreter with the -verbose:gc switch.
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a good link that discusses garbage collection.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic