Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/WeakReference/WeakReferenceTest/WeakReferenceTest.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
package gc.gctests.WeakReference;
24
25
import java.util.Random;
26
import java.util.ArrayList;
27
import java.lang.ref.WeakReference;
28
import nsk.share.TestFailure;
29
import nsk.share.gc.GC;
30
import nsk.share.gc.GCTestBase;
31
32
/**
33
* Original description from JRockit test:
34
*
35
* Tests for the WeakReference handling in JRockit.
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 WeakReferenceTest extends GCTestBase {
42
43
/**
44
* Test that all WeakReferences has been cleared after
45
* the GC has run. This test should not throw an OOME
46
* during the test execution.
47
*
48
*/
49
@Override
50
public void run() {
51
long seed;
52
int minSize;
53
int maxSize;
54
double memPercentToFill;
55
long totalMemAlloced = 0;
56
long memToAlloc = 0;
57
Runtime r = Runtime.getRuntime();
58
59
seed = runParams.getSeed();
60
minSize = 2048;
61
maxSize = 32768;
62
63
memPercentToFill = 0.45;
64
65
memToAlloc = (long) (r.maxMemory() * memPercentToFill);
66
67
Random rndGenerator = new Random(seed);
68
long multiplier = maxSize - minSize;
69
ArrayList arrWeakRefs = new ArrayList();
70
71
try {
72
while (totalMemAlloced < memToAlloc) {
73
int allocationSize = ((int) (rndGenerator.nextDouble()
74
* multiplier)) + minSize;
75
byte[] tmp = new byte[allocationSize];
76
77
arrWeakRefs.add(new WeakReference(tmp));
78
totalMemAlloced += allocationSize;
79
80
// Make sure the temporary object is dereferenced
81
tmp = null;
82
}
83
84
System.gc();
85
86
long numberOfNotNulledObjects = 0;
87
88
for (int i = 0; i < arrWeakRefs.size(); i++) {
89
WeakReference wr = (WeakReference) arrWeakRefs.get(i);
90
Object o = wr.get();
91
92
if (o != null) {
93
numberOfNotNulledObjects++;
94
}
95
}
96
97
if (numberOfNotNulledObjects > 0) {
98
throw new TestFailure(numberOfNotNulledObjects + " out of "
99
+ arrWeakRefs.size() + " WeakReferences was not "
100
+ "null after the GC had run");
101
}
102
103
log.info("All WeakReferences was cleared after the "
104
+ "GC had run");
105
} catch (OutOfMemoryError oome) {
106
throw new TestFailure("OutOfMemoryException was thrown. This should "
107
+ "not happen during the execution of this test.");
108
} finally {
109
arrWeakRefs = null;
110
}
111
}
112
113
public static void main(String[] args) {
114
GC.runTest(new WeakReferenceTest(), args);
115
}
116
}
117
118