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