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

Garbage Collection

 
Greenhorn
Posts: 7
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why it is said on a certain book, that object 2 referenced by two is eligible only after line 11, while first obj one is eligible for gc after line 8. It was confusing , this book....

 
Sheriff
Posts: 16635
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If by "object 2 referenced by two" they mean the object created on line 10 then, yes, it will only be eligible after line 11 when that variable goes out of scope and it no longer references the object. The first object (object 1?) created and assigned to two on line 4 will be eligible for gc after line 9.
 
gj sadvib
Greenhorn
Posts: 7
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry i forgot to mention, it was the object on line 4.

A. The Rabbit object from line 3 is first eligible for garbage collection immediately
following line 6.
B. The Rabbit object from line 3 is first eligible for garbage collection immediately
following line 8.
C. The Rabbit object from line 3 is first eligible for garbage collection immediately
following line 12.
D. The Rabbit object from line 4 is first eligible for garbage collection immediately
following line 9.
E. The Rabbit object from line 4 is first eligible for garbage collection immediately
following line 11.
F. The Rabbit object from line 4 is first eligible for garbage collection immediately
following line 12.

it is said the correct answer is b and e  
 
gj sadvib
Greenhorn
Posts: 7
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ahh sorry it was just my mistake i swapped the answers incorrectly. it is said b and d. also thanks
 
Greenhorn
Posts: 18
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lets go step by step :

On Line 3 an object is created whose reference variable is "one" ( ie one is pointing to the Rabbit1 object).
On Line 4 an object is created whose reference variable is "two" ( ie two is pointing to the Rabbit2 object).
On Line 5 we are creating a variable and asking it to point "one"  as "one" is pointing to Rabbit1 it will also point to  Rabbit1 ( Now Rabbit1 has 2 reference variable ie "one" and "three" )
On Line 6 we are asking "one" to point null which means now our Rabbit1 has  only one reference variable ie "three" ( "one" is removed)
On Line 7 we are creating a reference variable "four" and asking it to point "one "  as "one" is pointing nothing/null "four" will also point null.

Remember our Rabbit1 still has a reference variable "three"  and Rabbit2 has "two"
 
On line 8 we are asking variable "three" to point null. And now after this Line we can see that even reference variable "three" is removed from the object and our Rabbit1 has no reference variable pointing it and hence it is available for gc.

On line 9 we are asking variable "two" to point to null and hence after this Line our Object becomes eligible for GC  and on Line 10 "two" is pointing to an another object which confirms that the variable "two" is not pointing to Rabbit2 anymore.

As per my knowledge Rabbit2 is eligible for GC  after  Line 9  irrespective of Line 10 written or not.....



 
 
Marshal
Posts: 74359
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gj sadvib wrote:. . . on a certain book . . .

Please always say where such material comes from, to avoid copyright problems and so everybody else can verify the material.
 
Campbell Ritchie
Marshal
Posts: 74359
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You shou‍ld get some paper and a pencil, and draw little squares with obj1 obj2 obj3 etc in. Then write down the variables and you can draw arrows to each object and see which is still referred to.
 
reply
    Bookmark Topic Watch Topic
  • New Topic