Muhammad Imran Tariq
Muhammad Imran Tariq

Reputation: 23352

String[] Memory Usage

I am declaring a String array as:

String[] items1 = new String[5];
String[] items2 = new String[20];

How much effect both of these arrays will produce on memory and performance if both contain only 2 elements.

Upvotes: 3

Views: 791

Answers (7)

Peter Lawrey
Peter Lawrey

Reputation: 533442

You can test this yourself.

public static void main(String... args) {
    long free1 = free();
    String[] two = new String[2];
    long free2 = free();
    String[] twenty = new String[20];
    long free3 = free();
    if (free3 == free1) System.err.println("You need to use -XX:-UseTLAB");
    System.out.println("String[2] took " + (free1 - free2) + " bytes and String[20] took " + (free2 - free3) + " bytes.");
}

private static long free() {
    return Runtime.getRuntime().freeMemory();
}

prints

String[2] took 24 bytes and String[20] took 96 bytes.

So you could be wasting 72 bytes of memory. If your memory cost $10 per GB, you could be wasting 0.000072 cents of memory. However, a second of your time could be worth 2 cents. In this case it is literally not worth spending a milli-second worrying about it in this case. i.e. The time it takes to write 20 instead of 2 is worth far, far more.

Upvotes: 3

korifey
korifey

Reputation: 3509

Consumed_HEAP(items1) = (POINTER_SIZE*5) + Consumed_HEAP(items1[0])+Consumed_HEAP(items1[1]) + ARRAY_METADATA
Consumed_HEAP(items2) = (POINTER_SIZE*20) + Consumed_HEAP(items2[0])+Consumed_HEAP(items2[1]) + ARRAY_METADATA
POINTER_SIZE = x86 ? 4 : x64 ? 8 : ...
Consumed_HEAP(str) = str.length() * 2 + SOME_JVM_SPECIFIC_HEAP_FOR_STRING_REFERENCE_TYPE (~ 16-20) 

SOME_JVM_SPECIFIC_HEAP_FOR_STRING_REFERENCE_TYPE contains of all String fields:
1. reference from string to char array
2. memoized hash
3-n. other fields
n+1. link to class object

Upvotes: 0

codejitsu
codejitsu

Reputation: 3182

A single-dimension array is a single object. As expected, the array has the usual object header. However, this object head is 12 bytes to accommodate a four-byte array length. Then comes the actual array data which, as you might expect, consists of the number of elements multiplied by the number of bytes required for one element, depending on its type. The memory usage for one element is 4 bytes for an object reference; for a list of the memory usage of primitive types, see the page on Java data types. If the total memory usage of the array is not a multiple of 8 bytes, then the size is rounded up to the next mutlitple of 8 (just as for any other object).

See here: How to calculate the memory usage of a Java array

Upvotes: 3

Adam Zalcman
Adam Zalcman

Reputation: 27233

The effect will be negligible since all you're doing is storing extra 3 and 18 null references respectively. Storing an extra null reference does not increase memory footprint significantly nor does it affect performance of your code noticeably.

However, if you know beforehand, how many elements you're going to store, there seems to be no need for 5 or 20 elements arrays.

Upvotes: 2

Pratik
Pratik

Reputation: 30855

using with the new key word you define the array associated with it's size into the memory so it depend on you whether you store value in all or some them. if you need for dynamic then prefer the arraylist using list object as dynamic while the array was predefined type so after that it will not reduce or increase it's size for that you have to create new array object with it's new size

Upvotes: 2

soulcheck
soulcheck

Reputation: 36767

Both contain only references to strings so memory used by String[5] with two elements is equal to memory used by String[5] with 0 elements. The same goes for String[20].

Now memory used by the elements is a different story.

Upvotes: 0

Boris Pavlović
Boris Pavlović

Reputation: 64622

It depends on the size of those 2 elements.

Upvotes: 0

Related Questions