Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/FinalizeTest01/FinalizeTest01.java
41159 views
1
/*
2
* Copyright (c) 2002, 2020, 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
* @key stress randomness
27
*
28
* @summary converted from VM Testbase gc/gctests/FinalizeTest01.
29
* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent, monitoring]
30
*
31
* @library /vmTestbase
32
* /test/lib
33
* @run main/othervm -XX:-UseGCOverheadLimit gc.gctests.FinalizeTest01.FinalizeTest01
34
*/
35
36
package gc.gctests.FinalizeTest01;
37
38
import java.lang.management.ManagementFactory;
39
import java.lang.management.MemoryMXBean;
40
import nsk.share.gc.*;
41
import nsk.share.TestFailure;
42
import nsk.share.test.ExecutionController;
43
import nsk.share.test.Stresser;
44
45
/**
46
* Tests that GC works correctly with finalizers.
47
*
48
* Create a number of objects, some of which register their
49
* creation/finalization. Then try to eat available memory
50
* which forces garbage collection of created objects.
51
* Garbage collector should try to collect the garbage and thus
52
* call finalizers. The test checks that all finalizers have been called.
53
*/
54
55
/*
56
Reworked original test (4933478). Original comment:
57
58
Tests that objects w/finalizers will eventually finalize
59
(and by the way, the system doesn't crash!).
60
61
Returns exit code 0 on success, and 1 on failure.
62
63
*/
64
/**
65
* Class with finalizer that throws Exception.
66
* Used for FinalizeTest02
67
*/
68
class FinExceptMemoryObject extends FinMemoryObject {
69
70
public FinExceptMemoryObject(int size) {
71
super(size);
72
}
73
74
protected void finalize() {
75
super.finalize();
76
throw new RuntimeException("Exception in finalizer");
77
}
78
}
79
80
public class FinalizeTest01 extends GCTestBase {
81
82
private final int allocRatio = 5;
83
private final int size = 1024 * 2;
84
private int count = 1000;
85
private static boolean throwExceptions = false;
86
private ExecutionController stresser;
87
88
private void runOne() {
89
Object o;
90
for (int i = 0; i < count; i++) {
91
if (i % allocRatio == 0) {
92
if (throwExceptions) {
93
o = new FinExceptMemoryObject(size);
94
} else {
95
o = new FinMemoryObject(size);
96
}
97
} else {
98
o = new byte[size - Memory.getObjectExtraSize()];
99
}
100
}
101
o = null;
102
103
MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
104
long finalizationMaxTime = 1000 * 60; // 1min
105
106
/* Provoke GC to start finalization. */
107
Algorithms.eatMemory(stresser);
108
if (!stresser.continueExecution()) {
109
// we did not eat all memory
110
return;
111
}
112
long waitTime = System.currentTimeMillis() + finalizationMaxTime;
113
114
/*
115
* Before we force finalization it is needed to check that we have
116
* any object pending for finazlization. If not then is a GC bug.
117
*/
118
while (FinMemoryObject.getFinalizedCount()
119
+ mbean.getObjectPendingFinalizationCount() == 0
120
&& (System.currentTimeMillis() < waitTime)) {
121
System.out.println("No objects are found in the finalization queue. Waiting..");
122
try {
123
Thread.sleep(1000);
124
} catch (InterruptedException ie) {
125
}
126
}
127
if (FinMemoryObject.getFinalizedCount()
128
+ mbean.getObjectPendingFinalizationCount() == 0) {
129
throw new TestFailure("Test failed. (No objects were not queued for finalization during 1min)");
130
}
131
132
/* force finalization and wait for it finishs */
133
Runtime.getRuntime().runFinalization();
134
135
boolean error = (FinMemoryObject.getLiveCount() != 0);
136
137
/*
138
* The runFinalization() starts the second finalization thread and wait until it finishs.
139
* However it is a very little probability (less then 1%) that not all object are finalized yet.
140
* Possibly the regular Finalizer thread have not finished its work when we check getLiveCount()
141
* or GC is still clearing memory and adding objects to the queue.
142
*/
143
waitTime = System.currentTimeMillis() + finalizationMaxTime;
144
while (error && (System.currentTimeMillis() < waitTime)) {
145
// wait 1 sec (it could be less due to potential InterruptedException)
146
try {
147
Thread.sleep(1000);
148
} catch (InterruptedException ie) {
149
}
150
error = (FinMemoryObject.getLiveCount() != 0);
151
}
152
153
if (error) {
154
throw new TestFailure("Test failed (objects were not finalized during 1min)");
155
}
156
157
158
System.out.println("Allocated: " + FinMemoryObject.getAllocatedCount());
159
System.out.println("Finalized: " + FinMemoryObject.getFinalizedCount());
160
error = (FinMemoryObject.getLiveCount() != 0);
161
if (error) {
162
throw new TestFailure("Test failed.");
163
}
164
}
165
166
public void run() {
167
stresser = new Stresser(runParams.getStressOptions());
168
stresser.start(runParams.getIterations());
169
count = (int) Math.min(runParams.getTestMemory() / size, Integer.MAX_VALUE);
170
System.out.println("Allocating " + count
171
+ " objects. 1 out of " + allocRatio
172
+ " will have a finalizer.");
173
System.out.flush();
174
runOne();
175
}
176
177
public static void main(String[] args) {
178
for (int i = 0; i < args.length; ++i) {
179
if (args[i].equals("-throwExceptions")) {
180
throwExceptions = true;
181
}
182
}
183
GC.runTest(new FinalizeTest01(), args);
184
}
185
}
186
187