test/gc/TestSoftReferencesBehaviorOnOOME.java

Thu, 05 Jul 2018 18:27:02 +0200

author
sgehwolf
date
Thu, 05 Jul 2018 18:27:02 +0200
changeset 9346
5ba59d58d976
parent 8625
f7b4a17a9d49
permissions
-rw-r--r--

8206425: .gnu_debuglink sections added unconditionally when no debuginfo is stripped
Summary: Only add .gnu_debuglink sections when there is some stripping done.
Reviewed-by: erikj, dholmes

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

mercurial