1 /*
   2  * Copyright (c) 2014, 2017, 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  * @key randomness
  28  * @ignore 8078602
  29  * @summary Test verifies that lambda forms are garbage collected
  30  * @author kshefov
  31  * @library /lib/testlibrary /java/lang/invoke/common
  32  * @build TestMethods
  33  * @build LambdaFormTestCase
  34  * @build LFGarbageCollectedTest
  35  * @run main/othervm -Xmx64m
  36  *                   -XX:SoftRefLRUPolicyMSPerMB=0
  37  *                   -XX:+HeapDumpOnOutOfMemoryError
  38  *                   -DHEAP_DUMP=false
  39  *                   LFGarbageCollectedTest
  40  */
  41 
  42 import java.lang.invoke.MethodHandle;
  43 import java.lang.invoke.MethodType;
  44 import java.lang.ref.PhantomReference;
  45 import java.lang.ref.Reference;
  46 import java.lang.ref.ReferenceQueue;
  47 import java.lang.reflect.InvocationTargetException;
  48 import java.util.EnumSet;
  49 import java.util.Map;
  50 
  51 /**
  52  * Lambda forms garbage collection test class.
  53  */
  54 public final class LFGarbageCollectedTest extends LambdaFormTestCase {
  55     private static boolean HEAP_DUMP = Boolean.getBoolean("HEAP_DUMP");
  56 
  57     /**
  58      * Constructor for a lambda forms garbage collection test case.
  59      *
  60      * @param testMethod A method from {@code j.l.i.MethodHandles} class that
  61      * returns a {@code j.l.i.MethodHandle} instance.
  62      */
  63     public LFGarbageCollectedTest(TestMethods testMethod) {
  64         super(testMethod);
  65     }
  66 
  67     PhantomReference ph;
  68     ReferenceQueue rq = new ReferenceQueue();
  69     MethodType mtype;
  70     Map<String, Object> data;
  71 
  72     @Override
  73     public void doTest() {
  74         try {
  75             TestMethods testCase = getTestMethod();
  76             data = testCase.getTestCaseData();
  77             MethodHandle adapter;
  78             try {
  79                 adapter = testCase.getTestCaseMH(data, TestMethods.Kind.ONE);
  80             } catch (NoSuchMethodException ex) {
  81                 throw new Error("Unexpected exception", ex);
  82             }
  83             mtype = adapter.type();
  84             Object lambdaForm = INTERNAL_FORM.invoke(adapter);
  85             if (lambdaForm == null) {
  86                 throw new Error("Unexpected error: Lambda form of the method handle is null");
  87             }
  88 
  89             String kind = KIND_FIELD.get(lambdaForm).toString();
  90             if (kind.equals("IDENTITY")) {
  91                 // Ignore identity LambdaForms.
  92                 return;
  93             }
  94 
  95             ph = new PhantomReference(lambdaForm, rq);
  96             lambdaForm = null;
  97             adapter = null;
  98 
  99             collectLambdaForm();
 100         } catch (IllegalAccessException | IllegalArgumentException |
 101                 InvocationTargetException ex) {
 102             throw new Error("Unexpected exception", ex);
 103         }
 104     }
 105 
 106 
 107     private void collectLambdaForm() throws IllegalAccessException {
 108         // Usually, 2 System.GCs are necessary to enqueue a SoftReference.
 109         System.gc();
 110         System.gc();
 111 
 112         Reference ref = null;
 113         for (int i = 0; i < 10; i++) {
 114             try {
 115                 ref = rq.remove(1000);
 116             } catch (InterruptedException e) {
 117                 /* ignore */
 118             }
 119             if (ref != null) {
 120                 break;
 121             }
 122             System.gc(); // If the reference hasn't been queued yet, trigger one more GC.
 123         }
 124 
 125         if (ref == null) {
 126             dumpTestData();
 127             System.err.println("Method type: " + mtype);
 128             System.err.println("LambdaForm:  " + REF_FIELD.get(ph));
 129 
 130             if (HEAP_DUMP) {
 131                 // Trigger OOM to force heap dump for post-mortem analysis.
 132                 val = new long[1_000_000_000];
 133             }
 134             throw new AssertionError("Error: LambdaForm is not garbage collected");
 135         };
 136     }
 137 
 138     private void dumpTestData() {
 139         System.err.println("Test case: " + getTestMethod());
 140         for (String s : data.keySet()) {
 141             System.err.printf("\t%20s => %s\n", s, data.get(s));
 142         }
 143     }
 144 
 145     private static long[] val;
 146 
 147     /**
 148      * Main routine for lambda forms garbage collection test.
 149      *
 150      * @param args Accepts no arguments.
 151      */
 152     public static void main(String[] args) {
 153         // The "identity", "constant", "arrayElementGetter" and "arrayElementSetter"
 154         // methods should be removed from this test,
 155         // because their lambda forms are stored in a static field and are not GC'ed.
 156         // There can be only a finite number of such LFs for each method,
 157         // so no memory leak happens.
 158         EnumSet<TestMethods> testMethods = EnumSet.complementOf(EnumSet.of(
 159                 TestMethods.IDENTITY,
 160                 TestMethods.CONSTANT,
 161                 TestMethods.ARRAY_ELEMENT_GETTER,
 162                 TestMethods.ARRAY_ELEMENT_SETTER,
 163                 TestMethods.EXACT_INVOKER,
 164                 TestMethods.INVOKER));
 165         LambdaFormTestCase.runTests(LFGarbageCollectedTest::new, testMethods);
 166     }
 167 }