Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/SoftReference/SoftReferenceTest/SoftReferenceTest.java
41161 views
1
/*
2
* Copyright (c) 2004, 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/SoftReference/SoftReferenceTest.
29
* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent, jrockit]
30
* VM Testbase readme:
31
* DESCRIPTION
32
* Test that all SoftReferences has been cleared at time of OOM.
33
*
34
* COMMENTS
35
* This test was ported from JRockit test suite.
36
*
37
* @library /vmTestbase
38
* /test/lib
39
* @run main/othervm gc.gctests.SoftReference.SoftReferenceTest.SoftReferenceTest -stressTime 600
40
*/
41
42
package gc.gctests.SoftReference.SoftReferenceTest;
43
44
import java.lang.ref.SoftReference;
45
import java.util.ArrayList;
46
import java.util.Random;
47
import nsk.share.TestFailure;
48
import nsk.share.gc.GC;
49
import nsk.share.gc.GCTestBase;
50
import nsk.share.test.ExecutionController;
51
import nsk.share.test.Stresser;
52
53
/**
54
* Tests for the SoftReference handling in JRockit.
55
*/
56
public final class SoftReferenceTest extends GCTestBase {
57
58
private ExecutionController stresser;
59
60
/**
61
* Test that all SoftReferences has been cleared at
62
* time of OOM.
63
*
64
* @return success if all SoftReferences are NULL at
65
* time of OOM.
66
*/
67
public void run() {
68
//prepare stresser
69
stresser = new Stresser("Stresser to limit execution time", runParams.getStressOptions());
70
stresser.start(1);
71
72
long seed;
73
int minSize;
74
int maxSize;
75
int keepEveryXthObject;
76
long counter = 0;
77
78
seed = runParams.getSeed();
79
minSize = 2048;
80
maxSize = 32768;
81
keepEveryXthObject = 4;
82
83
84
Random rndGenerator = new Random(seed);
85
long multiplier = maxSize - minSize;
86
ArrayList arrSoftRefs = new ArrayList();
87
ArrayList arrObjects = new ArrayList();
88
long numberOfNotNulledObjects = 0;
89
long oomSoftArraySize = 0;
90
91
try {
92
while (true && stresser.continueExecution()) {
93
int allocationSize = ((int) (rndGenerator.nextDouble()
94
* multiplier)) + minSize;
95
byte[] tmp = new byte[allocationSize];
96
97
// Keep every Xth object to make sure we hit OOM pretty fast
98
if (counter % keepEveryXthObject == 0) {
99
arrObjects.add(tmp);
100
} else {
101
arrSoftRefs.add(new SoftReference(tmp));
102
}
103
104
// Make sure the temporary object is dereferenced
105
tmp = null;
106
107
counter++;
108
if (counter == Long.MAX_VALUE) {
109
counter = 0;
110
}
111
}
112
} catch (OutOfMemoryError oome) {
113
// Get the number of soft refs first, so we don't trigger
114
// another OOM.
115
oomSoftArraySize = arrSoftRefs.size();
116
117
for (int i = 0; i < arrSoftRefs.size(); i++) {
118
SoftReference sr = (SoftReference) arrSoftRefs.get(i);
119
Object o = sr.get();
120
121
if (o != null) {
122
numberOfNotNulledObjects++;
123
}
124
}
125
126
// Make sure we clear all refs before we return failure, since
127
// coconut require some memory to complete, and since we're in
128
// an OOM situation, that could cause trouble.
129
130
arrSoftRefs = null;
131
arrObjects = null;
132
133
if (numberOfNotNulledObjects > 0) {
134
throw new TestFailure(numberOfNotNulledObjects + " out of "
135
+ oomSoftArraySize + " SoftReferences was not "
136
+ "null at time of OutOfMemoryError");
137
}
138
} finally {
139
arrSoftRefs = null;
140
arrObjects = null;
141
}
142
143
144
}
145
146
public static void main(String[] args) {
147
GC.runTest(new SoftReferenceTest(), args);
148
}
149
}
150
151