File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about gc() 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 "about gc()" Watch "about gc()" New topic
Author

about gc()

Lakshmi Ram
Greenhorn

Joined: Nov 12, 2000
Posts: 10
hai,
public class Test{
public static void main(String[] args) {
Object a = new Object();
Object b = new Object();
Object c = new Object();
Object d = new Object();
d=c=b=a;
d=null;
}
}

How many objects are eligible for GC after d = null?
can anybody tell this
Thanks

Sridevi Shrikanth
Ranch Hand

Joined: Jan 11, 2001
Posts: 31
three.
Four objects are created.
When the assgn. takes place
d=c=b=a;
After this three Objects are eligible.
When d= null is executed,
the reference to the OBject A is reduced by 1. since b,c and a still have refernces, Object A is not eligible for gc(). At the end of the block, all the four are eligible, but not until then.
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
You don't even need d=null to get 3. With the assignment, d=c=b=a, you have the variable "a" reference pointing to it's original object. One you assigned the other variable reference to the same object as the "a" variable, then they lost reference to their object which makes those objects available for GC.

Output is:
finalize Foo(2)
finalize Foo(3)
finalize Foo(4)
-Peter
Lakshmi Ram
Greenhorn

Joined: Nov 12, 2000
Posts: 10
Hai,
Thank U peter,sridevi.
But ,can u explain it more .I didn't understand it clearly
thanks
SRI PARUCHURI
Greenhorn

Joined: Dec 17, 2000
Posts: 15
Hi,
when the statement d=c=b=a executes,The d,c,b are referenced to 'a' like d->c->b->a,finally all referenced to 'a'.so they lost their old actual positions,because they are nomore referenced to the actual values.so they are garbage collected.That's why peter saying that you don't even need the d is null statement.
I hope this helps.
Sri
nidiya gaspar
Greenhorn

Joined: Jan 31, 2001
Posts: 7
hi,
In case, u need more explanation, then u can read the following. I have tried to explain it in a simple way.
Object a = new Object();
Object b = new Object();
Object c = new Object();
Object d = new Object();
Imagine that the Object reference variable "a" refers to an Object Ob1.
Similarly, "b" refers to an Object Ob2. "c" refers to an Object
Ob3. "d" refers to an Object Ob4.
After the statement, d=c=b=a
"a" still refers to Ob1.
"b" also refers to Ob1.
"c" also refers to Ob1.
"d" also refers to Ob1.
So the objects Ob2, Ob3 and Ob4 are left unreachable(not used). That makes the 3 (Ob2,Ob3,Ob4) Objects eligible for garbage collection.
Hope this will do.
All the best
regards
nidiya
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
Nidiya,
I can't see how I could make it any clearer than you have. Good job!
-Peter
Lakshmi Ram
Greenhorn

Joined: Nov 12, 2000
Posts: 10
Hai sri,nidiya
Thank u all .u did a good job.Now i am clear.
Thanks
Sri Enga
Ranch Hand

Joined: Feb 02, 2001
Posts: 51
mport java.io.*;
class Foo {
int i;
Foo(int i) {
this.i = i;
}
protected void finalize() {
System.out.println("finalize Foo("+i+")");
}
}
public class GC{
public static void main(String[] args) {
Foo a = new Foo(1);
Foo b = new Foo(2);
Foo c = new Foo(3);
Foo d = new Foo(4);
d=c=b=a;
// after assignment b, c, and d don't point to their own objects anymore.
// d = null; // not needed!!!
// Try to get GC to run by calling it a bunch of times.
System.gc();
System.gc();
System.gc();
}
}
Output is:
finalize Foo(2)
finalize Foo(3)
finalize Foo(4)

Should the output not be
finalize Foo(1)
finalize Foo(1)
finalize Foo(1)
Be kind this is my first post!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about gc()
 
Similar Threads
garbage collection
Thread question - please verify my answer
GC
Objects eligible for garbage collection.
Tricky GC