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
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
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?
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