I read a lot about memory allocation for Strings lately and can't find any details if things are the same with Java 8.
How much memory space would a String like "Alexandru Tanasescu"
use in Java 8?
I use the 64bit version.
An empty String takes 40 bytes—enough memory to fit 20 Java characters.
Java strings are physically stored in UTF-16BE encoding, which uses 2 bytes per code unit, and String.
If all arrays use the same string literals, in total they take up about 200KB of RAM. If each array contains unique randomly-generated strings (of the same lengths as in the first case), they take up about 2MB of RAM.
By default, a string buffer has a 16 character capacity. By calling this method, we can calculate its current capacity. Capacity refers to the total number of character storage size in a string buffer.
Minimum String memory usage :
(bytes) = 8 * (int) ((((no chars) * 2) + 45) / 8)
So
80 = 8 * (int) ((((19) * 2) + 45) / 8)
Understanding String memory usage (SOURCE)
To understand the above calculation, we need to start by looking at the fields on a String object. A String contains the following:
- a char array— thus a separate object— containing the actual characters;
- an integer offset into the array at which the string starts;
- the length of the string;
- another int for the cached calculation of the hash code.
This means even if the string contains no characters, it will require 4 bytes for the char array reference, plus 3*4=12 bytes for the three int fields, plus 8 bytes of object header. This gives 24 bytes (which is a multiple of 8 so no "padding" bytes are needed so far).
Then, the (empty) char array will require a further 12 bytes (arrays have an extra 4 bytes to store their length), plus in this case 4 bytes of padding to bring the memory used by the char array object up to a multiple of 16. So in total, an empty string uses 40 bytes.
If the String
contains, say, 19 characters, then the String object itself still requires 24 bytes. But now the char array requires 12 bytes of header plus 19*2=38 bytes for the seventeen chars. Since 12+38=50 isn't a multiple of 8, we also need to round up to the next multiple of 8 (56). So overall, our 19-character String
will use up 56+24 = 80 bytes.
Java 8 does not have the
offset
andlength
anymore. Onlyhash
and theCharArray
.
@Thomas Jungblut
So, in Java8 the way to calculate memory for strings remains same but you must subtract 8 bytes less due to the missing offset
and length
.
If you look at the Oracle Java 8 sources, you have:
A char value[]
and an int hash
. A char
is 2 bytes, and an int
is 4 bytes.
So wouldn't the answer be yourstring.length
* 2 + 4?
No. Every object had overhead. An array stores its dimensions, for example. And both the array (an object) and the string will incur extra memory from the garbage collector storing information about them.
There is no reliable way to calculate this, because AFAIK each JRE and JDK has no obligation to the size of object overhead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With