• 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

question on garbage collection

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
 
Praveen Durbha
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic