Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

garbage collection

 
Puja Sinha
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please refer to the following code and let me know how may objects are created and how many objects are eligible for garbage collection:

Which two are true about the objects created within main(), and eligible for garbage collection
when line 12 is reached?

Regards,
Puja
 
Pranav Raulkar
Ranch Hand
Posts: 73
Android Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Puja, I think it goes like this

Line 6 creates 3 objects as "new" keyword is used. (Total objects = 3, available for GC = 0)
Line 7 creates a new object (again "new" keyword used), but since assigned to d[0], object previously held by d[0] is now "available" for GC (Total objects = 3, available for GC = 1)
Line 8 creates a new object (Total objects = 4)
Line 9 assigns object created at line 8 to d[1], so again previous object at d[1] is available for GC (Total objects = 3, available for GC = 2)
Line 10 d is assigned null, so its available for GC too (Total objects = 2, available for GC = 3)
Line 11 d[1] = d but d is already null (Total objects = 2, available for GC = 3)

Whats say you?
 
Shaikh Ali
Ranch Hand
Posts: 51
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question has been asked and discussed many times. Please see this link for one such discussion.
 
Puja Sinha
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Faraz! That was a good thread. I got the answer
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a general tip- lot of times the SCJP related doubts would have been raised and solved before. You might get faster results when you will SearchFirst
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic