📜 ⬆️ ⬇️

Comparison of memory consumption for different data storage structures

Different structures in Java consume different amounts of memory. Therefore, it is very important for us to choose the most efficient data storage method.

What is the difference in memory consumption between the `new int [1024]` and `new Integer [1024]` constructs?

int[] ints = new int[1024];
for (int i = 0; i < ints.length; i++) ints[i] = i;

Integer[] ints = new Integer[1024];
for (int i = 0; i < ints.length; i++) ints[i] = i;


Note: 1/8 of Integer values ​​will be cached and do not consume extra memory. All values ​​of types Boolean and Byte will also be cached.
')
StructureJVM 32-bit (size in bytes)JVM 64-bit (size in bytes)
new BitSet(1024)168168
new boolean[1024]10401040
new Boolean[1024]41124112
new ArrayList<Boolean>(1024)41364136
new LinkedList<Boolean>() with 10242462424624
new byte[1024]10401040
new Byte[1024]41124112
new ArrayList<Byte>(1024)41364136
new LinkedList<Byte>() with 10242462424624
new char[1024]20642064
new Character[1024]1844818448
new short[1024]20642064
new Short[1024]1844818448
new ArrayList<Character/Short>(1024)1847218472
new LinkedList<Character/Short>() with 10243896038960
new int[1024]41124112
new Integer[1024]1844818448
new float[1024]41124112
new Float[1024]2049620496
new ArrayList<Integer/Float>(1024)1847218472
new LinkedList<Integer/Float>() with 10243896038960
new long[1024]82088208
new Long[1024]1844825616
new double[1024]82088208
new Double[1024]2049628688
new ArrayList<Long/Double>(1024)1847225640
new LinkedList<Long/Double>() with 10243896046128
new String[1024]5246461456
new ArrayList<String>(1024)5248861480
new LinkedList<String>() with 10247297681968


Full code is available here.

ZY Presumably, every JAVA developer should know all this, but if someone does not know, then we smile and wave and read and remember :)

Source: https://habr.com/ru/post/124909/


All Articles