Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/WeakReference/WeakReferenceEvilTest/WeakReferenceEvilTest.java
41161 views
1
/*
2
* Copyright (c) 2004, 2018, 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
package gc.gctests.WeakReference;
25
26
import java.util.Random;
27
import java.util.ArrayList;
28
import java.lang.ref.WeakReference;
29
import nsk.share.TestFailure;
30
import nsk.share.gc.GC;
31
import nsk.share.gc.GCTestBase;
32
33
/**
34
* Original description from JRockit test:
35
* Tests for the WeakReference handling in JRockit (in a more evil way :)).
36
*
37
* This test must be run with a mx value set, as well as with
38
* -XXfullsystemgc, to ensure Runtime.maxMemory() doesn't return 0
39
* and that System.gc() gets run when called from Java.
40
*/
41
public class WeakReferenceEvilTest extends GCTestBase {
42
43
/**
44
* Test the WeakReference handling in a more evil way. In this test,
45
* it will only keep every Xth object, thus causing more fragmentation
46
* and fill the heap with unused objects. This test should not
47
* throw any OOME during the test execution.
48
*
49
* @return success if all WeakReferences are NULL after
50
* the test has run
51
*/
52
public void run() {
53
long seed;
54
int minSize;
55
int maxSize;
56
int keepEveryXthObject;
57
double memPercentToFill;
58
long counter = 0;
59
long totalMemAlloced = 0;
60
long memToAlloc = 0;
61
Runtime r = Runtime.getRuntime();
62
63
seed = runParams.getSeed();
64
minSize = 2048;
65
maxSize = 32768;
66
memPercentToFill = 0.45;
67
keepEveryXthObject = 5;
68
69
memToAlloc = (long) (r.maxMemory() * memPercentToFill);
70
71
Random rndGenerator = new Random(seed);
72
long multiplier = maxSize - minSize;
73
ArrayList arrWeakRefs = new ArrayList();
74
75
try {
76
while (totalMemAlloced < memToAlloc) {
77
int allocationSize = ((int) (rndGenerator.nextDouble()
78
* multiplier)) + minSize;
79
byte[] tmp = new byte[allocationSize];
80
81
if (counter % keepEveryXthObject == 0) {
82
arrWeakRefs.add(new WeakReference(tmp));
83
totalMemAlloced += allocationSize;
84
}
85
86
// Make sure the temporary object is dereferenced
87
tmp = null;
88
89
counter++;
90
if (counter == Long.MAX_VALUE) {
91
counter = 0;
92
}
93
}
94
95
System.gc();
96
97
long numberOfNotNulledObjects = 0;
98
99
for (int i = 0; i < arrWeakRefs.size(); i++) {
100
WeakReference wr = (WeakReference) arrWeakRefs.get(i);
101
Object o = wr.get();
102
103
if (o != null) {
104
numberOfNotNulledObjects++;
105
}
106
}
107
108
if (numberOfNotNulledObjects > 0) {
109
throw new TestFailure(numberOfNotNulledObjects + " out of "
110
+ arrWeakRefs.size() + " WeakReferences was not "
111
+ "null after the GC had run");
112
}
113
114
log.info("All WeakReferences was cleared after the "
115
+ "GC had run");
116
} catch (OutOfMemoryError oome) {
117
throw new TestFailure("OutOfMemoryException was thrown. This should "
118
+ "not happen during the execution of this test.");
119
} finally {
120
arrWeakRefs = null;
121
}
122
}
123
public static void main(String[] args) {
124
GC.runTest(new WeakReferenceEvilTest(), args);
125
}
126
}
127
128