aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A  question on Garbage Collection- Can any one  explain 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 "A  question on Garbage Collection- Can any one  explain" Watch "A  question on Garbage Collection- Can any one  explain" New topic
Author

A question on Garbage Collection- Can any one explain

Sanjeev Narula
Greenhorn

Joined: Mar 16, 2007
Posts: 19
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

Joined: Mar 08, 2007
Posts: 1585
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 ...


Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
Andy Morris
Ranch Hand

Joined: May 30, 2004
Posts: 78
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

Joined: Mar 08, 2007
Posts: 1585
Excuse me, but c3 is eligible as well.

Try the code in the IDE you use

Good luck ...
Andy Morris
Ranch Hand

Joined: May 30, 2004
Posts: 78
What object does c3 ever refer to?
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
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

Joined: Oct 02, 2003
Posts: 11229
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
I have been mistaken, so sorry i got it now ...
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

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.


apigee, a better way to API!
Andy Morris
Ranch Hand

Joined: May 30, 2004
Posts: 78
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

Joined: May 30, 2004
Posts: 78
..I'm a bit slow at typing, didn't know you'd both added replies...
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

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

Joined: Mar 08, 2007
Posts: 1585


Life is school, as well as java is

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

Regards ...
Sanjeev Narula
Greenhorn

Joined: Mar 16, 2007
Posts: 19
Thank all of you(Vassili Vladimir,Fred Rosenberger ,Andy Morris,Nitesh Kant) for explaining this question.My Doubt got clear.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A question on Garbage Collection- Can any one explain