author | lana |
Thu, 12 May 2016 15:15:02 +0000 | |
changeset 37915 | be4ff50b6cb6 |
parent 36851 | 03e2f4d0a421 |
child 40631 | ed82623d7831 |
permissions | -rw-r--r-- |
27606 | 1 |
/* |
29323
1e366d4e5472
8073950: Quarantine failing test: gc/TestSoftReferencesBehaviorOnOOME.java due to JDK-8073669
jwilhelm
parents:
27896
diff
changeset
|
2 |
* Copyright (c) 2014, 2015, 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. |
|
28 |
* @library /testlibrary |
|
36851 | 29 |
* @modules java.base/jdk.internal.misc |
29678
dd2f3932c21e
8075586: Add @modules as needed to the open hotspot tests
ykantser
parents:
29323
diff
changeset
|
30 |
* java.management |
27896
d4bd2f8b3055
8065865: gc/TestSoftReferencesBehaviorOnOOME.java: Error. Can't find source file: TestSoftReference.java
eistepan
parents:
27606
diff
changeset
|
31 |
* @build TestSoftReferencesBehaviorOnOOME |
27606 | 32 |
* @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 512 2k |
33 |
* @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 128k 256k |
|
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
34 |
* @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 2k 32k |
27606 | 35 |
*/ |
30604
b8d532cb6420
8067013: Rename the com.oracle.java.testlibary package
ykantser
parents:
29678
diff
changeset
|
36 |
import jdk.test.lib.Utils; |
30607 | 37 |
import jdk.test.lib.Asserts; |
27606 | 38 |
import java.lang.ref.SoftReference; |
39 |
import java.util.LinkedList; |
|
40 |
import java.util.Random; |
|
41 |
||
42 |
public class TestSoftReferencesBehaviorOnOOME { |
|
43 |
||
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
44 |
/** |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
45 |
* 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
|
46 |
* 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
|
47 |
* @param args - [minSize] [maxSize] [freq] |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
48 |
* where |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
49 |
* - minSize - min size of random objects |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
50 |
* - maxSize - max size of random objects |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
51 |
*/ |
27606 | 52 |
public static void main(String[] args) { |
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
53 |
long minSize = DEFAULT_MIN_SIZE; |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
54 |
long maxSize = DEFAULT_MAX_SIZE; |
27606 | 55 |
|
56 |
if ( args.length >= 2) { |
|
57 |
maxSize = getBytesCount(args[1]); |
|
58 |
} |
|
59 |
||
60 |
if ( args.length >= 1) { |
|
61 |
minSize = getBytesCount(args[0]); |
|
62 |
} |
|
63 |
||
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
64 |
new TestSoftReferencesBehaviorOnOOME().softReferencesOom(minSize, maxSize); |
27606 | 65 |
} |
66 |
||
67 |
/** |
|
68 |
* Test that all SoftReferences has been cleared at time of OOM. |
|
69 |
*/ |
|
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
70 |
void softReferencesOom(long minSize, long maxSize) { |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
71 |
System.out.format( "minSize = %d, maxSize = %d%n", minSize, maxSize ); |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
72 |
|
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
73 |
LinkedList<SoftReference> arrSoftRefs = new LinkedList(); |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
74 |
staticRef = arrSoftRefs; |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
75 |
LinkedList arrObjects = new LinkedList(); |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
76 |
staticRef = arrObjects; |
27606 | 77 |
|
78 |
long multiplier = maxSize - minSize; |
|
79 |
long numberOfNotNulledObjects = 0; |
|
80 |
||
81 |
try { |
|
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
82 |
|
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
83 |
// 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
|
84 |
// 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
|
85 |
long numSofts = (long) ((0.95 * Runtime.getRuntime().totalMemory()) / minSize); |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
86 |
System.out.println("num Soft: " + numSofts); |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
87 |
|
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
88 |
while (numSofts-- > 0) { |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
89 |
int allocationSize = ((int) (RND_GENERATOR.nextDouble() * multiplier)) |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
90 |
+ (int)minSize; |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
91 |
arrSoftRefs.add(new SoftReference(new byte[allocationSize])); |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
92 |
} |
27606 | 93 |
|
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
94 |
System.out.println("free: " + Runtime.getRuntime().freeMemory()); |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
95 |
|
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
96 |
// provoke OOME. |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
97 |
while (true) { |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
98 |
arrObjects.add(new byte[(int) Runtime.getRuntime().totalMemory()]); |
27606 | 99 |
} |
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
100 |
|
27606 | 101 |
} catch (OutOfMemoryError oome) { |
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
102 |
|
27606 | 103 |
// 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
|
104 |
staticRef = null; |
27606 | 105 |
arrObjects = null; |
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
106 |
long oomSoftArraySize = arrSoftRefs.size(); |
27606 | 107 |
|
108 |
for (SoftReference sr : arrSoftRefs) { |
|
109 |
Object o = sr.get(); |
|
110 |
||
111 |
if (o != null) { |
|
112 |
numberOfNotNulledObjects++; |
|
113 |
} |
|
114 |
} |
|
115 |
||
116 |
// Make sure we clear all refs before we return failure |
|
117 |
arrSoftRefs = null; |
|
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
118 |
Asserts.assertFalse(numberOfNotNulledObjects > 0, |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
119 |
"" + numberOfNotNulledObjects + " out of " |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
120 |
+ oomSoftArraySize + " SoftReferences was not " |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
121 |
+ "null at time of OutOfMemoryError" |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
122 |
); |
27606 | 123 |
} finally { |
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
124 |
Asserts.assertTrue(arrObjects == null, "OOME hasn't been provoked"); |
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
125 |
Asserts.assertTrue(arrSoftRefs == null, "OOME hasn't been provoked"); |
27606 | 126 |
} |
127 |
} |
|
128 |
||
129 |
private static final long getBytesCount(String arg) { |
|
130 |
String postfixes = "kMGT"; |
|
131 |
long mod = 1; |
|
132 |
||
133 |
if (arg.trim().length() >= 2) { |
|
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
134 |
mod = postfixes.indexOf(arg.trim().charAt(arg.length() - 1)); |
27606 | 135 |
|
136 |
if (mod != -1) { |
|
137 |
mod = (long) Math.pow(1024, mod+1); |
|
138 |
arg = arg.substring(0, arg.length() - 1); |
|
139 |
} else { |
|
140 |
mod = 1; // 10^0 |
|
141 |
} |
|
142 |
} |
|
143 |
||
144 |
return Long.parseLong(arg) * mod; |
|
145 |
} |
|
146 |
||
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
147 |
private static final Random RND_GENERATOR = Utils.getRandomInstance(); |
27606 | 148 |
private static final long DEFAULT_MIN_SIZE = 512; |
149 |
private static final long DEFAULT_MAX_SIZE = 1024; |
|
30557
8ba511484c3f
8073669: gc/TestSoftReferencesBehaviorOnOOME.java times out in nightlies
azakharov
parents:
29678
diff
changeset
|
150 |
private static Object staticRef; // to prevent compile optimisations |
27606 | 151 |
} |