aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Important Question regarding Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Important Question regarding Garbage Collection " Watch "Important Question regarding Garbage Collection " New topic
Author

Important Question regarding Garbage Collection

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
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

Joined: Aug 26, 2001
Posts: 7610
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 ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
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

Joined: Aug 26, 2001
Posts: 7610
If you want to know what the gc is doing just run the java interpreter with the -verbose:gc switch.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Here's a good link that discusses garbage collection.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Important Question regarding Garbage Collection