• 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

Garbage collection

 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have had the following option on one of my exams for a while and I'd like to get a wider opinion on it. Is the following statement true or false?

An object becomes eligible for garbage collection when all references to it are set to null.
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marcus Green:
I have had the following option on one of my exams for a while and I'd like to get a wider opinion on it. Is the following statement true or false?

An object becomes eligible for garbage collection when all references to it are set to null.



true

Why you are asking??? Is something special here???
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes ifeel it's true only for non-static variables.
[ December 28, 2005: Message edited by: Srinivasa Raghavan ]
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it is exactly true,Marcus.
Since there is no reference at all for a object,the object is eligible for garbage collection.
 
Karthikeyan Balasubramanian
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hii..Marcus..Got one point after reading the question once again..

The statement is FALSE.

Actually the question is trying to test the knowledge of Isolation of References.

The key point is if we set the real reference variable pointing to the object null,although we have instance variables of that class pointing to that object not set to null...The object is automatically eligible for garbage collection.

i think i m right.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by karthikeyan balasubramanian:

Actually the question is trying to test the knowledge of Isolation of References.

The key point is if we set the real reference variable pointing to the object null,although we have instance variables of that class pointing to that object not set to null...The object is automatically eligible for garbage collection.

i think i m right.



Could you please explain this more clearly...

Thanks.

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say false.
Setting the last reference to null is not the only way to make the object eligible for gc. Setting the last reference to another object would also make the original object eligible for gc.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Setting the last reference to null is not the only way to make the object eligible for gc.


Be careful about how you read the questions, especially for the test. The question as posted doesn't ask if it's the only way. It simply asks the following:

An object becomes eligible for garbage collection when all references to it are set to null.

If all references to an object are set to null, then there are no strong references to the object, and the object is therefore eligible for garbage collection.
[ December 28, 2005: Message edited by: Steve Morrow ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following on from Steve, I guess you could say that having no references to an object is a sufficient condition for being eligible for garbage collection, but it is not necessary (as with island of isolated objects, for example)
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since all the references are set to null. I think the objects are eligible for garbage collection.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eloquently said, Barry
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strictly speaking as long as there is no reference ..you could say that object is eligible to gc but still island of isolated objects are the exceptions for it
so i think answer should be false ..as because of above reason
Please correct if am wrong
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say, if an object is not reachable by any part of the code, it will be eligible for GC and will be collected in the next GC cycle(scavenge GC cycle incase of young objects, FULL GC cycle incase of old objects).

Someone has higlighted a term island of isolated objects. Can any one explain me what it means?
 
B Chen
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take for example three objects, A, B, and C. The program references A, A references B, and B references C.

program -> A -> B -> C

When in the program the reference to A no longer exist, objects A, B, and C are no longer accessible by the program and will be eligible for gc.

program A -> B -> C
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would echo the same rationale Steve and Barry outlined above.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to say false,
because you can have Weak References to that object.
And if you have only weak reference to that object, the object is well eligible for garbage collection.

That would be a better formulation:

"An object become eligible for garbage collection when it exists no more strong references pointing to that object"

(after correction of my english :-) )

Vincent
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I would like to say false,
because you can have Weak References to that object.


That doesn't negate the original assertion. Objects that have only weak references can be GC'd (as you stated). But an object that has had all references to it set to null will no longer have any strong references. The object will be the same condition as described in the "better formulation"; both are true.
[ December 29, 2005: Message edited by: Steve Morrow ]
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


An object becomes eligible for garbage collection when all references to it are set to null.



If programmer is setting references to null the answer is false. If the Object being created is from a String Literal, The String Literal Pool will still be referencing it, even if all references from programmer's code is set to null



A string literal will always be refrenced from the String Literal Pool once the String is created. The programmer cannot remove the reference in String Literal pool to the String. The String Literal Pool is maintained internal to JVM. Strings created by "new" are exempt from being in String Literal Pool.

Read the java ranch article referenced below.

The following statement is true


An object becomes eligible for garbage collection when all references to it are null.



http://java.sun.com/developer/technicalArticles/ALT/RefObj/

http://www.javaranch.com/journal/200409/Journal200409.jsp

http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#101083

Thanks for the question.
[ December 29, 2005: Message edited by: jiju ka ]
 
The knights of nee want a shrubbery. And a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic