• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Garbage Collection -K&B book

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Refer to Q12 K&B book Chapter 3-

A. After line 8 runs.
B. After line 9 runs.
C. After line 10 runs.
D. After line 11 runs.
E. Compilation fails.
F. Never in this program.
G. An exception is thrown at runtime.
Answer
I feel that the answer should be B. After line runs isn't e3 eligible for garbage collection. As e3 is set to null at line 9 and at line 6 e3.e =null, so e 3 should be eligible after line 9. Can somebody explain.
Thanks.
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this type of question, the safest way to do it with a pencil and paper and draw out the whole thing instead doing it in the head.

I don't know if you mistype something, but you said "at line 6 e3.e =null" but I see line 6 as e3.e = e2;

The answer is G. An exception is thrown at runtime is because of line 8 e2 = null; Once you set any object to null, you can't no longer tough any of it's field. In this case e2.e. That causes the NullPointerException to be thrown.

so originally

e2====>object===>e
but once you do e2=null;

all you get is e2 object===>e You can't no loger get to that object.
 
neha verma
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

At what point is only a single object eligible for GC?
Hi,I forgot to modify the code earlier. Please look at the modified code I have moved before we set e2 to null. In the K&B book answers sections it says that with the code above the answer would be

Never in this program

. But I think after line number 10, e3 should be eligible for garbage collection. Please clarify.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Based on your last stated code

"e1.e = e3;"

If e3 is set to null..
the object can't be GCed after line 10 as e1.e refers to this Object??

and after 11th line, all three will be eligible for gc. Tony's approach is the right way to go for such questions
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic