test/gc/TestSoftReferencesBehaviorOnOOME.java

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 10026
8c95980d0b66
parent 8625
f7b4a17a9d49
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset d3b4d62f391f

kshefov@8301 1 /*
shshahma@8625 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
kshefov@8301 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
kshefov@8301 4 *
kshefov@8301 5 * This code is free software; you can redistribute it and/or modify it
kshefov@8301 6 * under the terms of the GNU General Public License version 2 only, as
kshefov@8301 7 * published by the Free Software Foundation.
kshefov@8301 8 *
kshefov@8301 9 * This code is distributed in the hope that it will be useful, but WITHOUT
kshefov@8301 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
kshefov@8301 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kshefov@8301 12 * version 2 for more details (a copy is included in the LICENSE file that
kshefov@8301 13 * accompanied this code).
kshefov@8301 14 *
kshefov@8301 15 * You should have received a copy of the GNU General Public License version
kshefov@8301 16 * 2 along with this work; if not, write to the Free Software Foundation,
kshefov@8301 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
kshefov@8301 18 *
kshefov@8301 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
kshefov@8301 20 * or visit www.oracle.com if you need additional information or have any
kshefov@8301 21 * questions.
kshefov@8301 22 */
kshefov@8301 23
kshefov@8301 24 /**
kshefov@8301 25 * @test TestSoftReferencesBehaviorOnOOME
kshefov@8301 26 * @key gc
kshefov@8301 27 * @summary Tests that all SoftReferences has been cleared at time of OOM.
kshefov@8301 28 * @library /testlibrary
kshefov@8301 29 * @build TestSoftReferencesBehaviorOnOOME
kshefov@8301 30 * @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 512 2k
kshefov@8301 31 * @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 128k 256k
kshefov@8301 32 * @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 2k 32k 10
kshefov@8301 33 */
kshefov@8301 34 import java.util.*;
kshefov@8301 35 import com.oracle.java.testlibrary.Utils;
kshefov@8301 36 import java.lang.ref.SoftReference;
kshefov@8301 37 import java.util.LinkedList;
kshefov@8301 38
kshefov@8301 39 public class TestSoftReferencesBehaviorOnOOME {
kshefov@8301 40
kshefov@8301 41 private static final Random rndGenerator = new Random();
kshefov@8301 42
kshefov@8301 43 public static void main(String[] args) {
kshefov@8301 44 int semiRefAllocFrequency = DEFAULT_FREQUENCY;
kshefov@8301 45 long minSize = DEFAULT_MIN_SIZE,
kshefov@8301 46 maxSize = DEFAULT_MAX_SIZE;
kshefov@8301 47
kshefov@8301 48 if ( args.length >= 3 ) {
kshefov@8301 49 semiRefAllocFrequency = Integer.parseInt(args[2]);
kshefov@8301 50 }
kshefov@8301 51
kshefov@8301 52 if ( args.length >= 2) {
kshefov@8301 53 maxSize = getBytesCount(args[1]);
kshefov@8301 54 }
kshefov@8301 55
kshefov@8301 56 if ( args.length >= 1) {
kshefov@8301 57 minSize = getBytesCount(args[0]);
kshefov@8301 58 }
kshefov@8301 59
kshefov@8301 60 new TestSoftReferencesBehaviorOnOOME().softReferencesOom(minSize, maxSize, semiRefAllocFrequency);
kshefov@8301 61 }
kshefov@8301 62
kshefov@8301 63 /**
kshefov@8301 64 * Test that all SoftReferences has been cleared at time of OOM.
kshefov@8301 65 */
kshefov@8301 66 void softReferencesOom(long minSize, long maxSize, int semiRefAllocFrequency) {
kshefov@8301 67 System.out.format( "minSize = %d, maxSize = %d, freq = %d%n", minSize, maxSize, semiRefAllocFrequency );
kshefov@8301 68 long counter = 0;
kshefov@8301 69
kshefov@8301 70 long multiplier = maxSize - minSize;
kshefov@8301 71 LinkedList<SoftReference> arrSoftRefs = new LinkedList();
kshefov@8301 72 LinkedList arrObjects = new LinkedList();
kshefov@8301 73 long numberOfNotNulledObjects = 0;
kshefov@8301 74 long oomSoftArraySize = 0;
kshefov@8301 75
kshefov@8301 76 try {
kshefov@8301 77 while (true) {
kshefov@8301 78 // Keep every Xth object to make sure we hit OOM pretty fast
kshefov@8301 79 if (counter % semiRefAllocFrequency != 0) {
kshefov@8301 80 long allocationSize = ((int) (rndGenerator.nextDouble() * multiplier))
kshefov@8301 81 + minSize;
kshefov@8301 82 arrObjects.add(new byte[(int)allocationSize]);
kshefov@8301 83 } else {
kshefov@8301 84 arrSoftRefs.add(new SoftReference(new Object()));
kshefov@8301 85 }
kshefov@8301 86
kshefov@8301 87 counter++;
kshefov@8301 88 if (counter == Long.MAX_VALUE) {
kshefov@8301 89 counter = 0;
kshefov@8301 90 }
kshefov@8301 91 }
kshefov@8301 92 } catch (OutOfMemoryError oome) {
kshefov@8301 93 // Clear allocated ballast, so we don't get another OOM.
kshefov@8301 94
kshefov@8301 95 arrObjects = null;
kshefov@8301 96
kshefov@8301 97 // Get the number of soft refs first, so we don't trigger
kshefov@8301 98 // another OOM.
kshefov@8301 99 oomSoftArraySize = arrSoftRefs.size();
kshefov@8301 100
kshefov@8301 101 for (SoftReference sr : arrSoftRefs) {
kshefov@8301 102 Object o = sr.get();
kshefov@8301 103
kshefov@8301 104 if (o != null) {
kshefov@8301 105 numberOfNotNulledObjects++;
kshefov@8301 106 }
kshefov@8301 107 }
kshefov@8301 108
kshefov@8301 109 // Make sure we clear all refs before we return failure
kshefov@8301 110 arrSoftRefs = null;
kshefov@8301 111
kshefov@8301 112 if (numberOfNotNulledObjects > 0) {
kshefov@8301 113 throw new RuntimeException(numberOfNotNulledObjects + " out of "
kshefov@8301 114 + oomSoftArraySize + " SoftReferences was not "
kshefov@8301 115 + "null at time of OutOfMemoryError");
kshefov@8301 116 }
kshefov@8301 117 } finally {
kshefov@8301 118 arrSoftRefs = null;
kshefov@8301 119 arrObjects = null;
kshefov@8301 120 }
kshefov@8301 121 }
kshefov@8301 122
kshefov@8301 123 private static final long getBytesCount(String arg) {
kshefov@8301 124 String postfixes = "kMGT";
kshefov@8301 125 long mod = 1;
kshefov@8301 126
kshefov@8301 127 if (arg.trim().length() >= 2) {
kshefov@8301 128 mod = postfixes.indexOf(
kshefov@8301 129 arg.trim().charAt(arg.length() - 1)
kshefov@8301 130 );
kshefov@8301 131
kshefov@8301 132 if (mod != -1) {
kshefov@8301 133 mod = (long) Math.pow(1024, mod+1);
kshefov@8301 134 arg = arg.substring(0, arg.length() - 1);
kshefov@8301 135 } else {
kshefov@8301 136 mod = 1; // 10^0
kshefov@8301 137 }
kshefov@8301 138 }
kshefov@8301 139
kshefov@8301 140 return Long.parseLong(arg) * mod;
kshefov@8301 141 }
kshefov@8301 142
kshefov@8301 143 private static final long DEFAULT_MIN_SIZE = 512;
kshefov@8301 144 private static final long DEFAULT_MAX_SIZE = 1024;
kshefov@8301 145 private static final int DEFAULT_FREQUENCY = 4;
kshefov@8301 146 }

mercurial