Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

garbage collection fundamentals  RSS feed

 
Charles O'Leary
Ranch Hand
Posts: 185
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Sybex page 36)

An object is no longer reachable when one of two situations occurs:
  • The object no longer has any references pointing to it.
  • All references to the object have gone out of scope.



  • Aren't these essentially the same (single) situation?  Out of scope for the object to be reached is "Out of scope for the object to be reached" and it wouldn't matter if scope was either local, instance, or class, or whether there was one method or more than one method involved.  Correct?

    In other words, if the exam question presented 2 sets of code and you where told that each set of code represented a mutually exclusive "situation", what's your example of how each mutually exclusive "situation"/set of code look different?
     
    João Victor Gomes
    Ranch Hand
    Posts: 110
    11
    Eclipse IDE Java Netbeans IDE Postgres Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Actually, they are different.

    Let's see the following code


    Got it?
     
    Charles O'Leary
    Ranch Hand
    Posts: 185
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    João,
    I would argue that your very examples help to make my own point.  My (possibly ill advised?) position:  I don't see these as mutually exclusive.  Are you suggesting that you see these as mutually exclusive?

    Sybex page 25, car= null; means
    ...reference types can be assigned null, which means they do not currently refer to an object

    The object was clearly referenced prior to car=null.  After car=null, in addition to your suggestion that  'this is an example of "The object no longer has any references pointing to it."  ', I would argue that the car reference to ITS object no longer exists ... the "connection" was broken between the reference and the object  ... reference to the object went out of scope after car = null.  You do see my argument at least, right?  Namely, one situation implies the other situation.

    Succinctly, variable scope is not indicative of garbage collection, right?  Scope discussions are possible with primitives only, not requiring objects at all. 

    Garbage collection represents a dropped call or connection between a reference AND an object.  It does not matter how the call was dropped (home power outage/dead cell battery), either way the result between these two "situations" is the same: the call is dropped/object is now eligible for garbage collection.

    If I'm off base, somebody please get me clear.  With that said, if drawing the distinction between these two "situations" will never appear on the exam, please get me clear on that too and spare me the headache.      
     
    Charles O'Leary
    Ranch Hand
    Posts: 185
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    João,
    The first point is looking at eligibility from right to left, while the second point is looking from left to right.  Correct?
     
    João Victor Gomes
    Ranch Hand
    Posts: 110
    11
    Eclipse IDE Java Netbeans IDE Postgres Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I understand what you are asking. I focused my answer on trying to explain one example of each situation, and now I see that clearly it wasn't what you were expecting, and I apologize for that.
    But, my answer has something that can help me explain my point.

    Charles O'Leary wrote:(Sybex page 36)

    An object is no longer reachable when one of two situations occurs:
  • The object no longer has any references pointing to it.
  • All references to the object have gone out of scope.




  • In my opinion, by "All references to the object have gone out of scope", I think the authors were talking about variable scopes. A reference variable is still a variable, so it has a scope.
    Like in this code

    In the code above, the reference variable car will be out of scope when the method go() ends, so the object will be unreachable.

    But when "All references to the object have gone out of scope", it implies that the object no longer has any references pointing to it, which clashes the two definitions.
    And by "The object no longer has any references pointing to it", I understand it like when we point a reference to null, or another object, and the old object has no reference to it anymore.
    I agree with you, it's a little confusing, since if I point null to a reference variable, this reference variable is still on scope, but it doesn't point to its object anymore.

    So, I don't think that those two definitions that you mentioned are "mutually exclusive".

    Concluding, I think the authors were trying to separate this two concepts, to separately exemplify that an object becomes eligible for GC when the program forces the object to lose its reference, or when the reference variable points to an object, but this variable goes out of scope.
    When we put this two concepts together, they become similar.

    I hope we can keep this nice discussion.
    Maybe we could leave this to a more experienced programmer around here, but I'm here to give my opinion, if you want to.
     
    Charles O'Leary
    Ranch Hand
    Posts: 185
    4
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    João,

    No apologizes necessary.  I've had this on my bucket list of things that I wanted to further investigate prior to the exam.  Thanks for the discussion.  
     
    Charles O'Leary
    Ranch Hand
    Posts: 185
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ?
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!