The technique to measure object size you are using is just too crude: it gives you the size of only the String
instance itself and not of all the other instances it refers to—the size of its entire object graph. Existing tools use the crude call you have used, combined with reflection to walk the rest of the object graph, calling the same method in each step.
Specifically, the String
stores character data in a char[]
, whose object size is the most relevant to the overall memory occupied by a String
, for any longer string.