1 /* 2 * Copyright (c) 2018, 2019, 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 */ 23 24 package gc.stress; 25 26 /* 27 * @test TestReclaimStringsLeaksMemory 28 * @bug 8180048 29 * @summary Ensure that during a Full GC interned string memory is reclaimed completely. 30 * @requires vm.gc=="null" & !vm.graal.enabled & !vm.debug 31 * @key gc 32 * @library /test/lib 33 * @modules java.base/jdk.internal.misc 34 * @run main/othervm gc.stress.TestReclaimStringsLeaksMemory 35 * @run main/othervm gc.stress.TestReclaimStringsLeaksMemory -XX:+UseSerialGC 36 * @run main/othervm gc.stress.TestReclaimStringsLeaksMemory -XX:+UseParallelGC 37 * @run main/othervm gc.stress.TestReclaimStringsLeaksMemory -XX:+UseParallelGC -XX:-UseParallelOldGC 38 * @run main/othervm gc.stress.TestReclaimStringsLeaksMemory -XX:+UseConcMarkSweepGC 39 * @run main/othervm gc.stress.TestReclaimStringsLeaksMemory -XX:+UseG1GC 40 */ 41 42 import java.util.Arrays; 43 import java.util.ArrayList; 44 import java.util.regex.Pattern; 45 import java.util.regex.Matcher; 46 47 import jdk.test.lib.Asserts; 48 import jdk.test.lib.process.OutputAnalyzer; 49 import jdk.test.lib.process.ProcessTools; 50 51 public class TestReclaimStringsLeaksMemory { 52 53 // The amount of memory in kB reserved in the "Symbol" category that indicates a memory leak for 54 // this test. 55 public static final int ReservedThreshold = 70000; 56 57 public static void main(String[] args) throws Exception { 58 ArrayList<String> baseargs = new ArrayList(Arrays.asList( "-Xms256M", 59 "-Xmx256M", 60 "-Xlog:gc*", 61 "-XX:NativeMemoryTracking=summary", 62 "-XX:+UnlockDiagnosticVMOptions", 63 "-XX:+PrintNMTStatistics" )); 64 baseargs.addAll(Arrays.asList(args)); 65 baseargs.add(GCTest.class.getName()); 66 ProcessBuilder pb_default = 67 ProcessTools.createJavaProcessBuilder(baseargs.toArray(new String[] {})); 68 verifySymbolMemoryUsageNotTooHigh(new OutputAnalyzer(pb_default.start())); 69 } 70 71 private static void verifySymbolMemoryUsageNotTooHigh(OutputAnalyzer output) throws Exception { 72 String stdout = output.getStdout(); 73 System.out.println(stdout); 74 75 Pattern p = Pattern.compile("Symbol \\(reserved=(\\d*)"); 76 Matcher m = p.matcher(stdout); 77 78 if (!m.find()) { 79 throw new RuntimeException("Could not find Symbol memory usage in NMT output"); 80 } 81 82 int reserved = Integer.parseInt(m.group(1)); 83 Asserts.assertLT(reserved, ReservedThreshold, "Reserved memory size is " + reserved + "KB which is greater than or equal to " + ReservedThreshold + "KB indicating a memory leak"); 84 85 output.shouldHaveExitValue(0); 86 } 87 88 static class GCTest { 89 public static final String BaseName = "SomeRandomBaseString"; 90 public static volatile String lastString; 91 92 public static void main(String [] args) { 93 for (int iterations = 0; iterations < 20;) { 94 for (int i = 0; i < 1000000; i++) { 95 lastString = (BaseName + i).intern(); 96 } 97 if (++iterations % 5 == 0) { 98 System.gc(); 99 } 100 } 101 } 102 } 103 } 104