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