| Author |
Devak ExamLab - Garbage Collection
|
rouven gorsky
Greenhorn
Joined: Feb 10, 2009
Posts: 14
|
|
I need your help:
The following is from Mr. Devaka's ExamLab SCJP Simulator :
Practice Exam 1, question 42:
How many objects are eligible for the Garbage Collector, after executing the line-9 of this programm. F.Y.I: Array is an object.
1. public class Gabc{
2. Gabc ob[];
3. public Gabc(Gabc... gb){
4. ob=gb;
5. }
6. public static void main(String args[]){
7. Gabc bc=new Gabc(new Gabc(),new Gabc());
8. bc.ob[1].ob=new Gabc[]{new Gabc(),bc.ob[0]};
9. bc.ob[0]=null;
10. System.gc();
11. }
12. public void finalize(){
13. System.out.println("-GCed-");
14. }
15. }
According to ExamLab the answer is: A 0, means at that point no Object is eligible for the gc.
What I think is that after executing line 8, there is one Object eligible for the gc: The Object, that was referred before by
bc.ob[1].ob - an array of length 0. So shouldn't the answer be: B 1?
Thanks for your clarification.
|
 |
Devaka Cooray
Saloon Keeper
Joined: Jul 29, 2008
Posts: 2691
|
|
|
Please read this topic.
|
 |
rouven gorsky
Greenhorn
Joined: Feb 10, 2009
Posts: 14
|
|
Of course have I read the topic before and reread it. But it doesn't help. After lots of thinking I am pretty shure that the object - after execution of line 7 - referred by bc.ob[1].ob is eligible for gc when code-line 8 is done. You can proofe me wrong by telling me the reference to his object after line 8.
It is true that there will never ever be an output (generated from the finalize method) because the mentioned object is an Gabc[] of length 0. But this doesn't proof - even in case the gc would always run when envoking System.gc() - that there's no Object at all eligible for gc. It only shows that there's no Gabc object eligible for gc.
Anyone agrees?
|
 |
M Srilatha
Ranch Hand
Joined: Aug 27, 2008
Posts: 137
|
|
hi rouven,
Yeah you are right!
when i saw the code before reading your view, i thought there wont be any objects available for GC.
But as you said, there will be one available for GC (array object of length 0 which was previously referenced by bc.ob[1].ob).
This looks like a tricky question. I guess as the constructor accepts var-args as parameter, when there is no parameter passed, its creating an array of length 0.
Lets see what others have to say!
|
Thanks,<br />Srilatha M
|
 |
Devaka Cooray
Saloon Keeper
Joined: Jul 29, 2008
Posts: 2691
|
|
Whoa!
You are correct. I'll correct this as soon as possible!
|
 |
Stephen Davies
Ranch Hand
Joined: Jul 23, 2008
Posts: 352
|
|
Rouven, could you please use code tags, it is quite a challenge to read code as complex as this is, especially if not formatted clearly!
Many Thanks
|
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
|
 |
sudipto shekhar
Ranch Hand
Joined: Apr 02, 2008
Posts: 813
|
|
|
rouven gorsky please QuoteYourSources
|
Regards, Sud.
SCJP 5 ScjpFAQ JLS
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12911
|
|
He already clearly mentioned that this is from Devaka's ExamLab practice exam.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
sudipto shekhar
Ranch Hand
Joined: Apr 02, 2008
Posts: 813
|
|
Jesper Young wrote:
He already clearly mentioned that this is from Devaka's ExamLab practice exam.
|
 |
 |
|
|
subject: Devak ExamLab - Garbage Collection
|
|
|