Domanda

How much space does a java string array take up? More specifically, how much space (in bytes) does a string array that looks something like this:

Longbowman 
kingdom   
lord    
weapon1,weapon2,weapon3    
armor1,armor2,armor3

I ask this because I'm going to create a program that has 5,000+ of these arrays and want to know how much space it'll take up so I'll know if I should rework the data storage.

So to recap, how much space does a string array (if its quantifiable) take up?

È stato utile?

Soluzione

The string array is just an array of references - an array of size N will take approximately (N * 4 + 20) or (N * 8 + 20) bytes depending on the size of a reference in your JVM.

If you're interested in your total storage, you should work out how many separate String objects you've got, and also how many arrays you've got. If you've got 5000 arrays but they mostly contain references to the same few strings, it's likely to be fine. If you've got 5000 arrays each of which contains 5 strings which aren't used anywhere else, that's 25,000 strings... which still probably isn't very much (a string of length 20 will probably take about 60 bytes).

Of course, context matters here: what's your code going to run on? If it's running on a desktop PC, than taking a few megs probably isn't a problem... it might be more of an issue on a phone.

Altri suggerimenti

String arrays can take as much as you want? you define the size of the String array. String[] abc = new String [80]. But i suppose you already know this.. maybe i did not get your question.

1 char = 2 bytes not 4 bytes since Java uses 16 bit unicode - is this what you are looking for?

Integer.MAX_VALUE or available heap ? maybe the max size available?

It's not easy to answer since a String is a complex object with many fields. It's not only a matter of how many chars there are. And it also depends on how much memory is available on your system. On a 64GB RAM server is it a problem ? No. And on a mobile phone ? Yes. Too many variables may influence the answer.

You are driving your code by optimization and that's not the way it should be. You should drive your code by what you functionally want to do.

The only way to accurately measure memory usage is to use a memory profiler.

I've written a simple program that allocates 5,000 arrays that match your description. I've then measured its memory usage with YourKit.

The amount of memory used by the arrays varied by a factor of ten:

  1. If all arrays use the same string literals, in total they take up about 200KB of RAM.
  2. If each array contains unique randomly-generated strings (of the same lengths as in the first case), they take up about 2MB of RAM.

Jon Skeet is correct (as virtually always), but you may be able to greatly reduce your memory usage by using references to enums and EnumSets instead of the "untyped" String representations of your objects you are proposing.

Using typed data, especially enums, is also good coding practice.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top