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