test/gc/TestSoftReferencesBehaviorOnOOME.java

Print this page
rev 7970 : imported patch TestSoftReferencesBehaviorOnOOME.patch


   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 }


   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  * @build TestSoftReferencesBehaviorOnOOME
  30  * @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 512 2k
  31  * @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 128k 256k
  32  * @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 2k 32k
  33  */
  34 import com.oracle.java.testlibrary.Utils;
  35 import com.oracle.java.testlibrary.Asserts;
  36 import java.lang.ref.SoftReference;
  37 import java.util.LinkedList;
  38 import java.util.Random;
  39 
  40 public class TestSoftReferencesBehaviorOnOOME {
  41 
  42     /**
  43      * Test generates a lot of soft references to objects with random payloads.
  44      * Then it provokes OOME and checks that all SoftReferences has been gone
  45      * @param args - [minSize] [maxSize] [freq]
  46      *  where
  47      *  - minSize - min size of random objects
  48      *  - maxSize - max size of random objects
  49      */
  50     public static void main(String[] args) {

  51         long minSize = DEFAULT_MIN_SIZE,
  52                 maxSize = DEFAULT_MAX_SIZE;
  53 




  54         if ( args.length >= 2) {
  55             maxSize = getBytesCount(args[1]);
  56         }
  57 
  58         if ( args.length >= 1) {
  59             minSize = getBytesCount(args[0]);
  60         }
  61 
  62         new TestSoftReferencesBehaviorOnOOME().softReferencesOom(minSize, maxSize);
  63     }
  64 
  65     /**
  66      * Test that all SoftReferences has been cleared at time of OOM.
  67      */
  68     void softReferencesOom(long minSize, long maxSize) {
  69         System.out.format( "minSize = %d, maxSize = %d%n", minSize, maxSize );

  70 

  71         LinkedList<SoftReference> arrSoftRefs = new LinkedList();
  72         staticRef = arrSoftRefs;
  73         LinkedList arrObjects = new LinkedList();
  74         staticRef = arrObjects;
  75 
  76         long multiplier = maxSize - minSize;
  77         long numberOfNotNulledObjects = 0;
  78         long oomSoftArraySize;
  79 
  80         try {









  81 
  82             // Lets allocate as many as we can - taking size of all SoftRerefences
  83             // by minimum. So it can provoke some GC but we surely will allocate enough.
  84             long numSofts = (long) ((0.95 * Runtime.getRuntime().totalMemory()) / minSize);
  85             System.out.println("num Soft: " + numSofts);
  86 
  87             while (numSofts-- > 0) {
  88                 int allocationSize = ((int) (RND_GENERATOR.nextDouble() * multiplier))
  89                             + (int)minSize;
  90                 arrSoftRefs.add(new SoftReference(new byte[allocationSize]));
  91             }
  92 
  93             System.out.println("free: " + Runtime.getRuntime().freeMemory());
  94 
  95             // provoke OOME.
  96             while (true) {
  97                 arrObjects.add(new byte[(int) Runtime.getRuntime().totalMemory()]);
  98             }
  99 
 100         } catch (OutOfMemoryError oome) {

 101 
 102             // Clear allocated ballast, so we don't get another OOM.
 103             staticRef = null;
 104             arrObjects = null;
 105 
 106             // Get the number of soft refs first, so we don't trigger
 107             // another OOM.
 108             oomSoftArraySize = arrSoftRefs.size();
 109 
 110             for (SoftReference sr : arrSoftRefs) {
 111                 Object o = sr.get();
 112 
 113                 if (o != null) {
 114                     numberOfNotNulledObjects++;
 115                 }
 116             }
 117 
 118             // Make sure we clear all refs before we return failure
 119             arrSoftRefs = null;
 120 
 121             Asserts.assertFalse(numberOfNotNulledObjects > 0,
 122                     "" + numberOfNotNulledObjects + " out of "
 123                     + oomSoftArraySize + " SoftReferences was not "
 124                     + "null at time of OutOfMemoryError"
 125             );
 126         } finally {
 127             Asserts.assertTrue(arrObjects == null, "OOME hasn't been provoked");
 128             Asserts.assertTrue(arrSoftRefs == null, "OOME hasn't been provoked");
 129         }
 130     }
 131 
 132     private static final long getBytesCount(String arg) {
 133         String postfixes = "kMGT";
 134         long mod = 1;
 135 
 136         if (arg.trim().length() >= 2) {
 137             mod = postfixes.indexOf(arg.trim().charAt(arg.length() - 1));


 138 
 139             if (mod != -1) {
 140                 mod = (long) Math.pow(1024, mod+1);
 141                 arg = arg.substring(0, arg.length() - 1);
 142             } else {
 143                 mod = 1; // 10^0
 144             }
 145         }
 146 
 147         return Long.parseLong(arg) * mod;
 148     }
 149 
 150     private static final Random RND_GENERATOR = Utils.getRandomInstance();
 151     private static final long DEFAULT_MIN_SIZE = 512;
 152     private static final long DEFAULT_MAX_SIZE = 1024;
 153     private static Object staticRef; // to prevent compile optimisations
 154 }