File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Garbage Collection query. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Garbage Collection query." Watch "Garbage Collection query." New topic

Garbage Collection query.

Kirtipriya Jammi

Joined: Sep 21, 2011
Posts: 5
Hi , i have a question on garbage collection...

When line 16 is reached, how many objects will be eligible for garbage collection?

My understanding is attached in the file.
So is the answer 2 ?

[Thumbnail for pic1.png]

Kirtipriya Jammi

Joined: Sep 21, 2011
Posts: 5
ohh that should be "When line 15 is reached" !!
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

I would say 1.

When we reach line 12 we have an object map as in the gc1-line12.png.

When we then de-ref a1, b1 and b2 refs, we only free the a1:Alpha instance, since the b1:Beta and b2:Beta still will have references from the static :Alpha via a2:Alpha.b1 and from a2:Alpha.b2.


The confusing bit of this question is that they use the variable names b1 and b2 in two different scopes. If you rename them as I did on the images, then it is quite clear.

[Thumbnail for gc1-line12.png]

[Thumbnail for gc1-line13.png]

Kirtipriya Jammi

Joined: Sep 21, 2011
Posts: 5
Thank you Ove . !!

the foremost mistake i did was .. i put the static variable Beta b1 inside the Object Alpha.. i realized this after seeing your diagram.

Static variable cannot be an instance variable .. its a class variable .

Now i get the answer 1

I agree. Here's the link:
subject: Garbage Collection query.
It's not a secret anymore!