Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

garbage collection

 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many objects are eligible for garbage collection after executing line 7?
view source print

public class Tester {

public static void main(String[] args) {

Integer x = new Integer(3000);

Integer y = new Integer(4000);

Integer z = new Integer(5000);
Object a = x;

x = y;

y = z;

z = null; //line 7

}

}here the answer is zero i don know how..anybody kindly explain..according to me the answer should be 3...
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how 3? 0 is correct
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the answer is zero...after line 7 a refers to the first first object....x to 2nd....y to 3rd...so everyone has a reference
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
curve karve wrote:How many objects are eligible for garbage collection after executing line 7?
view source print

public class Tester {

public static void main(String[] args) {

Integer x = new Integer(3000);

Integer y = new Integer(4000);

Integer z = new Integer(5000);
Object a = x;

x = y;

y = z;

z = null; //line 7

}

}here the answer is zero i don know how..anybody kindly explain..according to me the answer should be 3...


Ok, I set up a little graph, lets see if I can recreate it:

Three objects are created. For ease, I call them ojb1, obj2, obj3, in order of creation. Left column is the objects, right is the variable referencing them, through the line Object a = x:

Objects|references
obj1 |x,a
obj2 |y
obj3 |z

Now we assign x the same reference as y, so our graph changes to this:

Objects|references
obj1 |x,a
obj2 |y,x
obj3 |z

Now we assign y the same reference as z, so our graph changes to the following:

Objects|references
obj1 |x,a
obj2 |y,x
obj3 |z,y

Now we assign z null, so our graph changes to the following:

Objects|references
obj1 |x,a
obj2 |y,x
obj3 |z,y

Even though z is pointing to nothing, all created objects have active references. Therefore, none of them are eligible for GC.
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yep right zero is right 3 is wrong everyone has a reference


well that graph thing is really great can be used to solve any GC question thanks for it W.Joe Smith
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


HTH

Minhaj
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahesh Chitale wrote:yep right zero is right 3 is wrong everyone has a reference


well that graph thing is really great can be used to solve any GC question thanks for it W.Joe Smith


There are other ways to do it, especially with really complicated GC problems, but this one was simple enough that my graph worked well
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well can you tell us the other methods which may be requied in SCJP
because these GC questions are really a headache
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey thanks i got it....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic