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

Provide MAX_ARRAY_SIZE publicly

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      The JVM may throw an OutOfMemoryError if it cannot allocate an array of the requested size, even if there is enough available memory, see JDK-8029587.
      The JDK has this maximum size hardcoded at multiple places, but does not expose it publicly. This has the disadvantages that:
      - Library authors have to rely on implementation details if they want to prevent OutOfMemoryErrors in their own code
      - A third-party JVM might have a lower maximum size than the one assumed by the JDK

      Therefore it would be useful to expose this value publicly, possibly as constant of java.lang.reflect.Array (related: JDK-7153247) and have all JVMs adhere to this limit. The limit is currently defined internally by `jdk.internal.util.ArraysSupport.MAX_ARRAY_LENGTH`.
      Maybe it would also make sense to differentiate between a guaranteed max array size which all JVM's have to adhere to and the maximum of the currently running JVM (which might be higher), though the difference might be negligible, so maybe such a differentiation is not worth it.
      Note however that only exposing the limit of the current JVM and not a general limit would probably cause issues e.g. when a JVM with a higher limit serializes a large array and then a JVM with a lower limit tries to deserialize it.

      Additionally the JLS should be updated to mention that an OutOfMemoryError may be thrown if the JVM does not support the requested array size, because apparently this is currently not documented (see https://docs.oracle.com/javase/specs/jls/se14/html/jls-10.html#jls-10.6).
      And also refer to the constant field which is proposed by this report.

      And JDK classes should then consistently use that constant instead of redeclaring this constant over and over again, examples:
      - https://github.com/openjdk/jdk/blob/e1b8e91e80201a62683cc3b5237adc5ae88fa5c7/src/java.base/share/classes/jdk/internal/util/ArraysSupport.java#L609
      - https://github.com/openjdk/jdk/blob/e1b8e91e80201a62683cc3b5237adc5ae88fa5c7/src/java.base/share/classes/java/lang/AbstractStringBuilder.java#L238
      - https://github.com/openjdk/jdk/blob/e1b8e91e80201a62683cc3b5237adc5ae88fa5c7/src/jdk.zipfs/share/classes/jdk/nio/zipfs/ByteArrayChannel.java#L226
      - https://github.com/openjdk/jdk/blob/6bab0f539fba8fb441697846347597b4a0ade428/src/java.base/share/classes/java/util/stream/Nodes.java#L61
      - https://github.com/openjdk/jdk/blob/e1b8e91e80201a62683cc3b5237adc5ae88fa5c7/src/java.base/share/classes/java/util/Hashtable.java#L397
      - https://github.com/openjdk/jdk/blob/9d764ee48ee7c2e7be7a25aee2ed7bed2fcd2000/src/java.base/share/classes/java/util/ArrayDeque.java#L133
      - https://github.com/openjdk/jdk/blob/e1b8e91e80201a62683cc3b5237adc5ae88fa5c7/src/java.base/share/classes/java/util/concurrent/ConcurrentHashMap.java#L520
      - https://github.com/openjdk/jdk/blob/e1b8e91e80201a62683cc3b5237adc5ae88fa5c7/src/java.base/share/classes/java/util/concurrent/PriorityBlockingQueue.java#L145



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

              Created:
              Updated: