Prabhakar Bokka wrote:In the first case all the strings created before the last statement are became orphan. Since they are not assigned to any variables they are eligible for Garbage Collection.
In the second case all the strings are pointing to some variables so, they are not eligible for Garbage Collection.
Hope you got the reason why first case is optimum.
Janeice DelVecchio wrote:Rob -
I like your example.
In the first example, how is the "old a" ever able to be GC?
I was under the impression that string literals hung out in the pool forever..... of course I'm incredibly dense when it comes to GC and strings.....
Rauhl Roy wrote:
kemp may i know the answer to my question?
Rauhl Roy wrote:String literals safely remain in the pool as long as one or more reference is pointing at them. So when the reference to "how" is overwritten in a with a reference to "howare" the older String's milliseconds are numbered.
Unlike most objects, String literals always have a reference to them from the String Literal Pool. That means that they always have a reference to them and are, therefore, not eligible for garbage collection.
Janeice DelVecchio wrote:In the first example, how is the "old a" ever able to be GC?
Krep Lock wrote:Q. Which example is better?
A. Example 1, which uses only String a.
Q. Why o why?
A. Example 1 uses less heap memory. By the time you get to the final line of example 1 you have the following string persisting on the heap:
By the last line of example 2 there are many more strings persisting on the heap:
Prabhakar Bokka wrote:
Is there any size limit to the "String pool"?
If no limit, isn't it occupy the whole heep? (Thinking there are many strings created in many classes in a large application)