今天看源码对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