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 question on 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 "question on garbage collection" Watch "question on garbage collection" New topic
Author

question on garbage collection

Praveen Durbha
Greenhorn

Joined: May 16, 2005
Posts: 16
Point point = new Point(2,4);
Rectangle rectangle = new Rectangle(point, 20, 20);
point = null;

How many objects are eligible for collection after the code runs. My answer is the Point object is eligible for garbage collection..but the java tutorial says none..Not sure why.Any thoughts?

Thanks.
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
What version of Java? In 1.4.2 there is no constructor for java.awt.Rectangle that takes (Point, int, int) as arguments.

What does Rectangle do with the point? Does it store the reference or does it create references for the x and y values of the Point? If the former, you must also make rectangle = null.
soumya ravindranath
Ranch Hand

Joined: Jan 26, 2001
Posts: 300
When you pass 'point' to Rectangle( ), a copy of the reference is passed to that constructor. So, the Point object has a reference and hence cannot be garbage collected. Isn't that so ?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The point (no pun intended) is that you do not know whether or not the point object can be garbage collected. Without knowing the implementation of Rectangle we do not know if it holds a reference to the point object. It could, for instance, make a new point object, copy the original point's state into the new point object, and then "let the original point object go". So the correct answer in my opinion is "you cannot say definitely that the point object is elegible for garbage collection".


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Praveen Durbha
Greenhorn

Joined: May 16, 2005
Posts: 16
Thanks for your feedback. This is what I understood from your comments.

It doesn't matter if the orignial reference is set to null..since a copy of the point reference exists and since we are not sure what the copy reference is doing the Point object cannot be garbage collected. So in the Rectangle constructor if the code looks something like this ..then the object is eligible for garbage collection

Rectangle(Point pt,int x,int y){
pt = null; // The copy reference is set to null

}

Rectangle(Point pt,int x,int y){
pt = new Point(); //The copy reference goes off and creates a new
//instance and it has no way of reaching the
//original instance

}

The execution of this code combined with the statement point = null will make the object eligible for garbage collection. Correct?

Thanks.
[ May 23, 2005: Message edited by: Praveen Durbha ]
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
since we are not sure what the copy reference is doing the Point object cannot be garbage collected




No, what Barry said was, since we are not sure what Rectangle is doing with it, we don't know whether it can be collected or not.

Rectangle(Point pt,int x,int y){
pt = null; // The copy reference is set to null

}


Rectangle doesn't necessarily have to set it to null, it could just access the members, save them, and then just not save a reference anywhere to the Point object. Then it would still be eligible to be collected.
Praveen Durbha
Greenhorn

Joined: May 16, 2005
Posts: 16
Understood. So what you are saying is the copy reference can modify the attributes of the original object but as long as the copy reference is not returned to the calling method, the object is still eligible for garbage collection.

[ May 23, 2005: Message edited by: Praveen Durbha ]
[ May 23, 2005: Message edited by: Praveen Durbha ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question on garbage collection
 
Similar Threads
question on garbage collection?
question about eligibility for GC
garbage collection question
Question on Garbage Collection
Garbage Collection in Java