This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic

Garbage Collection

deepa durai

Joined: May 08, 2007
Posts: 18
i have a doubt in this question..please explain..
Given the following code, how many objects will be eligible for garbage collection on the line with the comment //here
public class BettyAck {
public static void main(String argv[]){
BettyAck b =new BettyAck();
public BettyAck() {
Integer x = new Integer(10);
Integer y = new Integer(99);
Integer z = y;
z = null;
public void findOut(Integer y){
y = null;


i think the answer is 2.
1.y=null means (the reference y and x points nothing or the value of the object 10 becomes null)i think the first one is correct
2.can i say only two objects are created in the above code...(the space allocated only for "10" and "99") (x,y,z are references)
chandra reddy

Joined: Apr 11, 2007
Posts: 10
Answer is 0.Because even the objects are assigned to null , still the thread refering those objects.Thread refer those objects untill the end of scope.
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178

First see how many times 'new' is being called. Here three times for objects b,x and y. In the method call findOut(), you are only passing a copy of the reference to the object. Setting this reference to null will not affect the original object back in the calling method.

Similarly, setting z= null, will not affect the original value of y. z was pointing to y and now it is pointing to nothing.

Therefore, none of the objects are eligible for gc.
ragavendran krishnamoothy

Joined: Feb 13, 2007
Posts: 17
i thing there will be 2 objects in the memory that will be eligible for the garbage collection at the end the method " findOut "......
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11249

it depends on what you mean by "the end o the method findOut". The original question said "how many at the comment?". since the comment is still inside the method (we have not yet hit the closing curly brace), the objects created in the method are NOT yet eligible. but if by "the end of the method" you mean "after leaving the method", then i agree there are two. of course, after you've left the method, the next thing you do is leave main, so the BettyAck object also becomes available.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
sandipan chakrabarti

Joined: Mar 25, 2007
Posts: 6
In java when when a method call happens, that has an Object as a parameter, a copy of , reference to that object, is passed to the method.Therefore
in the method if y = null is done, the object still has a reference that is outside the method, and therefore it cannt be garbage collected
krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185
The Ans is 0!!!

See, when you pass an object reference to a methos, actually you are passing a copy of the reference. though you make it null in the called method, still the object is being reference by the original reference varialble!!!

so, in this sityaltion, No object is eligible for the garbage collection.
refer the garbage collection tutorial in this website.

Thanks&Regards, Krishna.
SCJP1.4, SCWCD1.4, SCBCD 5.0
jQuery in Action, 2nd edition
subject: Garbage Collection