Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Devak ExamLab - Garbage Collection

 
rouven gorsky
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
ExamLab Creator
Marshal
Pie
Posts: 4682
333
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read this topic.
 
rouven gorsky
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4682
333
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoa!

You are correct. I'll correct this as soon as possible!
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
sudipto shekhar
Ranch Hand
Posts: 826
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rouven gorsky please QuoteYourSources
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15485
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sudipto shekhar wrote:rouven gorsky please QuoteYourSources

He already clearly mentioned that this is from Devaka's ExamLab practice exam.
 
sudipto shekhar
Ranch Hand
Posts: 826
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:
He already clearly mentioned that this is from Devaka's ExamLab practice exam.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic