Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/TestSoftReferencesBehaviorOnOOME.java
32281 views
/*1* Copyright (c) 2014, 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*/2223/**24* @test TestSoftReferencesBehaviorOnOOME25* @key gc26* @summary Tests that all SoftReferences has been cleared at time of OOM.27* @library /testlibrary28* @build TestSoftReferencesBehaviorOnOOME29* @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 512 2k30* @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 128k 256k31* @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 2k 32k 1032*/33import java.util.*;34import com.oracle.java.testlibrary.Utils;35import java.lang.ref.SoftReference;36import java.util.LinkedList;3738public class TestSoftReferencesBehaviorOnOOME {3940private static final Random rndGenerator = new Random();4142public static void main(String[] args) {43int semiRefAllocFrequency = DEFAULT_FREQUENCY;44long minSize = DEFAULT_MIN_SIZE,45maxSize = DEFAULT_MAX_SIZE;4647if ( args.length >= 3 ) {48semiRefAllocFrequency = Integer.parseInt(args[2]);49}5051if ( args.length >= 2) {52maxSize = getBytesCount(args[1]);53}5455if ( args.length >= 1) {56minSize = getBytesCount(args[0]);57}5859new TestSoftReferencesBehaviorOnOOME().softReferencesOom(minSize, maxSize, semiRefAllocFrequency);60}6162/**63* Test that all SoftReferences has been cleared at time of OOM.64*/65void softReferencesOom(long minSize, long maxSize, int semiRefAllocFrequency) {66System.out.format( "minSize = %d, maxSize = %d, freq = %d%n", minSize, maxSize, semiRefAllocFrequency );67long counter = 0;6869long multiplier = maxSize - minSize;70LinkedList<SoftReference> arrSoftRefs = new LinkedList();71LinkedList arrObjects = new LinkedList();72long numberOfNotNulledObjects = 0;73long oomSoftArraySize = 0;7475try {76while (true) {77// Keep every Xth object to make sure we hit OOM pretty fast78if (counter % semiRefAllocFrequency != 0) {79long allocationSize = ((int) (rndGenerator.nextDouble() * multiplier))80+ minSize;81arrObjects.add(new byte[(int)allocationSize]);82} else {83arrSoftRefs.add(new SoftReference(new Object()));84}8586counter++;87if (counter == Long.MAX_VALUE) {88counter = 0;89}90}91} catch (OutOfMemoryError oome) {92// Clear allocated ballast, so we don't get another OOM.9394arrObjects = null;9596// Get the number of soft refs first, so we don't trigger97// another OOM.98oomSoftArraySize = arrSoftRefs.size();99100for (SoftReference sr : arrSoftRefs) {101Object o = sr.get();102103if (o != null) {104numberOfNotNulledObjects++;105}106}107108// Make sure we clear all refs before we return failure109arrSoftRefs = null;110111if (numberOfNotNulledObjects > 0) {112throw new RuntimeException(numberOfNotNulledObjects + " out of "113+ oomSoftArraySize + " SoftReferences was not "114+ "null at time of OutOfMemoryError");115}116} finally {117arrSoftRefs = null;118arrObjects = null;119}120}121122private static final long getBytesCount(String arg) {123String postfixes = "kMGT";124long mod = 1;125126if (arg.trim().length() >= 2) {127mod = postfixes.indexOf(128arg.trim().charAt(arg.length() - 1)129);130131if (mod != -1) {132mod = (long) Math.pow(1024, mod+1);133arg = arg.substring(0, arg.length() - 1);134} else {135mod = 1; // 10^0136}137}138139return Long.parseLong(arg) * mod;140}141142private static final long DEFAULT_MIN_SIZE = 512;143private static final long DEFAULT_MAX_SIZE = 1024;144private static final int DEFAULT_FREQUENCY = 4;145}146147148