1 /*
   2  * Copyright (c) 2014, 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  * @test TestSoftReference
  25  * @key gc
  26  * @summary Tests that all SoftReferences has been cleared at time of OOM.
  27  * @library /testlibrary
  28  * @build TestSoftReference
  29  * @run main/othervm -Xmx200m TestSoftReference 512 2k
  30  * @run main/othervm -Xmx200m TestSoftReference 128k 256k
  31  * @run main/othervm -Xmx200m TestSoftReference 2k 32k 10
  32  */
  33 import com.oracle.java.testlibrary.Utils;
  34 import java.lang.ref.SoftReference;
  35 import java.util.LinkedList;
  36 import java.util.Random;
  37 
  38 public class TestSoftReference {
  39 
  40     private static final Random rndGenerator = Utils.getRandomInstance();
  41 
  42     public static void main(String[] args) {
  43         int semiRefAllocFrequency = DEFAULT_FREQUENCY;
  44         long minSize = DEFAULT_MIN_SIZE,
  45                 maxSize = DEFAULT_MAX_SIZE;
  46 
  47         if ( args.length >= 3 ) {
  48             semiRefAllocFrequency = Integer.parseInt(args[2]);
  49         }
  50 
  51         if ( args.length >= 2) {
  52             maxSize = getBytesCount(args[1]);
  53         }
  54 
  55         if ( args.length >= 1) {
  56             minSize = getBytesCount(args[0]);
  57         }
  58 
  59         new TestSoftReference().softReferencesOom(minSize, maxSize, semiRefAllocFrequency);
  60     }
  61 
  62     /**
  63      * Test that all SoftReferences has been cleared at time of OOM.
  64      */
  65     void softReferencesOom(long minSize, long maxSize, int semiRefAllocFrequency) {
  66         System.out.format( "minSize = %d, maxSize = %d, freq = %d%n", minSize, maxSize, semiRefAllocFrequency );
  67         long counter = 0;
  68 
  69         long multiplier = maxSize - minSize;
  70         LinkedList<SoftReference> arrSoftRefs = new LinkedList();
  71         LinkedList arrObjects = new LinkedList();
  72         long numberOfNotNulledObjects = 0;
  73         long oomSoftArraySize = 0;
  74 
  75         try {
  76             while (true) {
  77                 // Keep every Xth object to make sure we hit OOM pretty fast
  78                 if (counter % semiRefAllocFrequency != 0) {
  79                     long allocationSize = ((int) (rndGenerator.nextDouble() * multiplier))
  80                             + minSize;
  81                     arrObjects.add(new byte[(int)allocationSize]);
  82                 } else {
  83                     arrSoftRefs.add(new SoftReference(new Object()));
  84                 }
  85 
  86                 counter++;
  87                 if (counter == Long.MAX_VALUE) {
  88                     counter = 0;
  89                 }
  90             }
  91         } catch (OutOfMemoryError oome) {
  92             // Clear allocated ballast, so we don't get another OOM.
  93 
  94             arrObjects = null;
  95 
  96             // Get the number of soft refs first, so we don't trigger
  97             // another OOM.
  98             oomSoftArraySize = arrSoftRefs.size();
  99 
 100             for (SoftReference sr : arrSoftRefs) {
 101                 Object o = sr.get();
 102 
 103                 if (o != null) {
 104                     numberOfNotNulledObjects++;
 105                 }
 106             }
 107 
 108             // Make sure we clear all refs before we return failure, since
 109             // coconut require some memory to complete, and since we're in
 110             // an OOM situation, that could cause trouble.
 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 }