关于list理论最大大小Integer.MAX_VALUE - 8

博客探讨了Java中List理论的最大大小为何是Integer.MAX_VALUE - 8,这涉及到数组头的存储需求。由于数组长度信息和其他信息占用8个字节,因此最大长度需要减去这些字节以确保至少能存储8个元素。然而,实际创建如此大的数组会受到JVM和分配给程序的堆空间限制。通过示例代码展示了不同类型的数组在不同堆空间限制下能创建的最大长度。
摘要由CSDN通过智能技术生成

今天看源码对list最大大小为什么是Integer.MAX_VALUE - 8产生了兴趣。
目前得出以下结论:
首先数组头需要存储数组大小信息以及其它的一些信息,假设数组达到了最大则数组大小需要8个字节来存储。而当留下8个数组大小时则可保证至少有8个字节(存储byte类型时恰好为8个字节,其它类型则大于8个字节)
当然,由于虚拟机的限制完全不可能达到这个大小。
附上stackoverflow上的一些探讨结果:

Well, Ivan has already correctly pointed out that the array length does have a well defined upper limit and that it is again JVM/Platform dependent. In fact, more importantly, he also stated that how much length of array you can actually create in your code will mainly be controlled by how much max heap space you have allocated to your program while execution.
I would just like to add small code snippet to support his explanation. For example, theoretically, an array [] should accept length <= INTEGER.MAX_VALUE - x (here x is header size that is again JVM/Platform specific) but suppose you run following Java program with VM option -Xmx32m then you will see that none of the

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值