• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on Garbage Collection

 
Ravindranath Chowdary
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,
In the Java Sun site I read the following question.

Question : 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;
...
The answer given for this in the Sun site is...
Answer : There is one reference to the Point object and one to the Rectangle object. Neither object is eligible for garbage collection.


According to my knowledge only two references exists and the point object is eligible for garabage collection.

Can any one please suggest which one is correct.

Thanks,
Ravindra.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12084
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
unless there is more code shown, i don't think you can tell what is elegible for GC. you have no idea from this code what is being done with the reference passed into the Rectangle constructor.
 
Sasha Ruehmkorf
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately there is no more code shown on the sun-website...
I think

a) As there is no (Point, int, int)-Constructor for Rectangle, this code won't compile (sure about this, just tried :-) )

b) If there was such a constructor you would need to know how the Point-class is built internally. If there were two instance-variables associated with a Rectangle-Object e.g.: One point and a Dimension, the given answer could be correct.
 
Sasha Ruehmkorf
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strange, I wasn't able to edit my post...

Forget about b) even under the mentioned circumstances the point-Object should be eligible for GC
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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



If we forget about what the standard Point and Rectangle constructors are and see the code only.

Copy of the Point reference is passed to the Rectangle constructor so that even if you set the Point reference to null, no object is eligible to GC. The reason is obvious, Point would still have active reference.
But we can't be sure what the Rectangle is doing of Point inside.
So we are not able to say it in loud pitch.

Thanks and Regards,
cmbhatt
 
fred rosenberger
lowercase baba
Bartender
Posts: 12084
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

if you make the call to this constructor, where is the active reference to the Point object once you leave said constructor?
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There will be two references and no classes for GC, as there is still an active reference to Rectangle, and we assume an active reference to point within it.

Fred: Would not the reference to p become out of scope at the end of teh code block and thus be removed from the heap?

G
 
fred rosenberger
lowercase baba
Bartender
Posts: 12084
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The point I am trying to make (and i am not sure of this point) is that we don't know what is happening to the Point reference inside the constructor.

if you use this:

and my hacked-together constructor, after you set point to null, i don't see how you have an active reference to the Point object created. I believe that the Point object is now eligible for GC.

But if the constructor DOES save the reference, then the Point object is NOT eligible.

So, i don't think you can answer this question with the code supplied
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually Ravindranath didn't provide code inside the constructor.
We only have supposition that in case the Rectangle constructor keeps the reference to the Point object. Outside the constructor null(ing) the point reference won't make the Point object eligible to Garbage collection.***

EXACTLY. We DON'T KNOW what's going on inside the 'real' constructor, so we have NO IDEA if the reference is saved or not.

Therefore, WE DON'T KNOW if the Point object is elligible or not. If it IS saved, the object is not elligible. If it is NOT saved, the object IS elligible.

You can make any suppositions you want, but really they're just guesses.
[ April 11, 2007: Message edited by: Fred Rosenberger ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12084
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah, shoot.

Chandra, I'm sorry. i thought i was quoting your post, but i guess i hit the 'edit' button by mistake.

PLEASE accept my sincere apology. it was an accident, and i take full responsibility.

So, everyone, the above post by Chandra is really my reply to what he said.
the quoted part is (i believe) what he originally said, and the messge is my reply.
[ April 11, 2007: Message edited by: Fred Rosenberger ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright Fred,

Ultimately we came to real conclusion:
"GIVE US WHAT IS HAPPENING INSIDE THE Rectangle constructor"
then only we can say whether Point is eligible for GC or not!




Regards,
cmbhatt
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
according to me,

point=null makes point eligible for GC.
As java passes by value
call to
Rectangle r=new Rectanle(point,2,3);
Here point's reference's copy is sent.but point reference itself is of no use after this stmt.

thnx
 
Rambabu Gonela
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ravindra

Here point object reference is using in rectangle Object, so its not eligible for GC.
 
Sasha Ruehmkorf
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So we can say:

Practically: The Point object will be eligible for the gc as it is very unlikely (to say the least) that the constructor of the Point-class constructs an object with a still active reference to the assigned Point object

Theoretically: As we are not 100% sure about the implementation of classes in the Java-API, the question is not decidable.

In either case: The given answer on the sun-website is wrong.

Anyone acclaim?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic