当前位置:   article > 正文

【Java基础】ArrayList的容量上限?为什么Integer.MAX_VALUE要减8?_arraylist最大容量

arraylist最大容量

ArrayList容量上限

我们知道ArrayList维护了一个int整型的数组容量size:

    /**
     * Returns the number of elements in this list.
     *
     * @return the number of elements in this list
     */
    public int size() {
        return size;
    }

    /**
     * Returns {@code true} if this list contains no elements.
     *
     * @return {@code true} if this list contains no elements
     */
    public boolean isEmpty() {
        return size == 0;
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

正常说来,int整型的范围不超过2^31-1,也就是Integer.MAX_VALUE,所以ArrayList容量上限就是Integer.MAX_VALUE吗?不是的。

查看ArrayList源码可以发现

	/**
	 * The maximum size of array to allocate (unless necessary).
	 * 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;
	
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

所以ArrayList容量上限是Integer.MAX_VALUE - 8

为什么要减8?

因为自己作为数组,除了存储数据本身以外,还需要32 bytes的大小来存储对象头(object header)信息。Java每个对象都包含了对象头,HotSpot虚拟机中对象头的大小不会超过32 bytes,所以最大容量减8才不会溢出。

我们知道,Java对象在堆内存中的存储布局可以分为三部分:对象头(object header),实例数据(Instance Data)和对齐填充(Padding)。

在这里插入图片描述

对象头包括:

  1. Mark Word:用于对象自身的运行时数据存储,如HashCode,GC分代年龄,锁状态标志,线程持有的锁,偏向线程ID和偏向时间戳等;
  2. Klass Pointer:对象指向它类元数据的指针,JVM通过这个指针长度来确定对象是哪个类的实例。
  3. 数组长度(只有数组对象才有):记录数组对象的长度。

在这里插入图片描述

我们知道 8 int(整数) 就等于32 bytes(字节),所以减去对象头大小的32 bytes来源于:

32 bytes = 8 bytes(Mark Word的最大占用) + 8 bytes(Klass Pointer的最大占用) + 4 bytes(数组长度)+ 8 bytes(引用指针的最大占用:数组中存放的是对象的引用) + 4 bytes(padding:为了方便寻址,JVM要求对象大小要求是8的倍数,不够就填充)

参考

https://www.cnblogs.com/silyvin/p/11721144.html

https://blog.csdn.net/yangshangwei/article/details/106958768

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/766535
推荐阅读
相关标签
  

闽ICP备14008679号