1 /*
   2  * Copyright (c) 2018, 2020, 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"
  31  * @requires !vm.debug
  32  * @library /test/lib
  33  * @modules java.base/jdk.internal.misc
  34  * @run driver gc.stress.TestReclaimStringsLeaksMemory
  35  * @run driver gc.stress.TestReclaimStringsLeaksMemory -XX:+UseSerialGC
  36  * @run driver gc.stress.TestReclaimStringsLeaksMemory -XX:+UseParallelGC
  37  * @run driver gc.stress.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 
  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 = ProcessTools.createJavaProcessBuilder(baseargs);
  65         verifySymbolMemoryUsageNotTooHigh(new OutputAnalyzer(pb.start()));
  66     }
  67 
  68     private static void verifySymbolMemoryUsageNotTooHigh(OutputAnalyzer output) throws Exception {
  69         String stdout = output.getStdout();
  70         System.out.println(stdout);
  71 
  72         Pattern p = Pattern.compile("Symbol \\(reserved=(\\d*)");
  73         Matcher m = p.matcher(stdout);
  74 
  75         if (!m.find()) {
  76             throw new RuntimeException("Could not find Symbol memory usage in NMT output");
  77         }
  78 
  79         int reserved = Integer.parseInt(m.group(1));
  80         Asserts.assertLT(reserved, ReservedThreshold, "Reserved memory size is " + reserved + "KB which is greater than or equal to " + ReservedThreshold + "KB indicating a memory leak");
  81 
  82         output.shouldHaveExitValue(0);
  83     }
  84 
  85     static class GCTest {
  86         public static final String BaseName = "SomeRandomBaseString";
  87         public static volatile String lastString;
  88 
  89         public static void main(String [] args) {
  90             for (int iterations = 0; iterations < 20;) {
  91                 for (int i = 0; i < 1000000; i++) {
  92                     lastString = (BaseName + i).intern();
  93                 }
  94                 if (++iterations % 5 == 0) {
  95                     System.gc();
  96                 }
  97             }
  98             // Do one last GC and sleep to give ServiceThread a chance to run.
  99             System.out.println("One last gc");
 100             System.gc();
 101             for (int i = 0; i < 100; i++) {
 102                 try {
 103                     Thread.sleep(10);
 104                 } catch (InterruptedException ex) {
 105                 }
 106             }
 107             System.out.println("End of test");
 108         }
 109     }
 110 }
 111