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 long 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 79 try { 80 81 // Lets allocate as many as we can - taking size of all SoftRerefences 82 // by minimum. So it can provoke some GC but we surely will allocate enough. 83 long numSofts = (long) ((0.95 * Runtime.getRuntime().totalMemory()) / minSize); 84 System.out.println("num Soft: " + numSofts); 85 86 while (numSofts-- > 0) { 87 int allocationSize = ((int) (RND_GENERATOR.nextDouble() * multiplier)) 88 + (int)minSize; 89 arrSoftRefs.add(new SoftReference(new byte[allocationSize])); 90 } 91 92 System.out.println("free: " + Runtime.getRuntime().freeMemory()); 93 94 // provoke OOME. 95 while (true) { 96 arrObjects.add(new byte[(int) Runtime.getRuntime().totalMemory()]); 97 } 98 99 } catch (OutOfMemoryError oome) { 100 101 // Clear allocated ballast, so we don't get another OOM. 102 staticRef = null; 103 arrObjects = null; 104 long oomSoftArraySize = arrSoftRefs.size(); 105 106 for (SoftReference sr : arrSoftRefs) { 107 Object o = sr.get(); 108 109 if (o != null) { 110 numberOfNotNulledObjects++; 111 } 112 } 113 114 // Make sure we clear all refs before we return failure 115 arrSoftRefs = null; 116 Asserts.assertFalse(numberOfNotNulledObjects > 0, 117 "" + numberOfNotNulledObjects + " out of " 118 + oomSoftArraySize + " SoftReferences was not " 119 + "null at time of OutOfMemoryError" 120 ); 121 } finally { 122 Asserts.assertTrue(arrObjects == null, "OOME hasn't been provoked"); 123 Asserts.assertTrue(arrSoftRefs == null, "OOME hasn't been provoked"); 124 } 125 } 126 127 private static final long getBytesCount(String arg) { 128 String postfixes = "kMGT"; 129 long mod = 1; 130 131 if (arg.trim().length() >= 2) { 132 mod = postfixes.indexOf(arg.trim().charAt(arg.length() - 1)); 133 134 if (mod != -1) { 135 mod = (long) Math.pow(1024, mod+1); 136 arg = arg.substring(0, arg.length() - 1); 137 } else { 138 mod = 1; // 10^0 139 } 140 } 141 142 return Long.parseLong(arg) * mod; 143 } 144 145 private static final Random RND_GENERATOR = Utils.getRandomInstance(); 146 private static final long DEFAULT_MIN_SIZE = 512; 147 private static final long DEFAULT_MAX_SIZE = 1024; 148 private static Object staticRef; // to prevent compile optimisations 149 }