Home Java Comparison of memory consumption of different storage structures

Comparison of memory consumption of different storage structures

by admin

Different structures in Java consume different amounts of memory. So it is very important for us to choose the most efficient method of data storage.
What would be the difference in memory consumption between the constructs `new int[1024]` and `new Integer[1024]`?
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/8th of Integer values will be cached and will not eat up unnecessary memory. All Boolean and Byte values will also be cached.

Structure JVM 32-bit(size in bytes) JVM 64-bit(size in bytes)
new BitSet(1024) 168 168
new boolean[1024] 1040 1040
new Boolean[1024] 4112 4112
new ArrayList<Boolean> (1024) 4136 4136
new LinkedList<Boolean> () with 1024 24624 24624
new byte[1024] 1040 1040
new Byte[1024] 4112 4112
new ArrayList<Byte> (1024) 4136 4136
new LinkedList<Byte> () with 1024 24624 24624
new char[1024] 2064 2064
new Character[1024] 18448 18448
new short[1024] 2064 2064
new Short[1024] 18448 18448
new ArrayList<Character/Short> (1024) 18472 18472
new LinkedList<Character/Short> () with 1024 38960 38960
new int[1024] 4112 4112
new Integer[1024] 18448 18448
new float[1024] 4112 4112
new Float[1024] 20496 20496
new ArrayList<Integer/Float> (1024) 18472 18472
new LinkedList<Integer/Float> () with 1024 38960 38960
new long[1024] 8208 8208
new Long[1024] 18448 25616
new double[1024] 8208 8208
new Double[1024] 20496 28688
new ArrayList<Long/Double> (1024) 18472 25640
new LinkedList<Long/Double> () with 1024 38960 46128
new String[1024] 52464 61456
new ArrayList<String> (1024) 52488 61480
new LinkedList<String> () with 1024 72976 81968

Full code available here
Z.I. Presumably any JAVA developer should know all this, but if someone doesn’t, then smile and wave. read and remember 🙂

You may also like