hotspot/test/gc/TestSoftReferencesBehaviorOnOOME.java
author lfoltan
Fri, 23 Jan 2015 09:54:33 -0500
changeset 28731 f7339cba0a6a
parent 27896 d4bd2f8b3055
child 29323 1e366d4e5472
permissions -rw-r--r--
8067480: Crash in klassItable::initialize_itable_for_interface when running vm.runtime.defmeth.StaticMethodsTest. Summary: A static method should be skipped in entirety by find_instance_method(), searching should continue to find an overpass method of the same name and signature if present. Reviewed-by: acorn, hseigel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27606
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
     1
/*
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
     2
 * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
     4
 *
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
     8
 *
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    13
 * accompanied this code).
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    14
 *
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    18
 *
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    21
 * questions.
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    22
 */
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    23
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    24
/**
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    25
 * @test TestSoftReferencesBehaviorOnOOME
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    26
 * @key gc
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    27
 * @summary Tests that all SoftReferences has been cleared at time of OOM.
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    28
 * @library /testlibrary
27896
d4bd2f8b3055 8065865: gc/TestSoftReferencesBehaviorOnOOME.java: Error. Can't find source file: TestSoftReference.java
eistepan
parents: 27606
diff changeset
    29
 * @build TestSoftReferencesBehaviorOnOOME
27606
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    30
 * @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 512 2k
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    31
 * @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 128k 256k
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    32
 * @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 2k 32k 10
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    33
 */
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    34
import com.oracle.java.testlibrary.Utils;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    35
import java.lang.ref.SoftReference;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    36
import java.util.LinkedList;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    37
import java.util.Random;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    38
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    39
public class TestSoftReferencesBehaviorOnOOME {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    40
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    41
    private static final Random rndGenerator = Utils.getRandomInstance();
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    42
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    43
    public static void main(String[] args) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    44
        int semiRefAllocFrequency = DEFAULT_FREQUENCY;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    45
        long minSize = DEFAULT_MIN_SIZE,
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    46
                maxSize = DEFAULT_MAX_SIZE;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    47
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    48
        if ( args.length >= 3 ) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    49
            semiRefAllocFrequency = Integer.parseInt(args[2]);
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    50
        }
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    51
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    52
        if ( args.length >= 2) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    53
            maxSize = getBytesCount(args[1]);
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    54
        }
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    55
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    56
        if ( args.length >= 1) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    57
            minSize = getBytesCount(args[0]);
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    58
        }
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    59
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    60
        new TestSoftReferencesBehaviorOnOOME().softReferencesOom(minSize, maxSize, semiRefAllocFrequency);
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    61
    }
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    62
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    63
    /**
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    64
     * Test that all SoftReferences has been cleared at time of OOM.
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    65
     */
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    66
    void softReferencesOom(long minSize, long maxSize, int semiRefAllocFrequency) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    67
        System.out.format( "minSize = %d, maxSize = %d, freq = %d%n", minSize, maxSize, semiRefAllocFrequency );
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    68
        long counter = 0;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    69
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    70
        long multiplier = maxSize - minSize;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    71
        LinkedList<SoftReference> arrSoftRefs = new LinkedList();
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    72
        LinkedList arrObjects = new LinkedList();
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    73
        long numberOfNotNulledObjects = 0;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    74
        long oomSoftArraySize = 0;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    75
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    76
        try {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    77
            while (true) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    78
                // Keep every Xth object to make sure we hit OOM pretty fast
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    79
                if (counter % semiRefAllocFrequency != 0) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    80
                    long allocationSize = ((int) (rndGenerator.nextDouble() * multiplier))
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    81
                            + minSize;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    82
                    arrObjects.add(new byte[(int)allocationSize]);
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    83
                } else {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    84
                    arrSoftRefs.add(new SoftReference(new Object()));
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    85
                }
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    86
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    87
                counter++;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    88
                if (counter == Long.MAX_VALUE) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    89
                    counter = 0;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    90
                }
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    91
            }
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    92
        } catch (OutOfMemoryError oome) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    93
            // Clear allocated ballast, so we don't get another OOM.
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    94
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    95
            arrObjects = null;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    96
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    97
            // Get the number of soft refs first, so we don't trigger
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    98
            // another OOM.
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    99
            oomSoftArraySize = arrSoftRefs.size();
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   100
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   101
            for (SoftReference sr : arrSoftRefs) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   102
                Object o = sr.get();
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   103
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   104
                if (o != null) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   105
                    numberOfNotNulledObjects++;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   106
                }
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   107
            }
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   108
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   109
            // Make sure we clear all refs before we return failure
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   110
            arrSoftRefs = null;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   111
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   112
            if (numberOfNotNulledObjects > 0) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   113
                throw new RuntimeException(numberOfNotNulledObjects + " out of "
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   114
                        + oomSoftArraySize + " SoftReferences was not "
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   115
                        + "null at time of OutOfMemoryError");
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   116
            }
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   117
        } finally {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   118
            arrSoftRefs = null;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   119
            arrObjects = null;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   120
        }
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   121
    }
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   122
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   123
    private static final long getBytesCount(String arg) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   124
        String postfixes = "kMGT";
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   125
        long mod = 1;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   126
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   127
        if (arg.trim().length() >= 2) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   128
            mod = postfixes.indexOf(
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   129
                    arg.trim().charAt(arg.length() - 1)
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   130
            );
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   131
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   132
            if (mod != -1) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   133
                mod = (long) Math.pow(1024, mod+1);
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   134
                arg = arg.substring(0, arg.length() - 1);
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   135
            } else {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   136
                mod = 1; // 10^0
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   137
            }
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   138
        }
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   139
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   140
        return Long.parseLong(arg) * mod;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   141
    }
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   142
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   143
    private static final long DEFAULT_MIN_SIZE = 512;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   144
    private static final long DEFAULT_MAX_SIZE = 1024;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   145
    private static final int DEFAULT_FREQUENCY = 4;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   146
}