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 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 "Garbage Collection" Watch "Garbage Collection" New topic

Garbage Collection

Swapna Anand

Joined: May 11, 2005
Posts: 7
Hi All,

I have been a regular reader of javaranch. Thanks all for the great work.
Please let me know of some good material for Garbage collection. Also tell
me how this works....question from K&B
X3 x2 = new X3();
X3 x3 = new X3();
X3 x5 = x3;
x3 = x2;
X3 x4 = x3;
x2 = null;
//insert code
what two lines of code inserted independently at line 18 wil make an object eligible for GC.
x5 = null;
x5 = x4;

class A {
private B b; ..............//1
void test(){
b=new B();
void demo(B bb) {
bb = null;
bb = new B();
When is the B object at 1 eligible for garbage colelction.

Nicky Eng
Ranch Hand

Joined: Mar 26, 2005
Posts: 378
for the question 2, i think the object in the indicated line 1 will not be eligible for garbage collection until all the process been done. Because it there is a method still using the object b, and eventually life time of object b is not determined.

From NickyEng
Diploma in Computer Studies
SCJP 1.4
Formula 1 app by Maxis (Playbook)
Arulkumar Gopalan
Ranch Hand

Joined: Oct 13, 2003
Posts: 104

After the execution of above code:

- X4 & X3 references are pointing to the object created by x2(X3 x2 = new X3() )
- X5 reference is pointing to the object created by x3(X3 x3 = new X3() )

so, any one of the below statements frees an object for GC.
x5 = null;
x5 = x4;
x5 = x3; //is this statement not given as an answer?

As posted above.
[ August 01, 2005: Message edited by: Arulkumar Gopalan ]

Anbudan & Mahalo,<br />Arul<br /> <br />-Not a sun certified Java professional :-)
Arulkumar Gopalan
Ranch Hand

Joined: Oct 13, 2003
Posts: 104
For Q1:
I don't think there might be a necessity to write such redundant code in practical.
I agree. Here's the link:
subject: Garbage Collection
jQuery in Action, 3rd edition