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