Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collector Question-1 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 Collector Question-1" Watch "Garbage Collector Question-1" New topic

Garbage Collector Question-1

Chintan B Shah
Ranch Hand

Joined: Sep 23, 2008
Posts: 83
Hi all,

I am having hard time understanding the concept of garbage collection.

I will throw in a few examples in different posts and will justify my answer.

To begin with, here is first example.


Q: Which are true about the objects created within main(), and eligible for garbage collection when //do stuff line is reached

A. Three objects were created
B. Four objects were created
C. Five objects were created -This is correct. Objects created are da(array object), da[0], dz of da[0], d, dz of d.
D. Zero objects are eligible for garbage collection
E. One object is eligible for garbage collection.
F. Two objects are eligible for garbage collection. This is right answer as per book and I am not able to understand why. The explanation is not very clear. It just says second Dozen object(and its "int array object") are not reachable.
G. Three objects are eligible for garbage collection.-According to my understanding(and I could be wrong), this is right answer as d and da[1] is explicitly made null and if d is made null, dz of d should also be available for GC (even though it has a valid reference to an array object..island of isolation?). Actually, as per my understanding, I think that even dz of da[1] will also be available for GC (island of isolation?), so according to me 4 objects are available for GC, however there was no option of 4 objects hence I selected this option .

I will post other example once I understand this perfectly.

Appreciate any help.


SCJA 1.0
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
when d is made null it simply means that d now refers to no one....the object that was earlier refferred by d is still there and being referred by da[1]..till now nothing is eligible for gc.....only when da[1] is made null the object and its array object referred by a instance variable becomes eligible for gc as no one can now obtain them

clear ?

Chintan B Shah
Ranch Hand

Joined: Sep 23, 2008
Posts: 83
This question is from K&B book (SELF -TEST SECTION CHAPTER-3).

Thanks for reply Ankur. I got it now. I was confusing "references available for GC"(I know there is no such concept) with "objects available for GC".

Your explanation made it clear.

I agree. Here's the link:
subject: Garbage Collector Question-1
It's not a secret anymore!