• 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

Object eligible for GC?

 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from the 1.4 practice exam from Mark

Given the following code, how many objects will be eligible for garbage collection on the line with the comment //here

The Correct Answer is
1) 0

A reference passed into a method is passed as if it were a copy of a pointer pointer rather than the actual object. Thus if that reference is assigned to a null it makes no difference to any other copy of that pointer. Thus the code within the method findOut makes no difference to any other references. Although reference z is assigned to null reference y still points to the object so no objects are eligible for garbage collection.

Isn't z available for GC? When the z Integer references y, y doesn't gain a reference to z does it? So z should be eligible for garbage collection. Perhaps there was suppose to be a line for "y=z"?
[ September 12, 2008: Message edited by: Paul Yule ]
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't have any reference, but it is a method parameter which normally lasts until the end of the method, but on the other hand it is set to null, which should automatically make it eligible for GC.

I think the given answer isn't correct.

This might help: http://www.janeg.ca/scjp/gc/eligible.html
[ September 12, 2008: Message edited by: Rusty Shackleford ]
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Rusty is correct.

  • The reference z doesn't point to anything, so we can ignore it.
  • The reference y pointed to an Integer(99).
  • The method calls findOut(x) and findOut(y) are red herrings. Pass-by-value, remember. The fact that the local variable in the findOut method is asigned to null doesn't make any difference to the original reference.
  • The reference x pointed to an Integer(10).
  • So when you get to wherever it is, two Integers value 10 and 99 are available for GC.

  • Rusty: am I anywhere near the right answer?
     
    Ranch Hand
    Posts: 354
    Eclipse IDE Oracle Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    There are only two "objects" in this context: Integers 10 and 99. x, y, z are only references. At "//here" both 10 and 99 have a valid references. I think the answer 0 is correct.
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Agree about two objects; even I worked out they were the Integers 10 and 99.

    You mean they remain valid because they haven't gone out of scope yet?
     
    Rusty Shackleford
    Ranch Hand
    Posts: 490
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yeah, 0 is correct, I was looking at it backwards.
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yes, I think we have got this sorted out. None because they haven't yet gone out of scope.
     
    Ranch Hand
    Posts: 99
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Paul,
    I'm thinking the same thing. Shouldn't z = null; be available for GC? It should, isn't it? What scope is there left for z?

    Originally posted by Paul Yule:
    Isn't z available for GC? When the z Integer references y, y doesn't gain a reference to z does it? So z should be eligible for garbage collection. Perhaps there was suppose to be a line for "y=z"?



    When z Integer references y, y doesn't gain a reference to z? What do you mean?
     
    author
    Posts: 23951
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Hi Paul,
    I'm thinking the same thing. Shouldn't z = null; be available for GC? It should, isn't it? What scope is there left for z?

    When z Integer references y, y doesn't gain a reference to z? What do you mean?



    Keep in mind -- References are *not* eligible for GC. It is the objects that are eligible for GC. So, when z is set equal to y, the object that is previously reachable via y, is now also reachable via z.

    When z is set to null, the object is no long reachable via z, but to be eligible for GC, it must be not reachible. So, is the object that is no longer reachable via z, still reachable?

    Henry
     
    Henry Wong
    author
    Posts: 23951
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Isn't z available for GC? When the z Integer references y, y doesn't gain a reference to z does it? So z should be eligible for garbage collection. Perhaps there was suppose to be a line for "y=z"?



    This statement doesn't make any sense. When you set y to z, z is now refering to the same object as y. That's it -- y and z are pointing to the same object.

    There is no reference from y to z, or z to y, or any reference to any other reference. References point to objects, not references.

    Henry
    [ September 12, 2008: Message edited by: Henry Wong ]
     
    Ls chin
    Ranch Hand
    Posts: 99
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Henry,

    Originally posted by Henry Wong:
    Keep in mind -- References are *not* eligible for GC. It is the objects that are eligible for GC. So, when z is set equal to y, the object that is previously reachable via y, is now also reachable via z.



    Thanks. :-) I was a 'slightly' confused by the reference variable thing. :roll:


    Originally posted by Henry Wong:
    When z is set to null, the object is no long reachable via z, but to be eligible for GC, it must be not reachible. So, is the object that is no longer reachable via z, still reachable?



    I think ~ if there are 2 reference variables pointing to the same object and only one of them is set to null (i.e. z = null), the other reference variable (i.e. y) is still pointing to that object. Hence, the object (= new Integer(99)) will *not* be eligible for GC.

    :-)

    Thanks again!
     
    Paul Yule
    Ranch Hand
    Posts: 230
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Okay, gotcha...thanks henry. Do references not take up any space on the heap? I was of the understanding that the pointer itself would also need to be GC'd. It's just under most circumstances this happened at the same time. Thanks again
     
    Henry Wong
    author
    Posts: 23951
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Paul Yule:
    I was of the understanding that the pointer itself would also need to be GC'd. It's just under most circumstances this happened at the same time.



    If the reference variable is a local variable, then it is part of the stack frame, which will be gone when it goes out of scope.

    If the reference variable is a instance variable, then it will be gone when the object is GC'ed.


    So, to answer you question... yes, it takes up space and needs to be gx'ed, but the GC doesn't have to do anything extra. It is attached to the objects already being GC'ed.

    Henry
     
    reply
      Bookmark Topic Watch Topic
    • New Topic