Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to make the map of object to be garbage collected

 
nitude gupta
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • 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!!!

 
Adi Kulkarni
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • 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.

~Aditya
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic