Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage collection

 
Marcus Green
arch rival
Rancher
Posts: 2813
  • Mark post as helpful
  • send pies
  • 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.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • 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???
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes ifeel it's true only for non-static variables.
[ December 28, 2005: Message edited by: Srinivasa Raghavan ]
 
Karthikeyan Balasubramanian
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • 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
  • 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: 3830
  • Mark post as helpful
  • send pies
  • 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.

 
B Chen
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • 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.
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • 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 ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • 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)
 
cathymala louis
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since all the references are set to null. I think the objects are eligible for garbage collection.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eloquently said, Barry
 
pravin kumar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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
 
Lakshmanan Arunachalam
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would echo the same rationale Steve and Barry outlined above.
 
Vincent Brabant
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • 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 ]
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic