File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem with GC exercises 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 "Problem with GC exercises" Watch "Problem with GC exercises" New topic

Problem with GC exercises

Federico Er

Joined: Jun 22, 2010
Posts: 8
Hi all,

i've been really bothered by these to exercises (10 and 11 from the book's third chapter), i just can't get it.

The answer is that 5 objets were created and 2 are elegible for GC, being "the second Dozens object (and its "int array" object" the ones that are not reachable. But instantiating a Dozens object does not create the int array, that's just an instance variable. Im i right? I know i must be wrong, but this is bothering me, i can't figure out why the int array counts as a reachable object here.

And the next exercise

In this case, there is only 1 object elegible for GC, because "there is still a reference to a2 (sure) and a2.b2 (sure!)", but here is the thing, "What might be less clear is that you can stil access the other Beta object through the static variable a2.b1 -because it's static". So a2.b1 it's static, but what does it have to do with a1 and b1, they where both set to null!

Sorry to bother, it seems i can't quite grasp this subject, if someone can give me a hand or at least point me to a good garbage collection tutorial i will be really thankfull.
Prateek Rawal
Ranch Hand

Joined: Dec 19, 2009
Posts: 90
An object becomes eligible for GC if it is not referenced by any reference variable.

Q.10 you have written incomplete, there is one more line:

da[1] = null;

See, each Dozen object has a reference variable to an int array object containing elements 1,2, on
Thus when you create a Dozen object, an int array object containing elements 1,2, on also get created and the reference to that array object lies in the dozen object as 'dz'.

Had the definition of class Dozens would have been something like this

then when you create a dozen object, no array object would have been created, ofcourse there would be an int array reference in each Dozen object but it would have been initialized to null(Default value of object-type instance variable).

So, now i hope you might have understood how 5 objects were created.
Now as da[1] = null and d = null
the second Dozen object becomes unreachable, and as it becomes unreachable, the array object pointed by reference variable inside it also becomes unreachable....

Thus 2 objects become unreachable and thus eligible for GC

See, imagine a class like this:

Now when i create an object of this class,
UnderstandStatic u1 = new UnderstandStatic();
it has a primitive variable num with value 0(default value),
when i create another object of this class
UnderstandStatic u2 = new UnderstandStatic();
and write something like:
u2.num = 5;
Now when i print u1.num, what do you think would get printed?

5 will get printed.

A static variable is a class variable. It is shared by all objects of the class.

Now coming back to your question,
when we have written
a1.b1 = b1;
we tell Class Variable b1 to point to that b1 object.
Now a2.b1 would also point to b1 object.

More precisely there is no a1.b1 and a2.b1, there is just an object reference variable of type Beta named as b1 and it is pointed to a Beta Object.
Thus a1.b1 and a2.b1 point to the same object.
Now, even if we can't access that object by b1 and a1.b1, we can still refer it by a2.b1, thus the object is reachable and thus not eligible for GC,,,

Hope you might have understood.......

Waiting for enlightenment....
Federico Er

Joined: Jun 22, 2010
Posts: 8
Prateek, THANKS!

You were very clear in your explanation in both exercises,
so i understand both issues now,

Prateek Rawal
Ranch Hand

Joined: Dec 19, 2009
Posts: 90
Refer this image link:
GC Graphical View

This will give you a very clear picture,

See variable b1 of each Alpha object(a1 and a2),being static it is shared.

Now consider each of the objects(1,2,3,4)

As a1 = null
we can't access (4) thus it is eligible for GC,

Now b2 = null,
but we can access (2) through a2.b2, thus it is not eligible for GC

Now (3) is still accessible as a2 is not null, thus it is also not eligible for GC

Now see (1), although b1 = null and a1 = null,
it can't be accessed through a1.b1 and b1,
but it can still be accessed by a2.b1, thus it is also not eligible for GC

Thus only single object is eligible for GC i.e. (4)

Happy SCJP!
I agree. Here's the link:
subject: Problem with GC exercises
It's not a secret anymore!