• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Garbage Collector Question-1

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


1.

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.

Thanks
Chintan.
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Regards,
Chintan.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic