Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/MemoryMXBean/Pending.java
38821 views
1
/*
2
* Copyright (c) 2003, 2010, 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
26
* @bug 4530538
27
* @summary Basic unit test of
28
* RuntimeMXBean.getObjectPendingFinalizationCount()
29
* 1. GC and runFinalization() to get the current pending number
30
* 2. Create some number of objects with reference and without ref.
31
* 3. Clear all the references
32
* 4. GC and runFinalization() and the finalizable objects should
33
* be garbage collected.
34
* @author Alexei Guibadoulline and Mandy Chung
35
*
36
*/
37
38
import java.lang.management.*;
39
40
public class Pending {
41
final static int NO_REF_COUNT = 600;
42
final static int REF_COUNT = 600;
43
final static int TOTAL_FINALIZABLE = (NO_REF_COUNT + REF_COUNT);
44
private static int finalized = 0;
45
private static MemoryMXBean mbean
46
= ManagementFactory.getMemoryMXBean();
47
48
private static final String INDENT = " ";
49
private static void printFinalizerInstanceCount() {
50
if (!trace) return;
51
52
int count = sun.misc.VM.getFinalRefCount();
53
System.out.println(INDENT + "Finalizable object Count = " + count);
54
55
count = sun.misc.VM.getPeakFinalRefCount();
56
System.out.println(INDENT + "Peak Finalizable object Count = " + count);
57
}
58
59
private static boolean trace = false;
60
public static void main(String argv[]) throws Exception {
61
if (argv.length > 0 && argv[0].equals("trace")) {
62
trace = true;
63
}
64
65
try {
66
if (trace) {
67
// Turn on verbose:gc to track GC
68
mbean.setVerbose(true);
69
}
70
test();
71
} finally {
72
if (trace) {
73
mbean.setVerbose(false);
74
}
75
}
76
System.out.println("Test passed.");
77
}
78
79
private static void test() throws Exception {
80
// Clean the memory and remove all objects that are pending
81
// finalization
82
System.gc();
83
Runtime.getRuntime().runFinalization();
84
85
// Let the finalizer to finish
86
try {
87
Thread.sleep(200);
88
} catch (Exception e) {
89
throw e;
90
}
91
92
// Create a number of new objects but no references to them
93
int startCount = mbean.getObjectPendingFinalizationCount();
94
95
System.out.println("Number of objects pending for finalization:");
96
System.out.println(" Before creating object: " + startCount +
97
" finalized = " + finalized);
98
printFinalizerInstanceCount();
99
100
for (int i = 0; i < NO_REF_COUNT; i++) {
101
new MyObject();
102
}
103
104
Snapshot snapshot = getSnapshot();
105
System.out.println(" Afer creating objects with no ref: " + snapshot);
106
printFinalizerInstanceCount();
107
108
Object[] objs = new Object[REF_COUNT];
109
for (int i = 0; i < REF_COUNT; i++) {
110
objs[i] = new MyObject();
111
}
112
snapshot = getSnapshot();
113
System.out.println(" Afer creating objects with ref: " + snapshot);
114
printFinalizerInstanceCount();
115
116
// Now check the expected count - GC and runFinalization will be
117
// invoked.
118
checkFinalizerCount(NO_REF_COUNT, 0);
119
120
// Clean the memory and remove all objects that are pending
121
// finalization again
122
objs = null;
123
snapshot = getSnapshot();
124
System.out.println("Clear all references finalized = " + snapshot);
125
printFinalizerInstanceCount();
126
127
checkFinalizerCount(TOTAL_FINALIZABLE, NO_REF_COUNT);
128
129
snapshot = getSnapshot();
130
printFinalizerInstanceCount();
131
132
// Check the mbean now
133
if (snapshot.curFinalized != TOTAL_FINALIZABLE) {
134
throw new RuntimeException("Wrong number of finalized objects "
135
+ snapshot + ". Expected "
136
+ TOTAL_FINALIZABLE);
137
}
138
139
if (startCount != 0 || snapshot.curPending != 0) {
140
throw new RuntimeException("Wrong number of objects pending "
141
+ "finalization start = " + startCount
142
+ " end = " + snapshot);
143
}
144
145
}
146
147
private static void checkFinalizerCount(int expectedTotal, int curFinalized)
148
throws Exception {
149
int prevCount = -1;
150
Snapshot snapshot = getSnapshot();
151
if (snapshot.curFinalized != curFinalized) {
152
throw new RuntimeException(
153
"Unexpected finalized objects: " + snapshot +
154
" but expected = " + curFinalized);
155
}
156
int MAX_GC_LOOP = 6;
157
for (int i = 1;
158
snapshot.curFinalized != expectedTotal && i <= MAX_GC_LOOP;
159
i++) {
160
System.gc();
161
162
// Pause to give a chance to Finalizer thread to run
163
pause();
164
165
printFinalizerInstanceCount();
166
// Race condition may occur; attempt to check this
167
// a few times before throwing exception.
168
for (int j = 0; j < 5; j++) {
169
// poll for another current pending count
170
snapshot = getSnapshot();
171
if (snapshot.curFinalized == expectedTotal ||
172
snapshot.curPending != 0) {
173
break;
174
}
175
}
176
System.out.println(" After GC " + i + ": " + snapshot);
177
178
Runtime.getRuntime().runFinalization();
179
180
// Pause to give a chance to Finalizer thread to run
181
pause();
182
183
snapshot = getSnapshot();
184
if (snapshot.curFinalized == expectedTotal &&
185
snapshot.curPending != 0) {
186
throw new RuntimeException(
187
"Unexpected current number of objects pending for " +
188
"finalization: " + snapshot + " but expected = 0");
189
}
190
191
System.out.println(" After runFinalization " + i + ": " + snapshot);
192
printFinalizerInstanceCount();
193
194
try {
195
Thread.sleep(1000);
196
} catch (Exception e) {
197
throw e;
198
}
199
}
200
if (snapshot.curFinalized != expectedTotal) {
201
throw new RuntimeException(
202
"Unexpected current number of objects pending for " +
203
"finalization: " + snapshot + " but expected > 0");
204
}
205
}
206
207
private static Object lock = new Object();
208
private static class MyObject {
209
Object[] dummy = new Object[10];
210
public void finalize () {
211
synchronized (lock) {
212
finalized++;
213
}
214
}
215
}
216
217
static class Snapshot {
218
public int curFinalized;
219
public int curPending;
220
Snapshot(int f, int p) {
221
curFinalized = f;
222
curPending = p;
223
}
224
public String toString() {
225
return "Current finalized = " + curFinalized +
226
" Current pending = " + curPending;
227
}
228
}
229
230
private static Snapshot getSnapshot() {
231
synchronized (lock) {
232
int curCount = mbean.getObjectPendingFinalizationCount();
233
return new Snapshot(finalized, curCount);
234
}
235
}
236
237
private static Object pauseObj = new Object();
238
private static void pause() {
239
// Enter lock a without blocking
240
synchronized (pauseObj) {
241
try {
242
// may need to tune this timeout for different platforms
243
pauseObj.wait(20);
244
} catch (Exception e) {
245
System.err.println("Unexpected exception.");
246
e.printStackTrace(System.err);
247
}
248
}
249
}
250
}
251
252