【发布时间】:2015-06-27 06:47:53
【问题描述】:
下面是java.util.PriorityQueue(java 1.7)源码中我看不懂的注释。
/**
* 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;
为什么有些虚拟机会在数组中保留一些标题字?那是干什么用的?这些虚拟机到底是什么?
在这个cmets下面,这里是容量大于MAX_ARRAY_SIZE时处理的函数
private void grow(int minCapacity) {
int oldCapacity = queue.length;
// Double size if small; else grow by 50%
int newCapacity = oldCapacity + ((oldCapacity < 64) ?
(oldCapacity + 2) :
(oldCapacity >> 1));
// overflow-conscious code
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
queue = Arrays.copyOf(queue, newCapacity);
}
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
//why return Integer.MAX_VALUE ? Would this throw an exception in some certain VMs?
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
【问题讨论】:
标签: java priority-queue