1 /* 2 * Copyright (c) 2014, 2015, 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 /** 25 * @test TestSoftReferencesBehaviorOnOOME 26 * @key gc 27 * @summary Tests that all SoftReferences has been cleared at time of OOM. 28 * @library /testlibrary 29 * @build TestSoftReferencesBehaviorOnOOME 30 * @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 512 2k 31 * @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 128k 256k 32 * @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 2k 32k 33 */ 34 import com.oracle.java.testlibrary.Utils; 35 import com.oracle.java.testlibrary.Asserts; 36 import java.lang.ref.SoftReference; 37 import java.util.LinkedList; 38 import java.util.Random; 39 40 public class TestSoftReferencesBehaviorOnOOME { 41 42 /** 43 * Test generates a lot of soft references to objects with random payloads. 44 * Then it provokes OOME and checks that all SoftReferences has been gone 45 * @param args - [minSize] [maxSize] [freq] 46 * where 47 * - minSize - min size of random objects 48 * - maxSize - max size of random objects 49 */ 50 public static void main(String[] args) { 51 long minSize = DEFAULT_MIN_SIZE, 52 maxSize = DEFAULT_MAX_SIZE; 53 54 if ( args.length >= 2) { 55 maxSize = getBytesCount(args[1]); 56 } 57 58 if ( args.length >= 1) { 59 minSize = getBytesCount(args[0]); 60 } 61 62 new TestSoftReferencesBehaviorOnOOME().softReferencesOom(minSize, maxSize); 63 } 64 65 /** 66 * Test that all SoftReferences has been cleared at time of OOM. 67 */ 68 void softReferencesOom(long minSize, long maxSize) { 69 System.out.format( "minSize = %d, maxSize = %d%n", minSize, maxSize ); 70 71 LinkedList<SoftReference> arrSoftRefs = new LinkedList(); 72 staticRef = arrSoftRefs; 73 LinkedList arrObjects = new LinkedList(); 74 staticRef = arrObjects; 75 76 long multiplier = maxSize - minSize; 77 long numberOfNotNulledObjects = 0; 78 long oomSoftArraySize; 79 80 try { 81 82 // Lets allocate as many as we can - taking size of all SoftRerefences 83 // by minimum. So it can provoke some GC but we surely will allocate enough. 84 long numSofts = (long) ((0.95 * Runtime.getRuntime().totalMemory()) / minSize); 85 System.out.println("num Soft: " + numSofts); 86 87 while (numSofts-- > 0) { 88 int allocationSize = ((int) (RND_GENERATOR.nextDouble() * multiplier)) 89 + (int)minSize; 90 arrSoftRefs.add(new SoftReference(new byte[allocationSize])); 91 } 92 93 System.out.println("free: " + Runtime.getRuntime().freeMemory()); 94 95 // provoke OOME. 96 while (true) { 97 arrObjects.add(new byte[(int) Runtime.getRuntime().totalMemory()]); 98 } 99 100 } catch (OutOfMemoryError oome) { 101 102 // Clear allocated ballast, so we don't get another OOM. 103 staticRef = null; 104 arrObjects = null; 105 106 // Get the number of soft refs first, so we don't trigger 107 // another OOM. 108 oomSoftArraySize = arrSoftRefs.size(); 109 110 for (SoftReference sr : arrSoftRefs) { 111 Object o = sr.get(); 112 113 if (o != null) { 114 numberOfNotNulledObjects++; 115 } 116 } 117 118 // Make sure we clear all refs before we return failure 119 arrSoftRefs = null; 120 121 Asserts.assertFalse(numberOfNotNulledObjects > 0, 122 "" + numberOfNotNulledObjects + " out of " 123 + oomSoftArraySize + " SoftReferences was not " 124 + "null at time of OutOfMemoryError" 125 ); 126 } finally { 127 Asserts.assertTrue(arrObjects == null, "OOME hasn't been provoked"); 128 Asserts.assertTrue(arrSoftRefs == null, "OOME hasn't been provoked"); 129 } 130 } 131 132 private static final long getBytesCount(String arg) { 133 String postfixes = "kMGT"; 134 long mod = 1; 135 136 if (arg.trim().length() >= 2) { 137 mod = postfixes.indexOf(arg.trim().charAt(arg.length() - 1)); 138 139 if (mod != -1) { 140 mod = (long) Math.pow(1024, mod+1); 141 arg = arg.substring(0, arg.length() - 1); 142 } else { 143 mod = 1; // 10^0 144 } 145 } 146 147 return Long.parseLong(arg) * mod; 148 } 149 150 private static final Random RND_GENERATOR = Utils.getRandomInstance(); 151 private static final long DEFAULT_MIN_SIZE = 512; 152 private static final long DEFAULT_MAX_SIZE = 1024; 153 private static Object staticRef; // to prevent compile optimisations 154 }