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, dz of da, 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 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 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.
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..till now nothing is eligible for gc.....only when da 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
Chintan B Shah
Joined: Sep 23, 2008
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".