Understanding String memory usage
by, 04-26-2012 at 06:36 PM (1363 Views)
Fields which are present at the String object shall be looked first of all so that for a better understanding of the above calculations:
- a char array: Separate object which consist of the actual characters;
- an integer offset where the string starts;
- String Length
- Another int, for hash code’s cached calculations.
This shows that if no characters are present in a string, even then 4 bytes would be required for sake of the char array reference along with 12 bytes for sake of 2 int fields plus object header’s 8 bytes. This will be providing 24 bytes that is basically multiple of 8 therefore padding bytes wouldn’t be needed. Char array (empty) will further needing 12 bytes along with 4 padding bytes so that memory could be brought to multiple of 16 that has been utilized by char array. Hence 40 bytes will be used by an empty string.