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

查看:31
本文介绍了为什么 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. 为什么我不能创建大尺寸的数组?

有些人给出了一些逻辑,根据文档一些虚拟机在数组中保留了一些标题词".所以对于标题词,减去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,因为它是可调整大小的数组实现.

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 :描述对象状态的标志集合,包括对象的哈希码(如果有的话),以及对象的形状对象(即对象是否为数组).

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 :对象的同步信息——即对象当前是否同步.

Lock : The synchronization information for the object — that is, whether the object is currently synchronized.

Size : 数组的大小.

Size : The size of the array.

最大尺寸

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天全站免登陆