简体   繁体   中英

Java memory use in string[] vs many strings

Can anyone tell me from the perspective of memory usage what's better, one string array (string[]) that holds 100 Strings or 100 Strings.

I want to know whats the better approach when memory issues are critical.

It depends what kind of memory you are talking about and what you mean 100 string not in an array.

Lets start with what we can say for sure.

  1. If we assume that the strings are created the same way, then the actual representations of the strings will occupy the same amount of heap space in either case.

  2. The 100 strings themselves are likely to occupy orders of magnitude more space than the String[] with 100 elements, or the 100 individual String variables.

The difference is in how we hold the references to the strings.

  • If they are held in a String[] , the array object will take 100 heap words for the array elements plus 3 or 4 heap words for the array header and padding.

  • If they are held in as instance fields of an object, that takes 100 heap words for the fields. (Plus possibly the object's header + padding, if that isn't accounted otherwise. That will be 2 or 3 heap words.)

  • If they are held in as static fields, then that takes 100 heap words. (Plus possibly the hidden "statics" object's header + padding ... if that isn't accounted otherwise.)

  • If they are held as local variables, then no heap space is required. Instead the references will occupy 100 words of stack space.


I want to know whats the batter approach when memory issues are critical.

As you can see, the difference overall is small, and probably too small to be significant.

And, in practice, you also would need to take account of the difference in code size in using an array versus using 100 individual variables. That will depend on how the application uses the strings. Indeed, the code size differences could easily swamp the differences above.

My advice is to not bother with this line of thinking. Use whichever representation gives you the most readable and maintainable code. A difference of 1 or 2 machine words is unlikely to make a significant difference, even if you need to represent millions of separate groups of strings.

Java objects have a 8 byte header, and in case of single dimensional arrays, there is a 4 byte overhead to accomodate the array length.

For example, if you create 7 Strings, you are using 7 * 8 bytes = 56 bytes overhead for the objects. If you accomodate them in a single dimensional array, you would add 8 bytes for the array object + 4 bytes for the array length, and thus you will have 56 bytes + 12 bytes = 78 bytes overhead.

In case of multidimensional arrays, in Java they are a set of arrays, and therefore every row of a multidimensional array has the overhead of an object.

The String[] uses slightly more memory, specifically to create an array reference with a length property. However, we're talking about a very small amount of difference, and I would recommend whichever solution is easier to read (which I assume is the array).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM