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 36 import java.lang.invoke.MethodHandle; 37 import java.lang.invoke.MethodType; 38 import java.lang.ref.PhantomReference; 39 import java.lang.ref.Reference; 40 import java.lang.ref.ReferenceQueue; 41 import java.lang.reflect.Field; 42 import java.lang.reflect.InvocationTargetException; 43 import java.util.EnumSet; 44 import java.util.Map; 45 46 /** 47 * Lambda forms garbage collection test class. 48 */ 49 public final class LFGarbageCollectedTest extends LambdaFormTestCase { 50 private static boolean HEAP_DUMP = Boolean.getBoolean("HEAP_DUMP"); 51 52 /** 53 * Constructor for a lambda forms garbage collection test case. 54 * 55 * @param testMethod A method from {@code j.l.i.MethodHandles} class that 56 * returns a {@code j.l.i.MethodHandle} instance. 57 */ 58 public LFGarbageCollectedTest(TestMethods testMethod) { 59 super(testMethod); 60 } 61 62 PhantomReference ph; 63 ReferenceQueue rq = new ReferenceQueue(); 64 MethodType mtype; 65 66 @Override 67 public void doTest() { 68 try { 69 TestMethods testCase = getTestMethod(); 70 if (testCase == TestMethods.EXACT_INVOKER || testCase == TestMethods.INVOKER) { 71 // Invokers aren't collected. 72 return; 73 } 74 Map<String, Object> data = testCase.getTestCaseData(); 75 MethodHandle adapter; 76 try { 77 adapter = getTestMethod().getTestCaseMH(data, TestMethods.Kind.ONE); 78 } catch (NoSuchMethodException ex) { 79 throw new Error("Unexpected exception: ", ex); 80 } 81 mtype = adapter.type(); 82 Object lambdaForm = LambdaFormTestCase.INTERNAL_FORM.invoke(adapter); 83 if (lambdaForm == null) { 84 throw new Error("Unexpected error: Lambda form of the method handle is null"); 85 } 86 ph = new PhantomReference(lambdaForm, rq); 87 lambdaForm = null; 88 data = null; 89 adapter = null; 90 91 collectLambdaForm(); 92 } catch (IllegalAccessException | IllegalArgumentException | 93 InvocationTargetException ex) { 94 throw new Error("Unexpected exception: ", ex); 95 } 96 } 97 98 99 private void collectLambdaForm() throws IllegalAccessException { 100 // Usually, 2 System.GCs are necessary to enqueue a SoftReference. 101 System.gc(); 102 System.gc(); 103 104 Reference ref = null; 105 for (int i = 0; i < 10; i++) { 106 try { 107 ref = rq.remove(1000); 108 } catch (InterruptedException e) { 109 /* ignore */ 110 } 111 if (ref != null) { 112 break; 113 } 114 System.gc(); // If a reference hasn't been queued yet, trigger one more GC. 115 } 116 117 if (ref == null) { 118 dumpTestCase(); 119 System.out.println("Method type: "+mtype); 120 System.out.println("LambdaForm: "+REF_FIELD.get(ph)); 121 122 if (HEAP_DUMP) { 123 // Trigger OOM to force heap dump for post-mortem analysis. 124 val = new long[1_000_000_000]; 125 } 126 throw new AssertionError("Error: LambdaForm is not garbage collected"); 127 }; 128 } 129 130 private static long[] val; 131 private static final Field REF_FIELD; 132 static { 133 try { 134 REF_FIELD = Reference.class.getDeclaredField("referent"); 135 REF_FIELD.setAccessible(true); 136 } catch (Exception e) { 137 throw new Error(e); 138 } 139 } 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 LambdaFormTestCase.runTests(LFGarbageCollectedTest::new, testMethods); 158 } 159 }