|
![]() |
SCJP 5, SCWCD 5
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.
SCJP 5, SCWCD 5
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
When you do things right, people won't be sure you've done anything at all.
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.
luck, db
There are no new questions, but there may be new answers.
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:
"howareyou"
By the last line of example 2 there are many more strings persisting on the heap:
"how"
"are"
"you"
"howareyou"
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
SCJP 5, SCWCD 5
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)
You know it is dark times when the trees riot. I think this tiny ad is their leader:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
|