LFGarbageCollectedTest.java revision 11223:75c351a01d3c
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 36import java.lang.invoke.MethodHandle; 37import java.lang.invoke.MethodType; 38import java.lang.ref.PhantomReference; 39import java.lang.ref.Reference; 40import java.lang.ref.ReferenceQueue; 41import java.lang.reflect.InvocationTargetException; 42import java.util.EnumSet; 43import java.util.Map; 44 45/** 46 * Lambda forms garbage collection test class. 47 */ 48public 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 101 private void collectLambdaForm() throws IllegalAccessException { 102 // Usually, 2 System.GCs are necessary to enqueue a SoftReference. 103 System.gc(); 104 System.gc(); 105 106 Reference ref = null; 107 for (int i = 0; i < 10; i++) { 108 try { 109 ref = rq.remove(1000); 110 } catch (InterruptedException e) { 111 /* ignore */ 112 } 113 if (ref != null) { 114 break; 115 } 116 System.gc(); // If the reference hasn't been queued yet, trigger one more GC. 117 } 118 119 if (ref == null) { 120 dumpTestData(); 121 System.err.println("Method type: " + mtype); 122 System.err.println("LambdaForm: " + REF_FIELD.get(ph)); 123 124 if (HEAP_DUMP) { 125 // Trigger OOM to force heap dump for post-mortem analysis. 126 val = new long[1_000_000_000]; 127 } 128 throw new AssertionError("Error: LambdaForm is not garbage collected"); 129 }; 130 } 131 132 private void dumpTestData() { 133 System.err.println("Test case: " + getTestMethod()); 134 for (String s : data.keySet()) { 135 System.err.printf("\t%20s => %s\n", s, data.get(s)); 136 } 137 } 138 139 private static long[] val; 140 141 /** 142 * Main routine for lambda forms garbage collection test. 143 * 144 * @param args Accepts no arguments. 145 */ 146 public static void main(String[] args) { 147 // The "identity", "constant", "arrayElementGetter" and "arrayElementSetter" 148 // methods should be removed from this test, 149 // because their lambda forms are stored in a static field and are not GC'ed. 150 // There can be only a finite number of such LFs for each method, 151 // so no memory leak happens. 152 EnumSet<TestMethods> testMethods = EnumSet.complementOf(EnumSet.of( 153 TestMethods.IDENTITY, 154 TestMethods.CONSTANT, 155 TestMethods.ARRAY_ELEMENT_GETTER, 156 TestMethods.ARRAY_ELEMENT_SETTER, 157 TestMethods.EXACT_INVOKER, 158 TestMethods.INVOKER)); 159 LambdaFormTestCase.runTests(LFGarbageCollectedTest::new, testMethods); 160 } 161} 162