Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
Swapna Anand
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
QUESTION 1:
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.
Ans:
x5 = null;
x5 = x4;
QUESTION 2:

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

Thanks,
Swapna.
 
Nicky Eng
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arulkumar Gopalan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FOR Q1:
-------

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?

FOR Q2:
-------
As posted above.
[ August 01, 2005: Message edited by: Arulkumar Gopalan ]
 
Arulkumar Gopalan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For Q1:
I don't think there might be a necessity to write such redundant code in practical.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic