Path: blob/master/test/hotspot/jtreg/gc/TestSoftReferencesBehaviorOnOOME.java
40930 views
/*1* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package gc;2425/**26* @test TestSoftReferencesBehaviorOnOOME27* @key randomness28* @summary Tests that all SoftReferences has been cleared at time of OOM.29* @requires vm.gc != "Z"30* @requires vm.gc != "Shenandoah"31* @library /test/lib32* @modules java.base/jdk.internal.misc33* @run main/othervm -Xmx128m gc.TestSoftReferencesBehaviorOnOOME 512 2k34* @run main/othervm -Xmx128m gc.TestSoftReferencesBehaviorOnOOME 128k 256k35* @run main/othervm -Xmx128m gc.TestSoftReferencesBehaviorOnOOME 2k 32k36*/37import jdk.test.lib.Utils;38import jdk.test.lib.Asserts;39import java.lang.ref.SoftReference;40import java.util.LinkedList;41import java.util.Random;4243public class TestSoftReferencesBehaviorOnOOME {4445/**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 gone48* @param args - [minSize] [maxSize] [freq]49* where50* - minSize - min size of random objects51* - maxSize - max size of random objects52*/53public static void main(String[] args) {54long minSize = DEFAULT_MIN_SIZE;55long maxSize = DEFAULT_MAX_SIZE;5657if ( args.length >= 2) {58maxSize = getBytesCount(args[1]);59}6061if ( args.length >= 1) {62minSize = getBytesCount(args[0]);63}6465new TestSoftReferencesBehaviorOnOOME().softReferencesOom(minSize, maxSize);66}6768/**69* Test that all SoftReferences has been cleared at time of OOM.70*/71void softReferencesOom(long minSize, long maxSize) {72System.out.format( "minSize = %d, maxSize = %d%n", minSize, maxSize );7374LinkedList<SoftReference<byte[]>> arrSoftRefs = new LinkedList<>();75staticRef = arrSoftRefs;76LinkedList<byte[]> arrObjects = new LinkedList<>();77staticRef = arrObjects;7879long multiplier = maxSize - minSize;80long numberOfNotNulledObjects = 0;8182try {8384// Lets allocate as many as we can - taking size of all SoftRerefences85// by minimum. So it can provoke some GC but we surely will allocate enough.86long numSofts = (long) ((0.95 * Runtime.getRuntime().totalMemory()) / minSize);87System.out.println("num Soft: " + numSofts);8889while (numSofts-- > 0) {90int allocationSize = ((int) (RND_GENERATOR.nextDouble() * multiplier))91+ (int)minSize;92arrSoftRefs.add(new SoftReference<byte[]>(new byte[allocationSize]));93}9495System.out.println("free: " + Runtime.getRuntime().freeMemory());9697// provoke OOME.98while (true) {99arrObjects.add(new byte[(int) Runtime.getRuntime().totalMemory()]);100}101102} catch (OutOfMemoryError oome) {103104// Clear allocated ballast, so we don't get another OOM.105staticRef = null;106arrObjects = null;107long oomSoftArraySize = arrSoftRefs.size();108109for (SoftReference<byte[]> sr : arrSoftRefs) {110Object o = sr.get();111112if (o != null) {113numberOfNotNulledObjects++;114}115}116117// Make sure we clear all refs before we return failure118arrSoftRefs = null;119Asserts.assertFalse(numberOfNotNulledObjects > 0,120"" + numberOfNotNulledObjects + " out of "121+ oomSoftArraySize + " SoftReferences was not "122+ "null at time of OutOfMemoryError"123);124} finally {125Asserts.assertTrue(arrObjects == null, "OOME hasn't been provoked");126Asserts.assertTrue(arrSoftRefs == null, "OOME hasn't been provoked");127}128}129130private static final long getBytesCount(String arg) {131String postfixes = "kMGT";132long mod = 1;133134if (arg.trim().length() >= 2) {135mod = postfixes.indexOf(arg.trim().charAt(arg.length() - 1));136137if (mod != -1) {138mod = (long) Math.pow(1024, mod+1);139arg = arg.substring(0, arg.length() - 1);140} else {141mod = 1; // 10^0142}143}144145return Long.parseLong(arg) * mod;146}147148private static final Random RND_GENERATOR = Utils.getRandomInstance();149private static final long DEFAULT_MIN_SIZE = 512;150private static final long DEFAULT_MAX_SIZE = 1024;151private static Object staticRef; // to prevent compile optimisations152}153154155