Java数组有最大大小吗?

时间:2022-09-25 14:08:36

Is there a limit to the number of elements a Java array can contain? If so, what is it?

Java数组中元素的数量是否有限制?如果是,那是什么?

7 个解决方案

#1


149  

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:

在最近的HotSpot VM中,正确的答案是整数。MAX_VALUE - 5。一旦你超越了这个范围:

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

#2


109  

This is (of course) totally VM-dependent.

这是(当然)完全依赖于vm的。

Browsing through the source code of OpenJDK 7 and 8 java.util.ArrayList, .Hashtable, .AbstractCollection, .PriorityQueue, and .Vector, you can see this claim being repeated:

浏览OpenJDK 7和8 java.util的源代码。ArrayList, .Hashtable, .AbstractCollection, .PriorityQueue和。vector,您可以看到这个声明被重复:

/**
 * Some VMs reserve some header words in an array.
 * Attempts to allocate larger arrays may result in
 * OutOfMemoryError: Requested array size exceeds VM limit
 */
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

which is added by Martin Buchholz (Google) on 2010-05-09; reviewed by Chris Hegarty (Oracle).

由Martin Buchholz(谷歌)在2010-05-09年添加;Chris Hegarty (Oracle)评论。

So, probably we can say that the maximum "safe" number would be 2 147 483 639 (Integer.MAX_VALUE - 8) and "attempts to allocate larger arrays may result in OutOfMemoryError".

所以,我们可以说最大的“安全”数字是2 147 483 639(整数。MAX_VALUE - 8)和“尝试分配更大的数组可能导致OutOfMemoryError”。

(Yes, Buchholz's standalone claim does not include backing evidence, so this is a calculated appeal to authority. Even within OpenJDK itself, we can see code like return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; which shows that MAX_ARRAY_SIZE does not yet have a real use.)

(是的,Buchholz的独立声明不包括支持证据,所以这是对权威的一个计算的呼吁。甚至在OpenJDK内部,我们也可以看到类似于return (minCapacity > MAX_ARRAY_SIZE)这样的代码?整数。MAX_VALUE:MAX_ARRAY_SIZE;这表明MAX_ARRAY_SIZE还没有真正的使用。

#3


37  

There are actually two limits. One, the maximum element indexable for the array and, two, the amount of memory available to your application. Depending on the amount of memory available and the amount used by other data structures, you may hit the memory limit before you reach the maximum addressable array element.

实际上有两个限制。一个是数组的最大元素可索引,以及两个,用于应用程序的内存数量。根据可用内存的数量和其他数据结构所使用的数量,在到达最大可寻址数组元素之前,可能会达到内存限制。

#4


23  

Going by this article http://en.wikipedia.org/wiki/Criticism_of_Java#Large_arrays:

通过这篇文章http://en.wikipedia.org/wiki/sm_of_java # large_array:

Java has been criticized for not supporting arrays of more than 231−1 (about 2.1 billion) elements. This is a limitation of the language; the Java Language Specification, Section 10.4, states that:

Java被批评为不支持数组超过231−1(约21亿)的元素。这是语言的限制;Java语言规范第10.4节指出:

Arrays must be indexed by int values... An attempt to access an array component with a long index value results in a compile-time error.

数组必须以int值为索引…试图访问具有长索引值的数组组件会导致编译时错误。

Supporting large arrays would also require changes to the JVM. This limitation manifests itself in areas such as collections being limited to 2 billion elements and the inability to memory map files larger than 2 GiB. Java also lacks true multidimensional arrays (contiguously allocated single blocks of memory accessed by a single indirection), which limits performance for scientific and technical computing.

支持大数组也需要对JVM进行更改。这种限制体现在集合被限制为20亿个元素和不能存储大于2个GiB的内存映射文件等方面。Java还缺乏真正的多维数组(连续分配单个内存块访问的单个块),这限制了科学和技术计算的性能。

#5


10  

Arrays are non-negative integer indexed , so maximum array size you can access would be Integer.MAX_VALUE. The other thing is how big array you can create. It depends on the maximum memory available to your JVM and the content type of the array. Each array element has it's size, example. byte = 1 byte, int = 4 bytes, Object reference = 4 bytes (on a 32 bit system)

数组是非负整数索引的,所以您可以访问的最大数组大小是Integer.MAX_VALUE。另一件事是你可以创建多大的数组。它取决于JVM的最大可用内存和数组的内容类型。每个数组元素都有它的大小,例如。byte = 1字节,int = 4字节,对象引用= 4字节(在32位系统上)

So if you have 1 MB memory available on your machine, you could allocate an array of byte[1024 * 1024] or Object[256 * 1024].

因此,如果您的机器上有1 MB内存,那么可以分配一个字节数组[1024 * 1024]或对象[256 * 1024]。

Answering your question - You can allocate an array of size (maximum available memory / size of array item).

回答你的问题——你可以分配一个数组大小(最大可用内存/数组项的大小)。

Summary - Theoretically the maximum size of an array will be Integer.MAX_VALUE. Practically it depends on how much memory your JVM has and how much of that has already been allocated to other objects.

理论上,数组的最大大小是整数。max_value。实际上,这取决于您的JVM有多少内存,以及已经分配给其他对象的内存有多少。

#6


1  

Maximum number of elements of an array is (2^31)−1 or 2 147 483 647

最大数量的一个数组的元素(2 ^ 31)−1或2 147 483 647

#7


1  

I tried to create a byte array like this

我尝试创建一个这样的字节数组。

byte[] bytes = new byte[Integer.MAX_VALUE-x];
System.out.println(bytes.length);

With this run configuration:

这个运行配置:

-Xms4G -Xmx4G

And java version:

和java版本:

Openjdk version "1.8.0_141"

Openjdk版本“1.8.0_141”

OpenJDK Runtime Environment (build 1.8.0_141-b16)

OpenJDK运行时环境(构建1.8.0_141-b16)

OpenJDK 64-Bit Server VM (build 25.141-b16, mixed mode)

OpenJDK 64位服务器VM(构建25.141-b16,混合模式)

It only works for x >= 2 which means the maximum size of an array is Integer.MAX_VALUE-2

它只适用于x >= 2,这意味着数组的最大大小为Integer.MAX_VALUE-2。

Values above that give

值高于给

Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit at Main.main(Main.java:6)

线程“main”java.lang中的异常。OutOfMemoryError:请求的数组大小超出了主服务器的VM限制(Main.java:6)

#1


149  

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:

在最近的HotSpot VM中,正确的答案是整数。MAX_VALUE - 5。一旦你超越了这个范围:

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

#2


109  

This is (of course) totally VM-dependent.

这是(当然)完全依赖于vm的。

Browsing through the source code of OpenJDK 7 and 8 java.util.ArrayList, .Hashtable, .AbstractCollection, .PriorityQueue, and .Vector, you can see this claim being repeated:

浏览OpenJDK 7和8 java.util的源代码。ArrayList, .Hashtable, .AbstractCollection, .PriorityQueue和。vector,您可以看到这个声明被重复:

/**
 * Some VMs reserve some header words in an array.
 * Attempts to allocate larger arrays may result in
 * OutOfMemoryError: Requested array size exceeds VM limit
 */
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

which is added by Martin Buchholz (Google) on 2010-05-09; reviewed by Chris Hegarty (Oracle).

由Martin Buchholz(谷歌)在2010-05-09年添加;Chris Hegarty (Oracle)评论。

So, probably we can say that the maximum "safe" number would be 2 147 483 639 (Integer.MAX_VALUE - 8) and "attempts to allocate larger arrays may result in OutOfMemoryError".

所以,我们可以说最大的“安全”数字是2 147 483 639(整数。MAX_VALUE - 8)和“尝试分配更大的数组可能导致OutOfMemoryError”。

(Yes, Buchholz's standalone claim does not include backing evidence, so this is a calculated appeal to authority. Even within OpenJDK itself, we can see code like return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; which shows that MAX_ARRAY_SIZE does not yet have a real use.)

(是的,Buchholz的独立声明不包括支持证据,所以这是对权威的一个计算的呼吁。甚至在OpenJDK内部,我们也可以看到类似于return (minCapacity > MAX_ARRAY_SIZE)这样的代码?整数。MAX_VALUE:MAX_ARRAY_SIZE;这表明MAX_ARRAY_SIZE还没有真正的使用。

#3


37  

There are actually two limits. One, the maximum element indexable for the array and, two, the amount of memory available to your application. Depending on the amount of memory available and the amount used by other data structures, you may hit the memory limit before you reach the maximum addressable array element.

实际上有两个限制。一个是数组的最大元素可索引,以及两个,用于应用程序的内存数量。根据可用内存的数量和其他数据结构所使用的数量,在到达最大可寻址数组元素之前,可能会达到内存限制。

#4


23  

Going by this article http://en.wikipedia.org/wiki/Criticism_of_Java#Large_arrays:

通过这篇文章http://en.wikipedia.org/wiki/sm_of_java # large_array:

Java has been criticized for not supporting arrays of more than 231−1 (about 2.1 billion) elements. This is a limitation of the language; the Java Language Specification, Section 10.4, states that:

Java被批评为不支持数组超过231−1(约21亿)的元素。这是语言的限制;Java语言规范第10.4节指出:

Arrays must be indexed by int values... An attempt to access an array component with a long index value results in a compile-time error.

数组必须以int值为索引…试图访问具有长索引值的数组组件会导致编译时错误。

Supporting large arrays would also require changes to the JVM. This limitation manifests itself in areas such as collections being limited to 2 billion elements and the inability to memory map files larger than 2 GiB. Java also lacks true multidimensional arrays (contiguously allocated single blocks of memory accessed by a single indirection), which limits performance for scientific and technical computing.

支持大数组也需要对JVM进行更改。这种限制体现在集合被限制为20亿个元素和不能存储大于2个GiB的内存映射文件等方面。Java还缺乏真正的多维数组(连续分配单个内存块访问的单个块),这限制了科学和技术计算的性能。

#5


10  

Arrays are non-negative integer indexed , so maximum array size you can access would be Integer.MAX_VALUE. The other thing is how big array you can create. It depends on the maximum memory available to your JVM and the content type of the array. Each array element has it's size, example. byte = 1 byte, int = 4 bytes, Object reference = 4 bytes (on a 32 bit system)

数组是非负整数索引的,所以您可以访问的最大数组大小是Integer.MAX_VALUE。另一件事是你可以创建多大的数组。它取决于JVM的最大可用内存和数组的内容类型。每个数组元素都有它的大小,例如。byte = 1字节,int = 4字节,对象引用= 4字节(在32位系统上)

So if you have 1 MB memory available on your machine, you could allocate an array of byte[1024 * 1024] or Object[256 * 1024].

因此,如果您的机器上有1 MB内存,那么可以分配一个字节数组[1024 * 1024]或对象[256 * 1024]。

Answering your question - You can allocate an array of size (maximum available memory / size of array item).

回答你的问题——你可以分配一个数组大小(最大可用内存/数组项的大小)。

Summary - Theoretically the maximum size of an array will be Integer.MAX_VALUE. Practically it depends on how much memory your JVM has and how much of that has already been allocated to other objects.

理论上,数组的最大大小是整数。max_value。实际上,这取决于您的JVM有多少内存,以及已经分配给其他对象的内存有多少。

#6


1  

Maximum number of elements of an array is (2^31)−1 or 2 147 483 647

最大数量的一个数组的元素(2 ^ 31)−1或2 147 483 647

#7


1  

I tried to create a byte array like this

我尝试创建一个这样的字节数组。

byte[] bytes = new byte[Integer.MAX_VALUE-x];
System.out.println(bytes.length);

With this run configuration:

这个运行配置:

-Xms4G -Xmx4G

And java version:

和java版本:

Openjdk version "1.8.0_141"

Openjdk版本“1.8.0_141”

OpenJDK Runtime Environment (build 1.8.0_141-b16)

OpenJDK运行时环境(构建1.8.0_141-b16)

OpenJDK 64-Bit Server VM (build 25.141-b16, mixed mode)

OpenJDK 64位服务器VM(构建25.141-b16,混合模式)

It only works for x >= 2 which means the maximum size of an array is Integer.MAX_VALUE-2

它只适用于x >= 2,这意味着数组的最大大小为Integer.MAX_VALUE-2。

Values above that give

值高于给

Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit at Main.main(Main.java:6)

线程“main”java.lang中的异常。OutOfMemoryError:请求的数组大小超出了主服务器的VM限制(Main.java:6)