17

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.

Community
  • 1
  • 1
SkyWalker
  • 28,384
  • 14
  • 74
  • 132
  • 6
    If more than 8 header words are needed, then it'd crash when it got that big. So 8 is the ArrayList author's estimate of the maximum number of extra header words that'd be needed. – Louis Wasserman Mar 02 '16 at 19:25
  • 2
    On an unrelated note: if you're allocating an array with 2 billions elements, there is probably something wrong somewhere. – Tunaki Mar 02 '16 at 19:31
  • @LouisWasserman - in VM 8 bytes is more high? If possible, please let me know. – SkyWalker Mar 02 '16 at 19:31
  • 2
    @SkyWalker, that sentence makes no sense. Also, it's 8 bytes, not 8 bits, which is _lots_ of room for headers. – Louis Wasserman Mar 02 '16 at 19:32
  • @LouisWasserman I got that. So it will not exceed 8 bytes for header of VM – SkyWalker Mar 02 '16 at 19:38
  • see http://stackoverflow.com/questions/3038392/do-java-arrays-have-a-maximum-size for more info – Vasily Liaskovsky Mar 02 '16 at 20:03
  • Someone has given downvote but no comments. Would you please clarify and share the reason so that we can be more proactive to ask a question? – SkyWalker Oct 27 '17 at 13:18

4 Answers4

12

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

Pragnani
  • 20,075
  • 6
  • 49
  • 74
6

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

Community
  • 1
  • 1
Gurpreet Singh
  • 380
  • 4
  • 9
4

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?

Community
  • 1
  • 1
Andreas
  • 154,647
  • 11
  • 152
  • 247
0

integer max size is : 2^31 - 1 = 2,147,483,648 - 1

Integer.java: @Native public static final int MAX_VALUE = 0x7fffffff;

SMLZ
  • 21
  • 2