wood burning stoves 2.0*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "garbage collection" Watch "garbage collection" New topic
Author

garbage collection

maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 186
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

Joined: Oct 30, 2009
Posts: 504
how 3? 0 is correct


SCJP 1.6 96%
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
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


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
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.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
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

Joined: Jan 22, 2007
Posts: 400



HTH

Minhaj


SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
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

Joined: Nov 04, 2009
Posts: 114
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

Joined: Dec 29, 2009
Posts: 186
hey thanks i got it....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: garbage collection