Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question on Garbage Collection- Can any one explain

 
Sanjeev Narula
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class CardBoard {
Short story = 5;
CardBoard go(CardBoard cb) {
cb = null;
return cb;
}
public static void main(String[] args) {
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;
// do Stuff
} }
When // doStuff is reached, how many objects are eligible for GC?

Answer is 2. Can any one explain why?

Thanks in advance.
 
Vassili Vladimir
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The first eligible object for GC is c1, for it being set to null explicitly.
The second eligible object is c3, and the reason for that is, after the completion of the statement:

The method itself is returning null, which is being assigned to c3.

You have to note that, it's not c2 that's being assigned to null.

Best of luck ...
 
Andy Morris
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Vassili said is not correct.

Reference variable 'c3' never refers to an object, so there is no concept of an object it refers to being eligible for garbage collection.

'c1' has an attribute 'story' of type Short, so when reference variable c1 is set to null, both the Cardboard AND Short objects become eligible for gc.
[ March 21, 2007: Message edited by: Andy Morris ]
 
Vassili Vladimir
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excuse me, but c3 is eligible as well.

Try the code in the IDE you use

Good luck ...
 
Andy Morris
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What object does c3 ever refer to?
 
Vassili Vladimir
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I say,

SomeClassName objectName=null;

Is not it eligible for GC ?

If not, I will be thankful for some new information

Regards ...
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
c1, c2 and c3 are NOT objects. that's an important point to remember here. they REFER to objects.

after this line:
CardBoard c1 = new CardBoard();

two objects have been created. one is a CardBoard object, and c1 refers to this. within that object, a Short object is created. hence, two.

the c2 line creates two more objects, for a total of 4.

calling c1.go(c2) really does nothing. a copy of c2 is passed in - in other words, we now have a new referece, cb, pointing to the same object as c2. we set that reference to null, which DOES NOT CHANGE what c2 is pointing to. when we return cb, we return a null, so c3 points to nothing. c1 and c2 each still point to different objects, each of which contains an object.

When you set c1 to null, we no longer have a reference to that CardBoard object, so it is available for GC. But since the Short it contains effectivly has no references, it too is available.

Hence, two objects.
 
Vassili Vladimir
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been mistaken, so sorry i got it now ...
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vassili, it will be eligible for garbage collection if you do
SomeObject so = new SomeObject();
so =null;
Then the object created in line 1 will be eligible for garbage collection.
However, if you do
SomeObject so = null;
so =null;
Then there is nothing that is eligible for garbage collection as there is no object created.
This is what Andy is trying to explain.
 
Andy Morris
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing is eligible for gc in that situation - you never created any objects!

'objectName' is just a reference variable, which can can contain the memory address of an object of type 'SomeClassName', but it is nothing more than that. Since you assigned it the value 'null', it doesn't refer to any object.
 
Andy Morris
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
..I'm a bit slow at typing, didn't know you'd both added replies...
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vassili Vladimir:
If I say,

SomeClassName objectName=null;

Is not it eligible for GC ?

No, it is not. when you say "SomeClassName objectName=null", you don't have an object. you have a REFERENCE, and no object has been created.

you have to create the object somewhere, like this:
NOW there is an object somewhere. if you then come down and say
the object you referred to is now available for GC. but the reference variable could still be used, either by creating another new object, or simply assigning it.
 
Vassili Vladimir
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Life is school, as well as java is

Thanks for the nice warning which i have never paid attention to ...

Regards ...
 
Sanjeev Narula
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank all of you(Vassili Vladimir,Fred Rosenberger ,Andy Morris,Nitesh Kant) for explaining this question.My Doubt got clear.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic