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

自闭症网瘾萝莉.ら 提交于 2019-12-17 16:43:39

问题


I am studying Java 8 documentation for ArrayList. I got that maximum array size is defined as Integer.MAX_VALUE - 8 means 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?

/**
 * 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?
  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". So for header words, 8 is subtracted. But on that case, if header words need more than 8, then what will be the answer?

Please clarify me on that basis. Advance thanks for your cooperation.


回答1:


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




回答2:


The size of object header can not exceed 8 byte.

For HotSpot:

The object header consists of a mark word and a klass pointer.

The mark word has word size (4 byte on 32 bit architectures, 8 byte on 64 bit architectures) and

the klass pointer has word size on 32 bit architectures. 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.

This optimization is called "compressed oops" and you can also control it with the option UseCompressedOops.

What is in java object header




回答3:


The value is a worst-case scenario. Note the comment:

Attempts to allocate larger arrays may result in 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?



来源:https://stackoverflow.com/questions/35756277/why-the-maximum-array-size-of-arraylist-is-integer-max-value-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!