Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java: How much memory do different arrays consume

DZone's Guide to

Java: How much memory do different arrays consume

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

Arrays can be large and consume a significant amount of memory. It can me worth choosing the most memory efficient array/collection.

Comparing array sizes

How much space does a `new int[1024]` take compared with an `new Integer[1024]`?
int[] ints = new int[1024];
for (int i = 0; i < ints.length; i++) ints[i] = i;
compared with
Integer[] ints = new Integer[1024];
for (int i = 0; i < ints.length; i++) ints[i] = i;
Note: 1/8th of Integer values will come from the auto-box cache and not use additional memory. All possible Boolean and Byte values are cached.
array size in bytes
32-bit JVM
size in bytes
64-bit JVM
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
In both cases, Java 6 update 26 was used. For the 64-bit JVM of this version, -XX:+UseCompressedOops is the default option which uses 32-bit references. If you use more than 32 GB of memory, you would need to turn this option off, using more memory for the arrays of objects.

The full code

Is available here: MemoryUsageExamplesTest

From http://vanillajava.blogspot.com/2011/07/java-how-much-memory-do-different.html

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}