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