This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
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

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "garbage collection" Watch "garbage collection" New topic

garbage collection

Puja Sinha

Joined: May 25, 2011
Posts: 27

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?

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

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: 3151

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
I agree. Here's the link:
subject: garbage collection
It's not a secret anymore!