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/jdk.internal.misc
  30  *          java.management
  31  * @build TestSoftReferencesBehaviorOnOOME
  32  * @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 512 2k
  33  * @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 128k 256k
  34  * @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 2k 32k
  35  */
  36 import jdk.test.lib.Utils;
  37 import jdk.test.lib.Asserts;
  38 import java.lang.ref.SoftReference;
  39 import java.util.LinkedList;
  40 import java.util.Random;
  41 
  42 public class TestSoftReferencesBehaviorOnOOME {
  43 
  44     /**
  45      * Test generates a lot of soft references to objects with random payloads.
  46      * Then it provokes OOME and checks that all SoftReferences has been gone
  47      * @param args - [minSize] [maxSize] [freq]
  48      *  where
  49      *  - minSize - min size of random objects
  50      *  - maxSize - max size of random objects
  51      */
  52     public static void main(String[] args) {
  53         long minSize = DEFAULT_MIN_SIZE;
  54         long maxSize = DEFAULT_MAX_SIZE;
  55 
  56         if ( args.length >= 2) {
  57             maxSize = getBytesCount(args[1]);
  58         }
  59 
  60         if ( args.length >= 1) {
  61             minSize = getBytesCount(args[0]);
  62         }
  63 
  64         new TestSoftReferencesBehaviorOnOOME().softReferencesOom(minSize, maxSize);
  65     }
  66 
  67     /**
  68      * Test that all SoftReferences has been cleared at time of OOM.
  69      */
  70     void softReferencesOom(long minSize, long maxSize) {
  71         System.out.format( "minSize = %d, maxSize = %d%n", minSize, maxSize );
  72 
  73         LinkedList<SoftReference> arrSoftRefs = new LinkedList();
  74         staticRef = arrSoftRefs;
  75         LinkedList arrObjects = new LinkedList();
  76         staticRef = arrObjects;
  77 
  78         long multiplier = maxSize - minSize;
  79         long numberOfNotNulledObjects = 0;
  80 
  81         try {
  82 
  83             // Lets allocate as many as we can - taking size of all SoftRerefences
  84             // by minimum. So it can provoke some GC but we surely will allocate enough.
  85             long numSofts = (long) ((0.95 * Runtime.getRuntime().totalMemory()) / minSize);
  86             System.out.println("num Soft: " + numSofts);
  87 
  88             while (numSofts-- > 0) {
  89                 int allocationSize = ((int) (RND_GENERATOR.nextDouble() * multiplier))
  90                             + (int)minSize;
  91                 arrSoftRefs.add(new SoftReference(new byte[allocationSize]));
  92             }
  93 
  94             System.out.println("free: " + Runtime.getRuntime().freeMemory());
  95 
  96             // provoke OOME.
  97             while (true) {
  98                 arrObjects.add(new byte[(int) Runtime.getRuntime().totalMemory()]);
  99             }
 100 
 101         } catch (OutOfMemoryError oome) {
 102 
 103             // Clear allocated ballast, so we don't get another OOM.
 104             staticRef = null;
 105             arrObjects = null;
 106             long oomSoftArraySize = arrSoftRefs.size();
 107 
 108             for (SoftReference sr : arrSoftRefs) {
 109                 Object o = sr.get();
 110 
 111                 if (o != null) {
 112                     numberOfNotNulledObjects++;
 113                 }
 114             }
 115 
 116             // Make sure we clear all refs before we return failure
 117             arrSoftRefs = null;
 118             Asserts.assertFalse(numberOfNotNulledObjects > 0,
 119                     "" + numberOfNotNulledObjects + " out of "
 120                     + oomSoftArraySize + " SoftReferences was not "
 121                     + "null at time of OutOfMemoryError"
 122             );
 123         } finally {
 124             Asserts.assertTrue(arrObjects == null, "OOME hasn't been provoked");
 125             Asserts.assertTrue(arrSoftRefs == null, "OOME hasn't been provoked");
 126         }
 127     }
 128 
 129     private static final long getBytesCount(String arg) {
 130         String postfixes = "kMGT";
 131         long mod = 1;
 132 
 133         if (arg.trim().length() >= 2) {
 134             mod = postfixes.indexOf(arg.trim().charAt(arg.length() - 1));
 135 
 136             if (mod != -1) {
 137                 mod = (long) Math.pow(1024, mod+1);
 138                 arg = arg.substring(0, arg.length() - 1);
 139             } else {
 140                 mod = 1; // 10^0
 141             }
 142         }
 143 
 144         return Long.parseLong(arg) * mod;
 145     }
 146 
 147     private static final Random RND_GENERATOR = Utils.getRandomInstance();
 148     private static final long DEFAULT_MIN_SIZE = 512;
 149     private static final long DEFAULT_MAX_SIZE = 1024;
 150     private static Object staticRef; // to prevent compile optimisations
 151 }