Philip Grove wrote:During analysis of a heap dump I found that the value in my main HashMap is not a substring as put there, but the complete string and an index pointing at the substring. Is it smart enough to reference the same string or does it clone the string, which would result in excess copies of the same string in memory?
Actually, your question has little to do with HashMap and more to do with: Is the result of a
substring() a reference to the same String; and the answer is: not quite.
A substring is a separate String object, but (and I'm almost certain of this, but I'm happy to be corrected if anyone knows better) it shares the
character array of the original String. Thus, it will take whatever space overhead is associated with an object (≈16 bytes I think), plus internal indexes (2 or 3
ints; I forget), plus the reference of the array itself (4/8 bytes).
HIH
Winston
PS: It's also worth noting that
Java characters takes
two bytes, not one. Not sure if you took that into account in your calculations.