Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

question about eligibility for GC  RSS feed

 
kwame Iwegbue
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following is a question from "learning Java tutorial" on sun.com:



Question 2: The following code creates one Point object and one Rectangle object. How many references to those objects exist after the code executes? Is either object eligible for garbage collection?

...
Point point = new Point(2,4);
Rectangle rectangle = new Rectangle(point, 20, 20);
point = null;
...

Answer 2: There is one reference to the Point object and one to the Rectangle object. Neither object is eligible for garbage collection.



My question...How can this be?! I thought since point is set to null, there is no longer a reference to Point object!!!



(Can see full question with this link)
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes point reference variable is set to null, but the point object is still used by the Rectangle object. In other words rect object has a reference to it. So it's not eligible for GC.
[ July 27, 2005: Message edited by: Srinivasa Raghavan ]
 
kwame Iwegbue
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Srini,

..didn't see the point reference in Rectangle constructor!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We really don't know what Rectangle did with that Point. It might have kept a reference, cloned it or just copied out the x and y values. The Point object might be eligible. What do you think?
[ July 27, 2005: Message edited by: Stan James ]
 
Steve Simon Joseph Fernandez
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can be sure about Rectangle - it's not gonna disappear - but not about point

_steve.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan is right. The answer could even depend on the version of the runtime environment you are using!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!