Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-aarch32-jdk8u
Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/lang/invoke/LFCaching/LFGarbageCollectedTest.java
48795 views
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
* @ignore 8078602
28
* @summary Test verifies that lambda forms are garbage collected
29
* @author kshefov
30
* @library /lib/testlibrary/jsr292 /lib/testlibrary
31
* @build TestMethods
32
* @build LambdaFormTestCase
33
* @build LFGarbageCollectedTest
34
* @run main/othervm -Xmx64m -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+HeapDumpOnOutOfMemoryError -DHEAP_DUMP=false LFGarbageCollectedTest
35
*/
36
37
import java.lang.invoke.MethodHandle;
38
import java.lang.invoke.MethodType;
39
import java.lang.ref.PhantomReference;
40
import java.lang.ref.Reference;
41
import java.lang.ref.ReferenceQueue;
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
Map<String, Object> data;
66
67
@Override
68
public void doTest() {
69
try {
70
TestMethods testCase = getTestMethod();
71
data = testCase.getTestCaseData();
72
MethodHandle adapter;
73
try {
74
adapter = testCase.getTestCaseMH(data, TestMethods.Kind.ONE);
75
} catch (NoSuchMethodException ex) {
76
throw new Error("Unexpected exception", ex);
77
}
78
mtype = adapter.type();
79
Object lambdaForm = INTERNAL_FORM.invoke(adapter);
80
if (lambdaForm == null) {
81
throw new Error("Unexpected error: Lambda form of the method handle is null");
82
}
83
84
String debugName = (String)DEBUG_NAME.get(lambdaForm);
85
if (debugName != null && debugName.startsWith("identity_")) {
86
// Ignore identity_* LambdaForms.
87
return;
88
}
89
90
ph = new PhantomReference(lambdaForm, rq);
91
lambdaForm = null;
92
adapter = null;
93
94
collectLambdaForm();
95
} catch (IllegalAccessException | IllegalArgumentException |
96
InvocationTargetException ex) {
97
throw new Error("Unexpected exception", ex);
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
163