简体   繁体   English

为什么ArrayList的最大数组大小是Integer.MAX_VALUE - 8?

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

I am studying Java 8 documentation for ArrayList . 我正在研究ArrayList Java 8文档。 I got that maximum array size is defined as Integer.MAX_VALUE - 8 means 2^31 – 8 = 2 147 483 639 . 我得到的最大数组大小定义为Integer.MAX_VALUE - 8表示2 ^ 31 - 8 = 2 147 483 639 Then I have focused on why 8 is subtracted or why not less than 8 or more than 8 is subtracted? 然后我集中讨论为什么减去8或why not less than 8more than 8

/**
 * The maximum size of array to allocate.
 * 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;

I got some related answers but not fulfilling my thrust. 我得到了一些相关的答案,但没有实现我的目标。

  1. Do Java arrays have a maximum size? Java数组是否具有最大大小?
  2. How many data a list can hold at the maximum 列表最多可以容纳多少数据
  3. Why I can't create an array with large size? 为什么我无法创建大尺寸的数组?

Some people given some logic that as per documentation "Some VMs reserve some header words in an array" . 有些人给出了一些逻辑,根据文档"Some VMs reserve some header words in an array" So for header words, 8 is subtracted. 因此对于标题字,减去8。 But on that case, if header words need more than 8, then what will be the answer? 但在这种情况下,如果标题词需要超过8,那么答案是什么?

Please clarify me on that basis. 请在此基础上澄清我。 Advance thanks for your cooperation. 感谢您的合作。

Read the above article about Java Memory management , which clearly states 阅读上面有关Java内存管理的文章,该文章明确指出

I think this applies to ArrayList as it is the Resizable array implemenation. 我认为这适用于ArrayList,因为它是Resizable数组实现。

Anatomy of a Java array object Java数组对象的剖析

The shape and structure of an array object, such as an array of int values, is similar to that of a standard Java object. 数组对象的形状和结构(例如int值数组)类似于标准Java对象的形状和结构。 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. 对于int字段数组,这是一个指向int []类的指针。

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 作为数组,它自己需要8 bytes来存储大小2,147,483,648

so 所以

2^31 -8 (for storing size ), 

so maximum array size is defined as Integer.MAX_VALUE - 8 所以最大数组大小定义为Integer.MAX_VALUE - 8

The size of object header can not exceed 8 byte. 对象头的大小不能超过8个字节。

For HotSpot: 对于HotSpot:

The object header consists of a mark word and a klass pointer . 对象标题由a mark worda klass pointer

The mark word has word size (4 byte on 32 bit architectures, 8 byte on 64 bit architectures) and 标记字具有字大小(32位体系结构为4字节,64位体系结构为8字节)和

the klass pointer has word size on 32 bit architectures. klass指针32 bit体系结构上具有字大小。 On 64 bit architectures the klass pointer either has word size, but can also have 4 byte if the heap addresses can be encoded in these 4 bytes . 64 bit架构的克拉斯指针或者具有字的大小,但也可以具有4 byte ,如果堆地址可以在这些被编码4 bytes

This optimization is called "compressed oops" and you can also control it with the option UseCompressedOops. 此优化称为“压缩oops” ,您还可以使用UseCompressedOops选项对其进行控制。

What is in java object header 什么是java对象头文件

The value is a worst-case scenario. 该值是最坏的情况。 Note the comment: 注意评论:

Attempts to allocate larger arrays may result in OutOfMemoryError 尝试分配更大的数组可能会导致OutOfMemoryError

It doesn't say will , just may . 它不会说愿意 ,只是可能 If you stay below this value, you should have no issue (as long as memory is available, of course). 如果你保持低于这个值,你应该没有问题(当然,只要内存可用)。

You may want to look at the answers to this question for more information: 您可能需要查看此问题的答案以获取更多信息:
Why I can't create an array with large size? 为什么我无法创建大尺寸的数组?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM