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  * @ignore 8073669
  30  * @build TestSoftReferencesBehaviorOnOOME
  31  * @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 512 2k
  32  * @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 128k 256k
  33  * @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 2k 32k 10
  34  */
  35 import com.oracle.java.testlibrary.Utils;
  36 import java.lang.ref.SoftReference;
  37 import java.util.LinkedList;
  38 import java.util.Random;
  39 
  40 public class TestSoftReferencesBehaviorOnOOME {
  41 
  42     private static final Random rndGenerator = Utils.getRandomInstance();
  43 
  44     public static void main(String[] args) {
  45         int semiRefAllocFrequency = DEFAULT_FREQUENCY;
  46         long minSize = DEFAULT_MIN_SIZE,
  47                 maxSize = DEFAULT_MAX_SIZE;
  48 
  49         if ( args.length >= 3 ) {
  50             semiRefAllocFrequency = Integer.parseInt(args[2]);
  51         }
  52 
  53         if ( args.length >= 2) {
  54             maxSize = getBytesCount(args[1]);
  55         }
  56 
  57         if ( args.length >= 1) {
  58             minSize = getBytesCount(args[0]);
  59         }
  60 
  61         new TestSoftReferencesBehaviorOnOOME().softReferencesOom(minSize, maxSize, semiRefAllocFrequency);
  62     }
  63 
  64     /**
  65      * Test that all SoftReferences has been cleared at time of OOM.
  66      */
  67     void softReferencesOom(long minSize, long maxSize, int semiRefAllocFrequency) {
  68         System.out.format( "minSize = %d, maxSize = %d, freq = %d%n", minSize, maxSize, semiRefAllocFrequency );
  69         long counter = 0;
  70 
  71         long multiplier = maxSize - minSize;
  72         LinkedList<SoftReference> arrSoftRefs = new LinkedList();
  73         LinkedList arrObjects = new LinkedList();
  74         long numberOfNotNulledObjects = 0;
  75         long oomSoftArraySize = 0;
  76 
  77         try {
  78             while (true) {
  79                 // Keep every Xth object to make sure we hit OOM pretty fast
  80                 if (counter % semiRefAllocFrequency != 0) {
  81                     long allocationSize = ((int) (rndGenerator.nextDouble() * multiplier))
  82                             + minSize;
  83                     arrObjects.add(new byte[(int)allocationSize]);
  84                 } else {
  85                     arrSoftRefs.add(new SoftReference(new Object()));
  86                 }
  87 
  88                 counter++;
  89                 if (counter == Long.MAX_VALUE) {
  90                     counter = 0;
  91                 }
  92             }
  93         } catch (OutOfMemoryError oome) {
  94             // Clear allocated ballast, so we don't get another OOM.
  95 
  96             arrObjects = null;
  97 
  98             // Get the number of soft refs first, so we don't trigger
  99             // another OOM.
 100             oomSoftArraySize = arrSoftRefs.size();
 101 
 102             for (SoftReference sr : arrSoftRefs) {
 103                 Object o = sr.get();
 104 
 105                 if (o != null) {
 106                     numberOfNotNulledObjects++;
 107                 }
 108             }
 109 
 110             // Make sure we clear all refs before we return failure
 111             arrSoftRefs = null;
 112 
 113             if (numberOfNotNulledObjects > 0) {
 114                 throw new RuntimeException(numberOfNotNulledObjects + " out of "
 115                         + oomSoftArraySize + " SoftReferences was not "
 116                         + "null at time of OutOfMemoryError");
 117             }
 118         } finally {
 119             arrSoftRefs = null;
 120             arrObjects = null;
 121         }
 122     }
 123 
 124     private static final long getBytesCount(String arg) {
 125         String postfixes = "kMGT";
 126         long mod = 1;
 127 
 128         if (arg.trim().length() >= 2) {
 129             mod = postfixes.indexOf(
 130                     arg.trim().charAt(arg.length() - 1)
 131             );
 132 
 133             if (mod != -1) {
 134                 mod = (long) Math.pow(1024, mod+1);
 135                 arg = arg.substring(0, arg.length() - 1);
 136             } else {
 137                 mod = 1; // 10^0
 138             }
 139         }
 140 
 141         return Long.parseLong(arg) * mod;
 142     }
 143 
 144     private static final long DEFAULT_MIN_SIZE = 512;
 145     private static final long DEFAULT_MAX_SIZE = 1024;
 146     private static final int DEFAULT_FREQUENCY = 4;
 147 }