Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Question regarding GC.

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. class Eco {
2. public static void main(String[] args) {
3. Eco e1 = new Eco();
4. Eco e2 = new Eco();
5. Eco e3 = new Eco();
6. e3.e = e2;
7. e1.e = e3;
8. e2 = null;
9. e3 = null;
10. e2.e = e1;
11. e1 = null;
12. }
13. Eco e;
14. }
At what point is only a single object eligible for GC?
[ September 24, 2008: Message edited by: shailesh jamloki ]
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think its after Statement 11 and after statement 11, not 1 but 3 objects are islands-of-isolation and hence eligible for Garbage Collection.
I dont think that just one Object is eligible for GC in the given statements at any point of time.
Although I am not sure about this.
Looking for clarifications/other answers.
 
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The object created at line 4 will be eligible for GC immediately after executing line 9, but while executing line 10, the code will throw NPE )
 
Paul Somnath
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reply to above Post:
After line 9, e1 is still referring to an object. And through that we can reference the object earlier referenced by e3(See Statement 7).
[ September 24, 2008: Message edited by: Paul Somnath ]
 
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
When you post a question on JavaRanch that's copied from a book, mock exam or other source, we require that you quote your sources. So, please tell us where you copied this question from.
 
Paul Somnath
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey!
Nobody to comment on this thread?
Am I completely right?
Bubbling the thread, please some expert comment on this.
 
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code will throw a NullPointerException at line 10 i.e. before any object is eligible for garbage collection..

The memory representation of the code at line 7 is



and at line 9 it looks like this

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Relax Paul!

First: line 10 gives problems, since e2 is null. But the answer is "after line 9", so we can stop the program there

Just after line 7 there are two references to the Eco-object from line 4 (e2 and e3.e) and two references to the Eco-object from line 5 (e3 and e1.e). Eco~3 has one reference from e1.
After line 8 the number of references is 1 for Eco~4 (e3.e) and 2 for Eco~5 (e3 and e1.e). After line 9 e3.e will be an island (not reachable) and thus the number of references is 0 for Eco~4 and 1 for Eco~5 (e1.e).

Remember. Questions about threads = take a piece of paper and draw pools, stacks and arrows.
 
V.G. Hindriksen
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops! I totally missed the reference from the third to the second. Coffee!

Good work and nice drawing, Ankit!
 
Paul Somnath
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya Gotcha Ankit,
e2.e = e1;
creates a problem since the reference e2.e is no longer referenced.
However, the question is after what line exactly one object would be eligible for garbage collection?
Leaving the null-pointer exception, my doubt is cant we reach all the objects after Line 9?
The first object is directly accessible through e1.
Third Object is object through first object.
Second object is accessible via first->third->second.
 
Ankit Garg
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All the objects will be eligible for garbage collection after line 11.
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
he code will throw a NullPointerException at line 10 i.e. before any object is eligible for garbage collection..
but if we remove line 10 then All the objects will be eligible for garbage collection after line 11.
 
Paul Somnath
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that is what I had said in my initial post of this thread.
All three objects would be islands-of-isolation and would be eligible for collection after line 11.
At no point of time in the given code is only one object eligible for garbage collection.
I missed that null-pointer point though!
Original poster is requested to post the given options also since the question becomes ambigious now.
 
shailesh jamloki
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is copied from keithy serra.. 1.5 book.

and here are the answer..

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

G is the correct answer here
 
shailesh jamloki
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And the explaination is given as

G is correct. An error at line 10 causes a NullPointerException to be thrown because
e2 was set to null in line 8. If line 10 was moved between lines 7 and 8, then F would be
correct, because until the last reference is nulled none of the objects is eligible, and once
the last reference is nulled, all three are eligible
 
Paul Somnath
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is why we always say that PLEASE QUOTE YOUR SOURCES.
Had you done that, there wouldn't have been so much discussion on this. The answer was given in 3rd or 4th post.
Please try to post the full question along with the choice answers, so that there is no ambiguity.
Hope you understand.
 
vipin jain
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello shailesh jamloki
As you commented
" If line 10 was moved between lines 7 and 8, then F(Never in this program) would be
correct, because until the last reference is nulled none of the objects is eligible, and once
the last reference is nulled, all three are eligible "

this question answere is F means Never in this program but after the line 11 the last reference is nulled there means all three are eligible .
then how answer Fis correct?

Can you please replay me?
 
Ankit Garg
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what are you doing vipin???
 
reply
    Bookmark Topic Watch Topic
  • New Topic