Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/ref/SoftReference/Pin.java
38828 views
1
/*
2
* Copyright (c) 1997, 2005, 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
/* @test
25
* @bug 4076287
26
* @summary Invoking get on a SoftReference shouldn't pin the referent
27
* @run main/othervm -ms16m -mx16m Pin
28
* @author Peter Jones
29
* @author Mark Reinhold
30
*/
31
32
33
import java.lang.ref.SoftReference;
34
35
public class Pin {
36
37
final static int NUM_BLOCKS = 128;
38
final static int BLOCK_SIZE = 32768;
39
40
public static void main(String[] args) throws Exception {
41
42
SoftReference[] blocks = new SoftReference[NUM_BLOCKS];
43
44
byte[] block;
45
46
System.err.println("Filling array with " + NUM_BLOCKS +
47
" SoftReferences to blocks of " +
48
BLOCK_SIZE + " bytes.");
49
50
for (int i = 0; i < NUM_BLOCKS; ++ i) {
51
block = new byte[BLOCK_SIZE];
52
SoftReference ref = new SoftReference(block);
53
blocks[i] = ref;
54
}
55
block = null;
56
57
System.err.println("Allowing SoftReferences to be enqueued.");
58
System.gc();
59
Thread.sleep(1000);
60
61
/* -- Commenting out the following section will hide the bug -- */
62
System.err.println("Invoking get() on SoftReferences.");
63
for (int i = 0; i < NUM_BLOCKS; ++ i) {
64
block = (byte[]) blocks[i].get();
65
}
66
block = null;
67
/* -- end -- */
68
69
System.err.println("Forcing desperate garbage collection...");
70
java.util.Vector chain = new java.util.Vector();
71
try {
72
while (true) {
73
System.gc();
74
int[] hungry = new int[65536];
75
chain.addElement(hungry);
76
Thread.sleep(100); // yield, for what it's worth
77
}
78
} catch (OutOfMemoryError e) {
79
chain = null; // Free memory for further work.
80
System.err.println("Got OutOfMemoryError, as expected.");
81
}
82
83
int emptyCount = 0, fullCount = 0;
84
System.err.print("Examining contents of array:");
85
for (int i = 0; i < NUM_BLOCKS; ++ i) {
86
block = (byte[]) blocks[i].get();
87
if (block == null) {
88
emptyCount++;
89
} else {
90
fullCount++;
91
}
92
}
93
System.err.println(" " + emptyCount + " empty, " +
94
fullCount + " full.");
95
if (emptyCount == 0)
96
throw new Exception("No SoftReference instances were cleared");
97
}
98
99
}
100
101