Java util priorityQueue implementation

207 Views Asked by At

Below is a comment I don't understand in the source code of 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;

Why some VMs reserve some header words in an array? What is that used for? And what exactly these VMs are?

And below this comments, here is the function to handle when the capacity is large than 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;

}
1

There are 1 best solutions below

3
On

VM in this context refers to a JVM (Java Virtual Machine), the piece of software that executes Java byte code.

There are several different implementations for this software (most notable today - Oracle's and the OpenJDK's). Some of these implementations may add some additional memory to arrays for their internal purposes (e.g., tracking garbage collection). Since the JDK attempts to be ass generic as possible, and run on any JVM, it takes a precaution and will not allow you to create a PriorityQueue which internally uses an array longer than Integer.MAX_VALUE - 8.