• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Garbage Collection

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 "......
 
lowercase baba
Posts: 13081
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Just let me do the talking. Ahem ... so ... you see ... we have this tiny ad...
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic