JDK-7021343 : MethodHandle.asCollector(...) throws OOM for "virtual" method handle and very large arrayType
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang.invoke
  • Affected Version: 7
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2011-02-22
  • Updated: 2012-03-22
  • Resolved: 2011-03-30
Related Reports
Duplicate :  
Description
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)

Comments
EVALUATION Will be fixed as part of 7032323.
30-03-2011