Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8065313

OutOfMemoryError in ByteArrayOutputStream.grow()

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 7u40
    • core-libs
    • x86
    • windows_8

      FULL PRODUCT VERSION :
      java version "1.7.0_40"
      Java(TM) SE Runtime Environment (build 1.7.0_40-b43)
      Java HotSpot(TM) 64-Bit Server VM (build 24.0-b56, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Windows Server 2012 R2 Standard
      Version 6.3.9600 Build 9600


      A DESCRIPTION OF THE PROBLEM :
      In my JDK ByteArrayOutputStream.grow() method uses Integer.MAX_VALUE as a maximum ByteArray value:

          /**
           * Increases the capacity to ensure that it can hold at least the
           * number of elements specified by the minimum capacity argument.
           *
           * @param minCapacity the desired minimum capacity
           */
          private void grow(int minCapacity) {
              // overflow-conscious code
              int oldCapacity = buf.length;
              int newCapacity = oldCapacity << 1;
              if (newCapacity - minCapacity < 0)
                  newCapacity = minCapacity;
              if (newCapacity < 0) {
                  if (minCapacity < 0) // overflow
                      throw new OutOfMemoryError();
                  newCapacity = Integer.MAX_VALUE;
              }
              buf = Arrays.copyOf(buf, newCapacity);
          }

      However, java could not create an array of such size, the maximum I can get is Integer.MAX_VALUE-2.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      run this code to get the error:
      byte[] b = new byte[Integer.MAX_VALUE]

      Run this code to be able to create a new ByteArray:
      byte[] b = new byte[Integer.MAX_VALUE-2]

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      no exception when growing array to a large size
      ACTUAL -
      exception

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      OutOfMemoryError: Requested array size exceeds VM limit

      REPRODUCIBILITY :
      This bug can be reproduced always.

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: