• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

garbage collection

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how 3? 0 is correct
 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


HTH

Minhaj
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey thanks i got it....
 
reply
    Bookmark Topic Watch Topic
  • New Topic