I have the below declaration in my code:
String[] array1 = new String[];
if array1 has 1.000.000 elements (all strings with 80 characters) how heavy is it? I mean for the RAM memory.
I have the below declaration in my code:
String[] array1 = new String[];
if array1 has 1.000.000 elements (all strings with 80 characters) how heavy is it? I mean for the RAM memory.
The answer is that it depends on many factors:
null
or not,Dynamically created Strings are not interned by default. If you intern them, you may save space, if there are many "equal" Strings in your dataset. But if the flip side that the string pool has storage overheads (it is a big hash table) so if the ratio of equal to non-equal Strings is too small then you waste space rather than saving it.
The point about backing arrays is complicated too. The background is that the split
methods (for example) create String objects that share the original String'scharacter array. If you create lots of substrings of the same original string this can save space. But the flipside is that if you create a small substring of a large string, the small substring can cause the original String's entire backing array to remain reachable.
It's implementation-dependent. Assuming a typical JVM which uses UTF-16 encoding internally, it might be something like this.
1 million elements * 80 characters * 2 bytes = 160 million bytes for the text data.
Add on some overhead for each String's internal data structures (say 16 bytes or so), a reference to each String (say 8 bytes), and a little bit for the array itself (say 12 bytes) and you have:
184,000,012 bytes