3

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?

adarshr
  • 61,315
  • 23
  • 138
  • 167
Russell
  • 795
  • 11
  • 18
  • See http://stackoverflow.com/questions/52353/in-java-what-is-the-best-way-to-determine-the-size-of-an-object – Reddy Jan 17 '12 at 12:38
  • 1
    You could just measure. But let's say each is 1k, larger than what you show here. That's 5M. IMO not worth worrying about unless you're running out of memory. Are you? (There may not be a need to recap a four-sentence question.) – Dave Newton Jan 17 '12 at 12:38
  • you can get rough estimate by looking inside the String source. String stores its contents in character array and each character is 2 bytes in java. There are few more fields inside String. – Reddy Jan 17 '12 at 12:42
  • If you want the actual total, a profiler such as VisualVM (which comes free with the JDK) can tell you this without having to calculate it. – Peter Lawrey Jan 17 '12 at 13:49

5 Answers5

17

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks, so I should just find how much space each string will take up in the array and multiply it by 5,000. I'll do that now and re post. – Russell Jan 17 '12 at 12:40
  • 2
    @Russell: Well, not really - because it depends on whether those strings are shared or not. – Jon Skeet Jan 17 '12 at 12:42
  • It'll should run on a medium end computer. I've used your facts and it'll run just fine. (the program when done would then take up half gig or so) – Russell Jan 17 '12 at 12:43
  • Each reference can take 80 bytes? Is that a typo for `(N * 8 + 20)` or is it really that big? – OpenSauce Jan 17 '12 at 12:48
1

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?

Anish Mohile
  • 183
  • 1
  • 1
  • 10
1

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.

Jerome L
  • 607
  • 5
  • 11
  • I'd be a little wary about making a blanket statement like that; on embedded devices your algorithmic/storage mechanisms may very well be driven by device limitations. – Dave Newton Jan 17 '12 at 12:41
1

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.
NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

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.

Bohemian
  • 412,405
  • 93
  • 575
  • 722