Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/HeapUsageTest/HeapUsageTest.java
41159 views
1
/*
2
* Copyright (c) 2011, 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
27
*
28
* @summary converted from VM Testbase gc/gctests/HeapUsageTest.
29
* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent, jrockit]
30
* VM Testbase readme:
31
* DESCRIPTION
32
* Originally it was Micro benchmark that tests the heap usage.
33
*
34
* COMMENTS
35
* This test was ported from JRockit test suite.
36
*
37
* @library /vmTestbase
38
* /test/lib
39
* @run main/othervm -XX:-UseGCOverheadLimit gc.gctests.HeapUsageTest.HeapUsageTest
40
*/
41
42
package gc.gctests.HeapUsageTest;
43
44
import java.util.ArrayList;
45
import nsk.share.TestFailure;
46
import nsk.share.gc.GC;
47
import nsk.share.gc.GCTestBase;
48
import nsk.share.test.Stresser;
49
50
/**
51
* Micro benchmark that tests the heap usage.
52
*/
53
public class HeapUsageTest extends GCTestBase {
54
55
/**
56
* Helper class to store allocation size and iterations for the
57
* HeapUsageTest heap usage test program
58
*/
59
private class TestValue {
60
61
private int allocationSize;
62
private int allocationIterations;
63
64
TestValue(int allocSize, int allocIters) {
65
allocationSize = allocSize;
66
allocationIterations = allocIters;
67
}
68
69
final int getSize() {
70
return allocationSize;
71
}
72
73
final int getIterations() {
74
return allocationIterations;
75
}
76
}
77
78
/**
79
* Simple micro benchmark for testing heap usage. Returns a percentage
80
* that tells how much of the total heap size the test was able to
81
* allocate.
82
*
83
* @return success if test could run until OOME was thrown, and was
84
* able to determine the heap usage in percent without any other
85
* exceptions being thrown.
86
*/
87
public void run() {
88
89
try {
90
int[] testParams =
91
new int[]{512, 5, 2048, 3, 3145728, 2};
92
93
94
TestValue[] values = new TestValue[testParams.length / 2];
95
for (int i = 0; i < testParams.length / 2; i++) {
96
values[i] = new TestValue(testParams[i * 2],
97
testParams[i * 2 + 1]);
98
}
99
100
// NOTE: The call to Runtime might not look like it does anything
101
// here, but it codegens the class, so it will not OOM later on
102
// due to low-mem sitation for codegen.
103
Runtime r = Runtime.getRuntime();
104
// NOTE: Codegen freeMemory() and maxMemory() so this
105
// doesn't cause OOM in a OOM situation
106
ArrayList holdObjects = new ArrayList();
107
long currentAllocatedSize = 0;
108
Stresser stresser = new Stresser(runParams.getStressOptions());
109
stresser.start(0);
110
try {
111
long loopCount;
112
int nrOfLoops = 0;
113
114
for (int i = 0; i < values.length; i++) {
115
if (values[i].getIterations() > nrOfLoops) {
116
nrOfLoops = values[i].getIterations();
117
}
118
}
119
120
for (loopCount = 0;; loopCount++) {
121
for (int i = 0; i < nrOfLoops; i++) {
122
for (int k = 0; k < values.length; k++) {
123
if (i < values[k].getIterations()) {
124
if (!stresser.continueExecution()) {
125
// no time to eat all heap
126
return;
127
}
128
byte[] tmp = new byte[values[k].getSize()];
129
holdObjects.add(tmp);
130
currentAllocatedSize += (long) values[k].getSize();
131
}
132
}
133
}
134
}
135
} catch (OutOfMemoryError oome) {
136
long oomMaxMemory = r.maxMemory();
137
138
holdObjects = null;
139
140
double myPercentUsed =
141
(((double) (currentAllocatedSize))
142
/ oomMaxMemory) * 100;
143
144
log.info("Heap usage percentage ( "
145
+ myPercentUsed + " %) " + myPercentUsed);
146
} finally {
147
// NOTE: In case OOM wasn't hit, release references
148
// and cleanup by calling System.gc();
149
holdObjects = null;
150
}
151
} catch (OutOfMemoryError oome2) {
152
throw new TestFailure("OutOfMemoryError thrown even though it shouldn't. "
153
+ "Please investigate.", oome2);
154
}
155
}
156
157
public static void main(String[] args) {
158
GC.runTest(new HeapUsageTest(), args);
159
}
160
}
161
162