File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collection" Watch "Garbage collection" New topic
Author

Garbage collection

Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
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.


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Sep 28, 2004
Posts: 1228
Yes ifeel it's true only for non-static variables.
[ December 28, 2005: Message edited by: Srinivasa Raghavan ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Karthikeyan Balasubramanian
Ranch Hand

Joined: Apr 20, 2005
Posts: 89
I think it is exactly true,Marcus.
Since there is no reference at all for a object,the object is eligible for garbage collection.


SCJP 1.4--93%<br />SCWCD 1.4--86%<br />Next--???
Karthikeyan Balasubramanian
Ranch Hand

Joined: Apr 20, 2005
Posts: 89
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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Dec 27, 2005
Posts: 89
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.


SCJD<br />SCJP 1.4
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

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

Joined: Aug 03, 2002
Posts: 7729
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)


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
cathymala louis
Ranch Hand

Joined: Nov 02, 2005
Posts: 77
Since all the references are set to null. I think the objects are eligible for garbage collection.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
Eloquently said, Barry


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
pravin kumar
Ranch Hand

Joined: Nov 03, 2005
Posts: 30
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

Joined: Nov 02, 2005
Posts: 99
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?


Regards<br />Lakshmanan<br />IBM-OOAD & UML, SCEA-I
B Chen
Ranch Hand

Joined: Dec 27, 2005
Posts: 89
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

Joined: Aug 31, 2004
Posts: 11343

I would echo the same rationale Steve and Barry outlined above.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Vincent Brabant
Ranch Hand

Joined: Nov 01, 2004
Posts: 33
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


Vincent Brabant<br /><a href="http://fr.netbeans.org" target="_blank" rel="nofollow">http://fr.netbeans.org</a>
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

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

Joined: Oct 12, 2004
Posts: 306

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collection
 
Similar Threads
Garbage collection...
Return values of methods
i passed with 81%
gc ??
Tech Word Game