File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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
at ObjectGarbageCollectionTest1b.main(
Exception in thread "main"
Valentin Crettaz
Gold Digger

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:
[ August 21, 2002: Message edited by: Valentin Crettaz ]

[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.
Valentin Crettaz
Gold Digger

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:
subject: Important Question regarding Garbage Collection
It's not a secret anymore!