Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
deepa durai
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
findOut(x);
Integer y = new Integer(99);
Integer z = y;
z = null;
findOut(y);
//here
}
public void findOut(Integer y){
y = null;

}

a)0
b)1
c)2
d)3
e)4
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
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sandipan chakrabarti
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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®ards
krishna
scjp1.4
scwcd1.4
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic