author | jiangli |
Fri, 02 Nov 2018 19:30:31 -0400 | |
changeset 52397 | 1322829d1501 |
parent 50525 | 767cdb97f103 |
child 53523 | 4c5184c56dc2 |
permissions | -rw-r--r-- |
27606 | 1 |
/* |
40631
ed82623d7831
8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
36851
diff
changeset
|
2 |
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. |
27606 | 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 TestSoftReferencesBehaviorOnOOME |
|
26 |
* @key gc |
|
27 |
* @summary Tests that all SoftReferences has been cleared at time of OOM. |
|
50525
767cdb97f103
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
47216
diff
changeset
|
28 |
* @requires vm.gc != "Z" |
40631
ed82623d7831
8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
36851
diff
changeset
|
29 |
* @library /test/lib |
36851 | 30 |
* @modules java.base/jdk.internal.misc |
27606 | 31 |
* @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 512 2k |
32 |
* @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 128k 256k |
|
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
33 |
* @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 2k 32k |
27606 | 34 |
*/ |
30604
b8d532cb6420
8067013: Rename the com.oracle.java.testlibary package
ykantser
parents:
29678
diff
changeset
|
35 |
import jdk.test.lib.Utils; |
30607 | 36 |
import jdk.test.lib.Asserts; |
27606 | 37 |
import java.lang.ref.SoftReference; |
38 |
import java.util.LinkedList; |
|
39 |
import java.util.Random; |
|
40 |
||
41 |
public class TestSoftReferencesBehaviorOnOOME { |
|
42 |
||
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
43 |
/** |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
44 |
* Test generates a lot of soft references to objects with random payloads. |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
45 |
* Then it provokes OOME and checks that all SoftReferences has been gone |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
46 |
* @param args - [minSize] [maxSize] [freq] |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
47 |
* where |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
48 |
* - minSize - min size of random objects |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
49 |
* - maxSize - max size of random objects |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
50 |
*/ |
27606 | 51 |
public static void main(String[] args) { |
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
52 |
long minSize = DEFAULT_MIN_SIZE; |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
53 |
long maxSize = DEFAULT_MAX_SIZE; |
27606 | 54 |
|
55 |
if ( args.length >= 2) { |
|
56 |
maxSize = getBytesCount(args[1]); |
|
57 |
} |
|
58 |
||
59 |
if ( args.length >= 1) { |
|
60 |
minSize = getBytesCount(args[0]); |
|
61 |
} |
|
62 |
||
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
63 |
new TestSoftReferencesBehaviorOnOOME().softReferencesOom(minSize, maxSize); |
27606 | 64 |
} |
65 |
||
66 |
/** |
|
67 |
* Test that all SoftReferences has been cleared at time of OOM. |
|
68 |
*/ |
|
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
69 |
void softReferencesOom(long minSize, long maxSize) { |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
70 |
System.out.format( "minSize = %d, maxSize = %d%n", minSize, maxSize ); |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
71 |
|
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
72 |
LinkedList<SoftReference> arrSoftRefs = new LinkedList(); |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
73 |
staticRef = arrSoftRefs; |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
74 |
LinkedList arrObjects = new LinkedList(); |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
75 |
staticRef = arrObjects; |
27606 | 76 |
|
77 |
long multiplier = maxSize - minSize; |
|
78 |
long numberOfNotNulledObjects = 0; |
|
79 |
||
80 |
try { |
|
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
81 |
|
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
82 |
// Lets allocate as many as we can - taking size of all SoftRerefences |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
83 |
// by minimum. So it can provoke some GC but we surely will allocate enough. |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
84 |
long numSofts = (long) ((0.95 * Runtime.getRuntime().totalMemory()) / minSize); |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
85 |
System.out.println("num Soft: " + numSofts); |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
86 |
|
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
87 |
while (numSofts-- > 0) { |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
88 |
int allocationSize = ((int) (RND_GENERATOR.nextDouble() * multiplier)) |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
89 |
+ (int)minSize; |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
90 |
arrSoftRefs.add(new SoftReference(new byte[allocationSize])); |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
91 |
} |
27606 | 92 |
|
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
93 |
System.out.println("free: " + Runtime.getRuntime().freeMemory()); |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
94 |
|
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
95 |
// provoke OOME. |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
96 |
while (true) { |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
97 |
arrObjects.add(new byte[(int) Runtime.getRuntime().totalMemory()]); |
27606 | 98 |
} |
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
99 |
|
27606 | 100 |
} catch (OutOfMemoryError oome) { |
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
101 |
|
27606 | 102 |
// Clear allocated ballast, so we don't get another OOM. |
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
103 |
staticRef = null; |
27606 | 104 |
arrObjects = null; |
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
105 |
long oomSoftArraySize = arrSoftRefs.size(); |
27606 | 106 |
|
107 |
for (SoftReference sr : arrSoftRefs) { |
|
108 |
Object o = sr.get(); |
|
109 |
||
110 |
if (o != null) { |
|
111 |
numberOfNotNulledObjects++; |
|
112 |
} |
|
113 |
} |
|
114 |
||
115 |
// Make sure we clear all refs before we return failure |
|
116 |
arrSoftRefs = null; |
|
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
117 |
Asserts.assertFalse(numberOfNotNulledObjects > 0, |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
118 |
"" + numberOfNotNulledObjects + " out of " |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
119 |
+ oomSoftArraySize + " SoftReferences was not " |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
120 |
+ "null at time of OutOfMemoryError" |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
121 |
); |
27606 | 122 |
} finally { |
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
123 |
Asserts.assertTrue(arrObjects == null, "OOME hasn't been provoked"); |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
124 |
Asserts.assertTrue(arrSoftRefs == null, "OOME hasn't been provoked"); |
27606 | 125 |
} |
126 |
} |
|
127 |
||
128 |
private static final long getBytesCount(String arg) { |
|
129 |
String postfixes = "kMGT"; |
|
130 |
long mod = 1; |
|
131 |
||
132 |
if (arg.trim().length() >= 2) { |
|
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
133 |
mod = postfixes.indexOf(arg.trim().charAt(arg.length() - 1)); |
27606 | 134 |
|
135 |
if (mod != -1) { |
|
136 |
mod = (long) Math.pow(1024, mod+1); |
|
137 |
arg = arg.substring(0, arg.length() - 1); |
|
138 |
} else { |
|
139 |
mod = 1; // 10^0 |
|
140 |
} |
|
141 |
} |
|
142 |
||
143 |
return Long.parseLong(arg) * mod; |
|
144 |
} |
|
145 |
||
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
146 |
private static final Random RND_GENERATOR = Utils.getRandomInstance(); |
27606 | 147 |
private static final long DEFAULT_MIN_SIZE = 512; |
148 |
private static final long DEFAULT_MAX_SIZE = 1024; |
|
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
149 |
private static Object staticRef; // to prevent compile optimisations |
27606 | 150 |
} |