1 /*
   2 * Copyright (c) 2013, 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 TestDisableExplicitGCFlag
  26  * @key gc
  27  * @bug 8026047
  28  * @summary Verify that DisableExplicitGC flag affect System.gc() calls treatment
  29  * @library /testlibrary
  30  * @build TestDisableExplicitGCFlag
  31  * @run main/othervm TestDisableExplicitGCFlag
  32  */
  33 
  34 import com.oracle.java.testlibrary.*;
  35 
  36 public class TestDisableExplicitGCFlag {
  37 
  38     public static void main(String args[]) throws Exception {
  39         String options = System.getProperty("test.java.opts");
  40         if (options == null) {
  41             options = "";
  42         }
  43 
  44         options = options.replaceAll("-Xloggc:[^ ]*", "");
  45 
  46         testDisableExplicitGC(true, options);
  47         testDisableExplicitGC(false, options);
  48     }
  49 
  50     public static void testDisableExplicitGC(boolean disableGC, String options) throws Exception {
  51         options += (disableGC ? " -XX:+DisableExplicitGC" : " -XX:-DisableExplicitGC") +
  52                    " -XX:+PrintGC " + GCProvoker.class.getName();
  53 
  54         options = options.replaceAll("[\\s]+", " ").trim();
  55 
  56         ProcessBuilder procBuilder = ProcessTools.createJavaProcessBuilder(options.split(" "));
  57         OutputAnalyzer analyzer = new OutputAnalyzer(procBuilder.start());
  58 
  59         analyzer.shouldHaveExitValue(0);
  60 
  61         if (disableGC) {
  62             analyzer.shouldNotContain("System.gc");
  63         } else {
  64             analyzer.shouldContain("System.gc");
  65         }
  66     }
  67 
  68     public static class GCProvoker {
  69 
  70         public static void main(String args[]) {
  71             for (int i = 0; i < 100; i++) {
  72                 System.gc();
  73             }
  74         }
  75 
  76     }
  77 
  78 }