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 * @ignore 8057020 30 * @library /lib/testlibrary/jsr292 /lib/testlibrary 31 * @build TestMethods 32 * @build LambdaFormTestCase 33 * @build LFGarbageCollectedTest 34 * @run main/othervm/timeout=600 -Djava.lang.invoke.MethodHandle.USE_LF_EDITOR=true -DtestLimit=150 -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI LFGarbageCollectedTest 35 */ 36 37 import java.lang.invoke.MethodHandle; 38 import java.lang.ref.PhantomReference; 39 import java.lang.ref.ReferenceQueue; 40 import java.lang.reflect.InvocationTargetException; 41 import java.util.Arrays; 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 50 /** 51 * Constructor for a lambda forms garbage collection test case. 52 * 53 * @param testMethod A method from {@code j.l.i.MethodHandles} class that 54 * returns a {@code j.l.i.MethodHandle} instance. 55 */ 56 public LFGarbageCollectedTest(TestMethods testMethod) { 57 super(testMethod); 58 } 59 60 @Override 61 public void doTest() { 62 try { 63 Map<String, Object> data = getTestMethod().getTestCaseData(); 64 MethodHandle adapter; 65 try { 66 adapter = getTestMethod().getTestCaseMH(data, TestMethods.Kind.ONE); 67 } catch (NoSuchMethodException ex) { 68 throw new Error("Unexpected exception: ", ex); 69 } 70 Object lambdaForm = LambdaFormTestCase.INTERNAL_FORM.invoke(adapter); 71 if (lambdaForm == null) { 72 throw new Error("Unexpected error: Lambda form of the method handle is null"); 73 } 74 ReferenceQueue rq = new ReferenceQueue(); 75 PhantomReference ph = new PhantomReference(lambdaForm, rq); 76 lambdaForm = null; 77 data = null; 78 adapter = null; 79 for (int i = 0; i < 1000 && !ph.isEnqueued(); i++) { 80 System.gc(); 81 } 82 if (!ph.isEnqueued()) { 83 throw new AssertionError("Error: Lambda form is not garbage collected"); 84 } 85 } catch (IllegalAccessException | IllegalArgumentException | 86 InvocationTargetException ex) { 87 throw new Error("Unexpected exception: ", ex); 88 } 89 } 90 91 /** 92 * Main routine for lambda forms garbage collection test. 93 * 94 * @param args Accepts no arguments. 95 */ 96 public static void main(String[] args) { 97 // The "identity" and "constant" methods should be removed from this test, 98 // because their lambda forms are stored in a static filed and are not GC'ed. 99 // There can be only 5 such LFs for each method, so no memory leak happens. 100 EnumSet<TestMethods> testMethods = EnumSet.complementOf(EnumSet.of(TestMethods.IDENTITY, TestMethods.CONSTANT)); 101 LambdaFormTestCase.runTests(LFGarbageCollectedTest::new, testMethods); 102 } 103 }