This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
First off since "name" is not explicitly initialized to null I will assume these are class level variables that are implicitly initialized ;-)
Now, a new object is created when newName is assigned "Nick" but it is immediately dereferenced since newName is assigned to another string object "Jason". The "Nick" object is eligible for GC since it is in a dereferenced state. name is assigned to new String object with value "Frieda" but the reference is transferred to newestName so it will not be dereferenced when name is assigned null.. so "Frieda" is not eligible for GC since it can still be used using the newestName reference ;-)
Originally posted by HS Singh: How many objects are eligible for garbage collection once execution has reached the line labeled Line A?
From everything I've read they don't get garbage collected, but the do a heck of a job optimizing the "pool".
I did my best to try and "fill up the pool" from simple string concatenation in an infinite loop to random character generation and assembly into Strings. I then tried to watch a performance monitor to see if the memory usage climbed. The CPU would spike but after a very "small" initial memory consumption (...may not even be related to the string pool")it leveled off and remained steady even though it was still "looping". So in the backgroud it is either cleaning up after itself or doing some form of optimization with substrings. [ May 26, 2005: Message edited by: Byron Estes ]