String [] Memory Usage

I declare a String array as:

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

How much will both of these arrays be produced in memory and performance if they contain only 2 elements.

+3
source share
7 answers

You can check it out 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. 

Thus, you can spend 72 bytes of memory. If your memory costs $ 10 per GB, you can spend 0.000072 cents on memory. However, a second of your time can cost 2 cents. In this case, itโ€™s literally not worth spending a millisecond, worrying about it in this case. those. the time it takes to record 20 instead of 2 costs a lot more.

+3
source

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

See here: How to calculate memory usage in a Java array

+3
source

using the new keyword, you define the array associated with its size in memory, so it depends on you whether you store the value in all or some of them. if you need to be dynamic, then prefer that the arrarialist use the list object as dynamic, while the array was a predefined type, so after that it will not reduce or increase its size, for which you need to create a new array object with its new size

+2
source

The effect will be negligible, since all you do is store additional 3 and 18 null links respectively. Storing an additional null reference does not increase a significant amount of memory and does not affect the performance of your code noticeably.

However, if you know in advance how many elements you are going to store, there seems to be no need for 5 or 20 arrays of elements.

+2
source

It depends on the size of these two elements.

0
source

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

Now the memory used by the elements is a different story.

0
source
 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 
0
source

All Articles