为什么ArrayList的最大数组大小是Integer.MAX_VALUE - 8? [英] Why the maximum array size of ArrayList is Integer.MAX_VALUE - 8?

查看:992
本文介绍了为什么ArrayList的最大数组大小是Integer.MAX_VALUE - 8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 ArrayList 的Java 8文档。我得到的最大数组大小定义为 Integer.MAX_VALUE - 8 表示2 ^ 31 - 8 = 2 147 483 639 。然后我专注于为什么减去8或为什么不减少8 减去8

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. Java阵列是否有最大值尺寸?

  2. 列表最多可容纳多少数据

  3. 为什么我不能创建大尺寸的数组?

  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?

有些人给出了一些逻辑,根据文档有些虚拟机会在数组中保留一些标题字。因此对于标题字,减去8。但在这种情况下,如果标题词需要超过8,那么答案是什么?

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.

推荐答案

阅读上面有关 Java内存管理,明确说明

Read the above article about Java Memory management, which clearly states

我认为这是适用于ArrayList,因为它是Resizable数组实现。

I think this applies to ArrayList as it is the Resizable array implemenation.

Java数组对象剖析


数组对象的形状和结构(例如int
值的数组)类似于标准Java对象的形状和结构。主
的区别在于数组对象有一个额外的
元数据,表示数组的大小。数组对象的元数据
然后包含:Class:指向类信息的指针,
描述对象类型。对于int字段数组,此
是指向int []类的指针。

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.

大小:数组的大小。

max size

2^31 = 2,147,483,648 

作为它自己需要的数组 8字节来存储大小
2,147,483,648

as the Array it self needs 8 bytes to stores the size 2,147,483,648

所以

2^31 -8 (for storing size ), 

所以最大数组大小定义为Integer.MAX_VALUE - 8

so maximum array size is defined as Integer.MAX_VALUE - 8

这篇关于为什么ArrayList的最大数组大小是Integer.MAX_VALUE - 8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆