test/hotspot/jtreg/gc/stress/TestReclaimStringsLeaksMemory.java
author kvn
Thu, 07 Jun 2018 17:38:34 -0700
changeset 50455 2b73cce96dce
parent 48632 a8ab9344dab6
child 53523 4c5184c56dc2
permissions -rw-r--r--
8202611: [GRAAL] Exclude CMS GC testing from runs with Graal Reviewed-by: iignatyev, kbarrett
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48632
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
     1
/*
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
     4
 *
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
     8
 *
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    13
 * accompanied this code).
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    14
 *
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    18
 *
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    21
 * questions.
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    22
 */
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    23
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    24
/*
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    25
 * @test TestReclaimStringsLeaksMemory
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    26
 * @bug 8180048
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    27
 * @summary Ensure that during a Full GC interned string memory is reclaimed completely.
50455
2b73cce96dce 8202611: [GRAAL] Exclude CMS GC testing from runs with Graal
kvn
parents: 48632
diff changeset
    28
 * @requires vm.gc=="null" & !vm.graal.enabled & !vm.debug
48632
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    29
 * @key gc
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    30
 * @library /test/lib
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    31
 * @modules java.base/jdk.internal.misc
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    32
 * @run main/othervm TestReclaimStringsLeaksMemory
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    33
 * @run main/othervm TestReclaimStringsLeaksMemory -XX:+UseSerialGC
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    34
 * @run main/othervm TestReclaimStringsLeaksMemory -XX:+UseParallelGC
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    35
 * @run main/othervm TestReclaimStringsLeaksMemory -XX:+UseParallelGC -XX:-UseParallelOldGC
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    36
 * @run main/othervm TestReclaimStringsLeaksMemory -XX:+UseConcMarkSweepGC
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    37
 * @run main/othervm TestReclaimStringsLeaksMemory -XX:+UseG1GC
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    38
 */
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    39
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    40
import java.util.Arrays;
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    41
import java.util.ArrayList;
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    42
import java.util.regex.Pattern;
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    43
import java.util.regex.Matcher;
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    44
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    45
import jdk.test.lib.Asserts;
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    46
import jdk.test.lib.process.OutputAnalyzer;
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    47
import jdk.test.lib.process.ProcessTools;
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    48
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    49
public class TestReclaimStringsLeaksMemory {
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    50
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    51
    // The amount of memory in kB reserved in the "Symbol" category that indicates a memory leak for
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    52
    // this test.
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    53
    public static final int ReservedThreshold = 70000;
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    54
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    55
    public static void main(String[] args) throws Exception {
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    56
        ArrayList<String> baseargs = new ArrayList(Arrays.asList( "-Xms256M",
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    57
                                                                  "-Xmx256M",
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    58
                                                                  "-Xlog:gc*",
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    59
                                                                  "-XX:NativeMemoryTracking=summary",
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    60
                                                                  "-XX:+UnlockDiagnosticVMOptions",
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    61
                                                                  "-XX:+PrintNMTStatistics" ));
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    62
        baseargs.addAll(Arrays.asList(args));
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    63
        baseargs.add(GCTest.class.getName());
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    64
        ProcessBuilder pb_default =
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    65
            ProcessTools.createJavaProcessBuilder(baseargs.toArray(new String[] {}));
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    66
        verifySymbolMemoryUsageNotTooHigh(new OutputAnalyzer(pb_default.start()));
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    67
    }
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    68
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    69
    private static void verifySymbolMemoryUsageNotTooHigh(OutputAnalyzer output) throws Exception {
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    70
        String stdout = output.getStdout();
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    71
        System.out.println(stdout);
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    72
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    73
        Pattern p = Pattern.compile("Symbol \\(reserved=(\\d*)");
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    74
        Matcher m = p.matcher(stdout);
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    75
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    76
        if (!m.find()) {
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    77
            throw new RuntimeException("Could not find Symbol memory usage in NMT output");
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    78
        }
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    79
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    80
        int reserved = Integer.parseInt(m.group(1));
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    81
        Asserts.assertLT(reserved, ReservedThreshold, "Reserved memory size is " + reserved + "KB which is greater than or equal to " + ReservedThreshold + "KB indicating a memory leak");
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    82
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    83
        output.shouldHaveExitValue(0);
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    84
    }
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    85
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    86
    static class GCTest {
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    87
        public static final String BaseName = "SomeRandomBaseString";
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    88
        public static volatile String lastString;
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    89
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    90
        public static void main(String [] args) {
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    91
            for (int iterations = 0; iterations < 20;) {
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    92
                for (int i = 0; i < 1000000; i++) {
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    93
                    lastString = (BaseName + i).intern();
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    94
                }
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    95
                if (++iterations % 5 == 0) {
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    96
                   System.gc();
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    97
                }
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    98
            }
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
    99
        }
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
   100
    }
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
   101
}
a8ab9344dab6 8180280: [TESTBUG] Test for JDK-8180048
tschatzl
parents:
diff changeset
   102