hotspot/test/gc/TestSoftReferencesBehaviorOnOOME.java
author hseigel
Thu, 02 Apr 2015 08:50:10 -0400
changeset 30136 1c5202fa105b
parent 29323 1e366d4e5472
child 29678 dd2f3932c21e
permissions -rw-r--r--
8076236: VM permits illegal flags for class init method Summary: Move check for multiple access flags so that it also covers instance initialization methods Reviewed-by: ctornqvi, lfoltan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27606
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
     1
/*
29323
1e366d4e5472 8073950: Quarantine failing test: gc/TestSoftReferencesBehaviorOnOOME.java due to JDK-8073669
jwilhelm
parents: 27896
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
27606
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
29323
1e366d4e5472 8073950: Quarantine failing test: gc/TestSoftReferencesBehaviorOnOOME.java due to JDK-8073669
jwilhelm
parents: 27896
diff changeset
    29
 * @ignore 8073669
27896
d4bd2f8b3055 8065865: gc/TestSoftReferencesBehaviorOnOOME.java: Error. Can't find source file: TestSoftReference.java
eistepan
parents: 27606
diff changeset
    30
 * @build TestSoftReferencesBehaviorOnOOME
27606
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    31
 * @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 512 2k
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    32
 * @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 128k 256k
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    33
 * @run main/othervm -Xmx128m TestSoftReferencesBehaviorOnOOME 2k 32k 10
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    34
 */
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    35
import com.oracle.java.testlibrary.Utils;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    36
import java.lang.ref.SoftReference;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    37
import java.util.LinkedList;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    38
import java.util.Random;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    39
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    40
public class TestSoftReferencesBehaviorOnOOME {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    41
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    42
    private static final Random rndGenerator = Utils.getRandomInstance();
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    43
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    44
    public static void main(String[] args) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    45
        int semiRefAllocFrequency = DEFAULT_FREQUENCY;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    46
        long minSize = DEFAULT_MIN_SIZE,
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    47
                maxSize = DEFAULT_MAX_SIZE;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    48
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    49
        if ( args.length >= 3 ) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    50
            semiRefAllocFrequency = Integer.parseInt(args[2]);
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    51
        }
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    52
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    53
        if ( args.length >= 2) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    54
            maxSize = getBytesCount(args[1]);
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    55
        }
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    56
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    57
        if ( args.length >= 1) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    58
            minSize = getBytesCount(args[0]);
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    59
        }
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    60
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    61
        new TestSoftReferencesBehaviorOnOOME().softReferencesOom(minSize, maxSize, semiRefAllocFrequency);
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
    /**
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    65
     * Test that all SoftReferences has been cleared at time of OOM.
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    66
     */
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    67
    void softReferencesOom(long minSize, long maxSize, int semiRefAllocFrequency) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    68
        System.out.format( "minSize = %d, maxSize = %d, freq = %d%n", minSize, maxSize, semiRefAllocFrequency );
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    69
        long counter = 0;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    70
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    71
        long multiplier = maxSize - minSize;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    72
        LinkedList<SoftReference> arrSoftRefs = new LinkedList();
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    73
        LinkedList arrObjects = new LinkedList();
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    74
        long numberOfNotNulledObjects = 0;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    75
        long oomSoftArraySize = 0;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    76
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    77
        try {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    78
            while (true) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    79
                // Keep every Xth object to make sure we hit OOM pretty fast
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    80
                if (counter % semiRefAllocFrequency != 0) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    81
                    long allocationSize = ((int) (rndGenerator.nextDouble() * multiplier))
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    82
                            + minSize;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    83
                    arrObjects.add(new byte[(int)allocationSize]);
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    84
                } else {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    85
                    arrSoftRefs.add(new SoftReference(new Object()));
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    86
                }
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    87
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    88
                counter++;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    89
                if (counter == Long.MAX_VALUE) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    90
                    counter = 0;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    91
                }
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    92
            }
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    93
        } catch (OutOfMemoryError oome) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    94
            // Clear allocated ballast, so we don't get another OOM.
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    95
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    96
            arrObjects = null;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    97
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    98
            // Get the number of soft refs first, so we don't trigger
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
    99
            // another OOM.
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   100
            oomSoftArraySize = arrSoftRefs.size();
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   101
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   102
            for (SoftReference sr : arrSoftRefs) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   103
                Object o = sr.get();
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   104
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   105
                if (o != null) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   106
                    numberOfNotNulledObjects++;
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
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   110
            // Make sure we clear all refs before we return failure
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   111
            arrSoftRefs = null;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   112
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   113
            if (numberOfNotNulledObjects > 0) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   114
                throw new RuntimeException(numberOfNotNulledObjects + " out of "
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   115
                        + oomSoftArraySize + " SoftReferences was not "
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   116
                        + "null at time of OutOfMemoryError");
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   117
            }
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   118
        } finally {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   119
            arrSoftRefs = null;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   120
            arrObjects = null;
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
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   124
    private static final long getBytesCount(String arg) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   125
        String postfixes = "kMGT";
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   126
        long mod = 1;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   127
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   128
        if (arg.trim().length() >= 2) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   129
            mod = postfixes.indexOf(
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   130
                    arg.trim().charAt(arg.length() - 1)
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   131
            );
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   132
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   133
            if (mod != -1) {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   134
                mod = (long) Math.pow(1024, mod+1);
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   135
                arg = arg.substring(0, arg.length() - 1);
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   136
            } else {
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   137
                mod = 1; // 10^0
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
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   141
        return Long.parseLong(arg) * mod;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   142
    }
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   143
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   144
    private static final long DEFAULT_MIN_SIZE = 512;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   145
    private static final long DEFAULT_MAX_SIZE = 1024;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   146
    private static final int DEFAULT_FREQUENCY = 4;
1c687c6de038 8059661: Test SoftReference and OOM behavior
azakharov
parents:
diff changeset
   147
}