• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Devak ExamLab - Garbage Collection

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read this topic.
 
rouven gorsky
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoa!

You are correct. I'll correct this as soon as possible!
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
rouven gorsky please QuoteYourSources
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper Young wrote:
He already clearly mentioned that this is from Devaka's ExamLab practice exam.



 
reply
    Bookmark Topic Watch Topic
  • New Topic