Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/AllocateWithoutOomTest/AllocateWithoutOomTest.java
41155 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 randomness
27
*
28
* @summary converted from VM Testbase gc/gctests/AllocateWithoutOomTest.
29
* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent, jrockit]
30
* VM Testbase readme:
31
* DESCRIPTION
32
* Small stress test that should be able to run for a specified
33
* time without hitting an OOM.
34
*
35
* COMMENTS
36
* This test was ported from JRockit test suite.
37
*
38
* @library /vmTestbase
39
* /test/lib
40
* @run main/othervm
41
* -XX:-UseGCOverheadLimit
42
* gc.gctests.AllocateWithoutOomTest.AllocateWithoutOomTest
43
*/
44
45
package gc.gctests.AllocateWithoutOomTest;
46
47
import java.util.ArrayList;
48
import java.util.Random;
49
import nsk.share.TestFailure;
50
import nsk.share.gc.GC;
51
import nsk.share.gc.GCTestBase;
52
import nsk.share.test.Stresser;
53
54
/**
55
* Small stress test that should be able to run for a specified
56
* time without hitting an OOM.
57
*/
58
public class AllocateWithoutOomTest extends GCTestBase {
59
60
/**
61
* Small stress test that allocates objects in a certain interval
62
* and runs for a specified time. It should not throw any OOM during
63
* the execution.
64
*
65
* @return success if the test runs for the specified time without
66
* and exceptions being thrown.
67
*/
68
@Override
69
public void run() {
70
int minSize;
71
int maxSize;
72
73
74
75
minSize = 2048;
76
maxSize = 32768;
77
78
79
ArrayList placeholder = new ArrayList();
80
long multiplier = maxSize - minSize;
81
Random rndGenerator = new Random(runParams.getSeed());
82
83
long memoryUpperLimit = runParams.getTestMemory();
84
long memoryLowerLimit = runParams.getTestMemory() / 3;
85
long memoryAllocatedLowerLimit = memoryUpperLimit
86
- memoryLowerLimit;
87
88
89
long totalAllocatedMemory = 0;
90
long totalAllocatedObjects = 0;
91
int allocationSize = -1;
92
long roundCounter = 1;
93
94
try {
95
Stresser stresser = new Stresser(runParams.getStressOptions());
96
stresser.start(0);
97
while (stresser.continueExecution()) {
98
while (totalAllocatedMemory < memoryUpperLimit) {
99
allocationSize = ((int) (rndGenerator.nextDouble()
100
* multiplier)) + minSize;
101
byte[] tmp = new byte[allocationSize];
102
totalAllocatedMemory += allocationSize;
103
totalAllocatedObjects++;
104
placeholder.add(tmp);
105
tmp = null;
106
}
107
108
// NOTE: Start on index 1 to make sure we don't remove to many
109
// consecutive objects in the beginning
110
int indexToRemove = 1;
111
112
while (totalAllocatedMemory > memoryAllocatedLowerLimit) {
113
// NOTE: Terminate if we only have zero objects left
114
if (placeholder.size() == 0) {
115
throw new TestFailure("No more objects to free, "
116
+ "so we can't continue");
117
}
118
119
if (indexToRemove >= placeholder.size()) {
120
indexToRemove = (placeholder.size() == 1) ? 0 : 1;
121
}
122
123
byte[] tmp = (byte[]) placeholder.remove(indexToRemove);
124
125
totalAllocatedMemory -= tmp.length;
126
totalAllocatedObjects--;
127
128
tmp = null;
129
// NOTE: Since we removed one object, we only need to
130
// increment index by 1 to move two steps. We want to
131
// remove every other object to create fragmentation
132
indexToRemove++;
133
}
134
135
roundCounter++;
136
}
137
placeholder = null;
138
log.info("Passed. Completed " + roundCounter
139
+ " rounds during the test");
140
} catch (OutOfMemoryError oome) {
141
placeholder = null;
142
throw new TestFailure("OOM thrown when allocating an object of size "
143
+ allocationSize, oome);
144
} finally {
145
placeholder = null;
146
}
147
}
148
149
public static void main(String[] args) {
150
GC.runTest(new AllocateWithoutOomTest(), args);
151
}
152
}
153
154