Java 数组有最大大小吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3038392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:53:42  来源:igfitidea点击:

Do Java arrays have a maximum size?

javaarrays

提问by Lizard

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

Java 数组可以包含的元素数量是否有限制?如果是,那是什么?

采纳答案by Kevin Bourrillion

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 中,正确答案是Integer.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

回答by tvanfosson

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.

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

回答by Pacerier

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 claimbeing 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;
/**
 * 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 (Google)在 2010-05-09 添加;由 Chris Hegarty (Oracle) 。

So, probablywe 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( Integer.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_SIZEdoes not yet have a realuse.)

(是的,Buchholz 的独立声明不包括支持证据,因此这是对权威的计算诉求。即使在 OpenJDK 本身中,我们也可以看到类似的代码return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE;表明MAX_ARRAY_SIZE尚未真正使用。)

回答by Baby

Maximum number of elements of an arrayis (2^31)?1or 2 147 483 647

an 的最大元素数array(2^31)?12 147 483 647

回答by working

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

通过这篇文章http://en.wikipedia.org/wiki/Criticism_of_Java#Large_arrays

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:

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.

Java 因不支持超过 2 31?1(约 21 亿)个元素的数组而受到批评。这是语言的限制;Java 语言规范第 10.4 节指出:

数组必须由 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 还缺乏真正的多维数组(由单个间接访问的连续分配的单个内存块),这限制了科学和技术计算的性能。

回答by Dhanuka

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 JVMand 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 byte, int = 4 bytes,Object reference = 4 bytes (on a 32 bit system)

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

因此,如果您1 MB的机器上有可用内存,则可以分配一个byte[1024 * 1024]or数组Object[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 JVMhas and how much of that has already been allocated to other objects.

总结- 理论上,数组的最大大小为Integer.MAX_VALUE. 实际上,这取决于您JVM拥有多少内存以及其中多少已分配给其他对象。

回答by Vasilis Nicolaou

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 Runtime Environment (build 1.8.0_141-b16)

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

Openjdk 版本“1.8.0_141”

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

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:请求的数组大小超出 Main.main(Main.java:6) 处的 VM 限制

回答by Ferned

Actually it's java limitation caping it at 2^30-4 being 1073741820. Not 2^31-1. Dunno why but i tested it manually on jdk. 2^30-3 still throwing vm except

实际上,它是 Java 限制,上限为 2^30-4,即 1073741820。不是 2^31-1。不知道为什么,但我在 jdk 上手动测试了它。2^30-3 仍然抛出 vm 除了

Edit: fixed -1 to -4, checked on windows jvm

编辑:固定 -1 到 -4,在 windows jvm 上检查

回答by Avinash Barde

Yes, there limit on java array. Java uses an integer as an index to the array and the maximum integer store by JVM is 2^32. so you can store 2,147,483,647 elements in the array.

是的,java 数组有限制。Java 使用整数作为数组的索引,JVM 存储的最大整数是 2^32。所以你可以在数组中存储 2,147,483,647 个元素。

In case you need more than max-length you can use two different arrays but the recommended method is store data into a file. because storing data in the file has no limit. because files stored in your storage drivers but array are stored in JVM. JVM provides limited space for program execution.

如果您需要超过 max-length,您可以使用两个不同的数组,但推荐的方法是将数据存储到文件中。因为在文件中存储数据没有限制。因为文件存储在您的存储驱动程序中,而数组存储在 JVM 中。JVM 为程序执行提供了有限的空间。