Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

1. Overview

In this tutorial, we'll look at the maximum size of an array in Java.

2. Max Size

A Java program can only allocate an array up to a certain size. It generally depends on the JVM that we're using and the platform. Since the index of the array is int, the approximate index value can be 2^31 – 1. Based on this approximation, we can say that the array can theoretically hold 2,147,483,647 elements.

For our example, we're using the OpenJDK and Oracle implementations of Java 8 and Java 15 on Linux and Mac machines. The results were the same throughout our testing.

This can be verified using a simple example:

for (int i = 2; i >= 0; i--) {
    try {
        int[] arr = new int[Integer.MAX_VALUE - i];
        System.out.println("Max-Size : " + arr.length);
    } catch (Throwable t) {
        t.printStackTrace();
    }
}

During the execution of the above program, using Linux and Mac machines, similar behavior is observed. On execution with VM arguments -Xms2G -Xmx2G, we'll receive the following errors:

java.lang.OutOfMemoryError: Java heap space
	at com.example.demo.ArraySizeCheck.main(ArraySizeCheck.java:8)
java.lang.OutOfMemoryError: Requested array size exceeds VM limit
	at com.example.demo.ArraySizeCheck.main(ArraySizeCheck.java:8)
java.lang.OutOfMemoryError: Requested array size exceeds VM limit

Note that the first error is different from the last two. The last two errors mention the VM limitation, whereas the first one is about heap memory limitation.

Now let's try with the VM arguments -Xms9G -Xmx9G to receive the exact maximum size:

Max-Size: 2147483645
java.lang.OutOfMemoryError: Requested array size exceeds VM limit
	at com.example.demo.ArraySizeCheck.main(ArraySizeCheck.java:8)
java.lang.OutOfMemoryError: Requested array size exceeds VM limit
	at com.example.demo.ArraySizeCheck.main(ArraySizeCheck.java:8)

The results show the maximum size is 2,147,483,645.

The same behavior can be observed for byte, boolean, long, and other data types in the array, and the results are the same.

3. ArraySupport

ArraysSupport is a utility class in OpenJDK, which suggests having the maximum size as Integer.MAX_VALUE – 8 to make it work with all the JDK versions and implementations.

4. Conclusion

In this article, we looked at the maximum size of an array in Java.

As usual, all code samples used in this tutorial are available over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Generic footer banner
1 Comment
Oldest
Newest
Inline Feedbacks
View all comments
Comments are closed on this article!