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