aspose file tools *
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "garbage collection" Watch "garbage collection" New topic
Author

garbage collection

Puja Sinha
Greenhorn

Joined: May 25, 2011
Posts: 27
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

Joined: Apr 20, 2011
Posts: 73

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

Joined: Jan 26, 2011
Posts: 51
This question has been asked and discussed many times. Please see this link for one such discussion.
Puja Sinha
Greenhorn

Joined: May 25, 2011
Posts: 27
Thanks Faraz! That was a good thread. I got the answer
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3100
    
  33

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


Mohamed Sanaulla | My Blog
 
wood burning stoves
 
subject: garbage collection