• 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 ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

How to make the map of object to be garbage collected

Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
I am giving SCJP1.5
I am seriously facing problem in calculating objects which will be garbage collected.
Lets take an example from Devaka's examulator

Question is how many objects are eligible for garbbbage collection after Line1?
P.S:I have typed the code they may be some grammatical mistake please ignore.

My problem is how to make map of objects to be garbbage collected

I am really bugged.
Please help!!!

Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case two objects will be eligible for garbage collection. One created in line 16 and one created in line 17.

Whenever you are trying to deal with such examples.
1: Draw a heap ( a rectangle)

2: draw every object created and the reference variable pointing to it. In this case since the class A has two reference variables a1 and a2 ,they are the class members. every new object created will have these in the object.

3: The is only one explicit reference variable to the object created in first line of main. Rest all are implicit ones.

4: At line 19 , the reference is made null and there is no reference to the object created on line 16. and hence it is eligible since it doesnt have any direct / indirect reference. The object created on line 17 is referenced by a2 variable of the object created in line 16. Since Object(16) is already eligible for GC , object(17) is also eligible.

I know its confusing . Please try to read the "Islands of isolation" from the KB book.

    Bookmark Topic Watch Topic
  • New Topic