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

Unexpected OutOfMemoryError when allocating an array larger than the heap

    Details

      Description

      ADDITIONAL SYSTEM INFORMATION :
      I tried 3 different versions, I see the bug everytime.

      Oracle JDK15 :
      java version "15" 2020-09-15
      Java(TM) SE Runtime Environment (build 15+36-1562)
      Java HotSpot(TM) 64-Bit Server VM (build 15+36-1562, mixed mode, sharing)

      OpenJDK 11 :
      openjdk version "11.0.8" 2020-07-14
      OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-0ubuntu120.04)
      OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Ubuntu-0ubuntu120.04, mixed mode)

      OpenJDK 8 :
      openjdk version "1.8.0_265"
      OpenJDK Runtime Environment (build 1.8.0_265-8u265-b01-0ubuntu2~20.04-b01)
      OpenJDK 64-Bit Server VM (build 25.265-b01, mixed mode)

      A DESCRIPTION OF THE PROBLEM :
      When trying to allocate an array bigger than the heap, I expect an OOM error thrown with the "Requested array size exceeds VM limit" message, but I get a "Java heap space" message instead.

      Documentation : https://docs.oracle.com/en/java/javase/11/troubleshoot/troubleshoot-memory-leaks.html#GUID-19F6D28E-75A1-4480-9879-D0932B2F305B


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Allocate a big array of 800 MB but restrict the heap size to 100 MB. The OutOfMemoryError is thrown, but it's the wrong message.

      Command :
      java -Xmx100m MemOverflow

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Heap max size: 100MB
      Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit
          at MemOverflow.main(MemOverflow.java:5)
      ACTUAL -
      Heap max size: 100MB
      Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
          at MemOverflow.main(MemOverflow.java:5)

      ---------- BEGIN SOURCE ----------
      public class MemOverflow {

          public static void main(final String[] args) {
              System.out.println("Heap max size: " + (Runtime.getRuntime().maxMemory() / 1024 / 1024) + "MB");
              long[] array = new long[100_000_000]; // ~800MB
              System.out.println(array.length);
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY : rarely


        Attachments

          Activity

            People

            • Assignee:
              pprasad Padmapriya Prasad
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: