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

MethodHandle.asCollector(...) throws OOM for "virtual" method handle and very large arrayType

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 7
    • core-libs

      The following code snippet throws OutOfMemoryException instead of IllegalArgumentException:

      $ cat AsCollectorTest.java import java.dyn.*;

      public class AsCollectorTest {
          public static void main(String[] args) throws Throwable {
              try {
                  asCollectorIAE();
                  System.out.println("FAILED");
              } catch(IllegalArgumentException iae) {
                  System.out.println("OKAY");
              }
          }

          private static void asCollectorIAE() throws Throwable {
              MethodHandle mh = MethodHandles.lookup().findVirtual(
                      AsCollectorTest.class, "m", MethodType.methodType(void.class));
              mh.asCollector(Object[].class, Integer.MAX_VALUE/2); // MAX_VALUE, MAX_VALUE-1, ...
          }

          private void m() {}
      }

      $ javac AsCollectorTest.java
      $ java -showversion -XX:+UnlockExperimentalVMOptions -XX:+EnableInvokeDynamic AsCollectorTest
      java version "1.7.0-ea"
      Java(TM) SE Runtime Environment (build 1.7.0-ea-b130)
      Java HotSpot(TM) Server VM (build 21.0-b02, mixed mode)

      Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
              at java.lang.reflect.Array.newArray(Native Method)
              at java.lang.reflect.Array.newInstance(Array.java:70)
              at java.util.Collections$CopiesList.toArray(Collections.java:3511)
              at java.dyn.MethodType.insertParameterTypes(MethodType.java:329)
              at java.dyn.MethodHandle.asCollector(MethodHandle.java:589)
              at AsCollectorTest.asCollectorIAE(AsCollectorTest.java:16)
              at AsCollectorTest.main(AsCollectorTest.java:6)

            Unassigned Unassigned
            ygaevsky Yuri Gaevsky (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: