Do Java Arrays Have a Maximum Size

Do Java arrays have a maximum size?

Haven't seen the right answer, even though it's very easy to test.

In a recent HotSpot VM, the correct answer is Integer.MAX_VALUE - 5. Once you go beyond that:

public class Foo {
public static void main(String[] args) {
Object[] array = new Object[Integer.MAX_VALUE - 4];
}
}

You get:

Exception in thread "main" java.lang.OutOfMemoryError:
Requested array size exceeds VM limit

Why I can't create an array with large size?

Theory

There are two possible exceptions:

  • OutOfMemoryError: Java heap space means your array does not fit into java heap space. In order to solve you can increase the maximum heap size by using JVM option -Xmx. Also take into account that the maximum size of object cannot be larger than the largest heap generation.
  • OutOfMemoryError: Requested array size exceeds VM limit means platform-specific size was exceeded:

    • the upper bound limit is set by the restrictions of the size type used to describe an index in the array, so theoretical array size is limited by 2^31-1=2147483647 elements.
    • the other limit is JVM/platform specific. According to chapter 10: Arrays of The Java Language Specification, Java SE 7 Edition there is no strict limit on array length, thus array size may be reduced without violating JLS.

Practice

In HotSpot JVM array size is limited by internal representation. In the GC code JVM passes around the size of an array in heap words as an int then converts back from heap words to jint this may cause an overflow. So in order to avoid crashes and unexpected behavior the maximum array length is limited by (max size - header size). Where header size depends on C/C++ compiler which was used to build the JVM you are running(gcc for linux, clang for macos), and runtime settings(like UseCompressedClassPointers). For example on my linux:

  • Java HotSpot(TM) 64-Bit Server VM 1.6.0_45 limit Integer.MAX_VALUE
  • Java HotSpot(TM) 64-Bit Server VM 1.7.0_72 limit Integer.MAX_VALUE-1
  • Java HotSpot(TM) 64-Bit Server VM 1.8.0_40 limit Integer.MAX_VALUE-2

Useful Links

  • https://bugs.openjdk.java.net/browse/JDK-8059914
  • https://bugs.openjdk.java.net/browse/JDK-8029587

maximum limit on Java array

int[][] adjecancy = new int[96295][96295];

When you do that you are trying to allocate 96525*96525*32 bits which is nearly 37091 MB which is nearly 37 gigs. That is highly impossible to get the memory from a PC for Java alone.

I don't think you need that much data in your hand on initialization of your program. Probably you have to look at ArrayList which gives you dynamic allocation of size and then keep on freeing up at runtime is a key to consider.

There is no limit or restriction to create an array. As long as you have memory, you can use it. But keep in mind that you should not hold a block of memory which makes JVM life hectic.

Why maximum size of an java array is Integer.MAX_VALUE/7?

As mentioned already by cloudworker, the real limits for array are explained here: Do Java arrays have a maximum size?

In your case 1GB is just not enough heap space for array that huge.

I do not know what exact processes are run in JVM, but from what I am able to count:

Integer.MAX_VALUE= ~2 billions
int = 4bytes
2billions*4bytes=8billions bytes = 8GB memory

With 1GB heapspace you should be able to have ~ /8 of MAX_VALUE. (I think that reason that you can actually get more than /8 is some optimization in JVM)

Why the maximum array size of ArrayList is Integer.MAX_VALUE - 8?

Read the above article about Java Memory management, which clearly states

I think this applies to ArrayList as it is the Resizable array implemenation.

Anatomy of a Java array object

The shape and structure of an array object, such as an array of int
values, is similar to that of a standard Java object. The primary
difference is that the array object has an additional piece of
metadata that denotes the array's size. An array object's metadata,
then, consists of: Class : A pointer to the class information, which
describes the object type. In the case of an array of int fields, this
is a pointer to the int[] class.

Flags : A collection of flags that describe the state of the object,
including the hash code for the object if it has one, and the shape of
the object (that is, whether or not the object is an array).

Lock : The synchronization information for the object — that is,
whether the object is currently synchronized.

Size : The size of the array.

max size

2^31 = 2,147,483,648 

as the Array it self needs 8 bytes to stores the size
2,147,483,648

so

2^31 -8 (for storing size ), 

so maximum array size is defined as Integer.MAX_VALUE - 8

What is the maximum length of an array in Go, Java and C#?

The Go Programming Language Specification

Array types

An array is a numbered sequence of elements of a single type, called
the element type. The number of elements is called the length and is
never negative.

The length is part of the array's type; it must evaluate to a
non-negative constant representable by a value of type int.

Numeric types

A numeric type represents sets of integer or floating-point values.

There is a set of predeclared numeric types with
implementation-specific sizes:


uint the set of all unsigned integers, either 32 or 64 bits
int the set of all signed integers, same size as uint

Go array length is a value of type int, which is a 32 or 64 bit signed integer, depending on the compilation architecture (GOARCH), for example, 386 or amd64. It's also subject to any hardware or operating system memory size limits.

package main

import (
"fmt"
"runtime"
"strconv"
)

func main() {
fmt.Println("int is", strconv.IntSize, "bits on", runtime.GOARCH)
}

Output:


int is 64 bits on amd64


Related Topics



Leave a reply



Submit