1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test LFGarbageCollectedTest
  26  * @bug 8046703
  27  * @summary Test verifies that lambda forms are garbage collected
  28  * @author kshefov
  29  * @library /lib/testlibrary/jsr292 /lib/testlibrary
  30  * @build TestMethods
  31  * @build LambdaFormTestCase
  32  * @build LFGarbageCollectedTest
  33  * @run main/othervm -Xmx64m -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+HeapDumpOnOutOfMemoryError -DHEAP_DUMP=false LFGarbageCollectedTest
  34  */
  35 
  36 import java.lang.invoke.MethodHandle;
  37 import java.lang.invoke.MethodType;
  38 import java.lang.ref.PhantomReference;
  39 import java.lang.ref.Reference;
  40 import java.lang.ref.ReferenceQueue;
  41 import java.lang.reflect.InvocationTargetException;
  42 import java.util.EnumSet;
  43 import java.util.Map;
  44 
  45 /**
  46  * Lambda forms garbage collection test class.
  47  */
  48 public final class LFGarbageCollectedTest extends LambdaFormTestCase {
  49     private static boolean HEAP_DUMP = Boolean.getBoolean("HEAP_DUMP");
  50 
  51     /**
  52      * Constructor for a lambda forms garbage collection test case.
  53      *
  54      * @param testMethod A method from {@code j.l.i.MethodHandles} class that
  55      * returns a {@code j.l.i.MethodHandle} instance.
  56      */
  57     public LFGarbageCollectedTest(TestMethods testMethod) {
  58         super(testMethod);
  59     }
  60 
  61      PhantomReference ph;
  62      ReferenceQueue rq = new ReferenceQueue();
  63      MethodType mtype;
  64      Map<String, Object> data;
  65 
  66     @Override
  67     public void doTest() {
  68         try {
  69             TestMethods testCase = getTestMethod();
  70             data = testCase.getTestCaseData();
  71             MethodHandle adapter;
  72             try {
  73                 adapter = testCase.getTestCaseMH(data, TestMethods.Kind.ONE);
  74             } catch (NoSuchMethodException ex) {
  75                 throw new Error("Unexpected exception: ", ex);
  76             }
  77             mtype = adapter.type();
  78             Object lambdaForm = INTERNAL_FORM.invoke(adapter);
  79             if (lambdaForm == null) {
  80                 throw new Error("Unexpected error: Lambda form of the method handle is null");
  81             }
  82             
  83             String debugName = (String)DEBUG_NAME.get(lambdaForm);
  84             if (debugName != null && debugName.startsWith("identity_")) {
  85                 // Ignore identity_* LambdaForms.
  86                 return;
  87             }
  88 
  89             ph = new PhantomReference(lambdaForm, rq);
  90             lambdaForm = null;
  91             adapter = null;
  92 
  93             collectLambdaForm();
  94         } catch (IllegalAccessException | IllegalArgumentException |
  95                 InvocationTargetException ex) {
  96             throw new Error("Unexpected exception: ", ex);
  97         }
  98     }
  99     
 100     private void collectLambdaForm() throws IllegalAccessException {
 101        // Usually, 2 System.GCs are necessary to enqueue a SoftReference.
 102         System.gc();
 103         System.gc();
 104 
 105         Reference ref = null;
 106         for (int i = 0; i < 10; i++) {
 107             try {
 108                 ref = rq.remove(1000);
 109             } catch (InterruptedException e) {
 110                 /* ignore */
 111             }
 112             if (ref != null) {
 113                 break;
 114             }
 115             System.gc(); // If the reference hasn't been queued yet, trigger one more GC.
 116         }
 117 
 118         if (ref == null) {
 119             dumpTestData();
 120             System.err.println("Method type: " + mtype);
 121             System.err.println("LambdaForm:  " + REF_FIELD.get(ph));
 122 
 123             if (HEAP_DUMP) {
 124                 // Trigger OOM to force heap dump for post-mortem analysis.
 125                 val = new long[1_000_000_000];
 126             }
 127             throw new AssertionError("Error: LambdaForm is not garbage collected");
 128         };
 129     }
 130 
 131    private void dumpTestData() {
 132         System.err.println("Test case: " + getTestMethod());
 133         for (String s : data.keySet()) {
 134             System.err.printf("\t%20s => %s\n", s, data.get(s));
 135         }
 136     }
 137 
 138     private static long[] val;
 139 
 140     /**
 141      * Main routine for lambda forms garbage collection test.
 142      *
 143      * @param args Accepts no arguments.
 144      */
 145     public static void main(String[] args) {
 146         // The "identity", "constant", "arrayElementGetter" and "arrayElementSetter"
 147         // methods should be removed from this test,
 148         // because their lambda forms are stored in a static field and are not GC'ed.
 149         // There can be only a finite number of such LFs for each method,
 150         // so no memory leak happens.
 151         EnumSet<TestMethods> testMethods = EnumSet.complementOf(EnumSet.of(
 152                 TestMethods.IDENTITY,
 153                 TestMethods.CONSTANT,
 154                 TestMethods.ARRAY_ELEMENT_GETTER,
 155                 TestMethods.ARRAY_ELEMENT_SETTER,
 156                 TestMethods.EXACT_INVOKER,
 157                 TestMethods.INVOKER));
 158         LambdaFormTestCase.runTests(LFGarbageCollectedTest::new, testMethods);
 159     }
 160 }