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?

share|improve this question

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 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 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 at 13:49
feedback

5 Answers

up vote 11 down vote accepted

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.

share|improve this answer
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 at 12:40
2  
@Russell: Well, not really - because it depends on whether those strings are shared or not. – Jon Skeet Jan 17 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 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 at 12:48
@OpenSauce: Typo, fixed thanks. – Jon Skeet Jan 17 at 12:53
feedback

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.
share|improve this answer
feedback

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.

share|improve this answer
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 at 12:41
feedback

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.

share|improve this answer
feedback

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?

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.