test/hotspot/jtreg/gc/g1/TestPeriodicCollection.java
changeset 52918 f94c7929a44b
child 53523 4c5184c56dc2
equal deleted inserted replaced
52917:0c637249d934 52918:f94c7929a44b
       
     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 TestPeriodicCollection
       
    26  * @requires vm.gc.G1
       
    27  * @requires vm.compMode != "Xcomp"
       
    28  * @summary Verify that heap shrinks when the application is idle.
       
    29  * @library /test/lib /
       
    30  * @modules java.base/jdk.internal.misc
       
    31  * @modules java.management/sun.management
       
    32  * @run main/othervm -XX:MaxNewSize=32M -XX:InitialHeapSize=48M -Xmx128M -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=25 -XX:+UseG1GC -XX:G1PeriodicGCInterval=3000 -XX:+G1PeriodicGCInvokesConcurrent -Xlog:gc,gc+periodic=debug,gc+ergo+heap=debug TestPeriodicCollection
       
    33  * @run main/othervm -XX:MaxNewSize=32M -XX:InitialHeapSize=48M -Xmx128M -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=25 -XX:+UseG1GC -XX:G1PeriodicGCInterval=3000 -XX:-G1PeriodicGCInvokesConcurrent -Xlog:gc,gc+periodic=debug,gc+ergo+heap=debug TestPeriodicCollection
       
    34  */
       
    35 
       
    36 import com.sun.management.HotSpotDiagnosticMXBean;
       
    37 
       
    38 import gc.testlibrary.Helpers;
       
    39 
       
    40 import java.lang.management.GarbageCollectorMXBean;
       
    41 import java.lang.management.ManagementFactory;
       
    42 import java.lang.management.MemoryUsage;
       
    43 import java.text.NumberFormat;
       
    44 import static jdk.test.lib.Asserts.*;
       
    45 
       
    46 public class TestPeriodicCollection {
       
    47 
       
    48     public static final String MIN_FREE_RATIO_FLAG_NAME = "MinHeapFreeRatio";
       
    49     public static final String MAX_FREE_RATIO_FLAG_NAME = "MaxHeapFreeRatio";
       
    50 
       
    51     private static final int IDLE_TIME = 7 * 1000;
       
    52 
       
    53     private static boolean gcOccurred() {
       
    54         for (GarbageCollectorMXBean b : ManagementFactory.getGarbageCollectorMXBeans()) {
       
    55             if (b.getCollectionCount() != 0) {
       
    56                 return true;
       
    57             }
       
    58         }
       
    59         return false;
       
    60     }
       
    61 
       
    62     public static void main(String[] args) {
       
    63         MemoryUsage muInitial = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
       
    64         MemoryUsagePrinter.printMemoryUsage("initial", muInitial);
       
    65 
       
    66         if (gcOccurred()) {
       
    67           System.out.println("At least one garbage collection occurred. Exiting as this may have already shrunk the heap.");
       
    68           return;
       
    69         }
       
    70 
       
    71         try {
       
    72             Thread.sleep(IDLE_TIME);
       
    73         } catch (InterruptedException ie) {
       
    74             System.err.println("Skipped. Failed to wait for idle collection");
       
    75         }
       
    76 
       
    77         MemoryUsage muAfter = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
       
    78         MemoryUsagePrinter.printMemoryUsage("after", muAfter);
       
    79 
       
    80         assertLessThan(muAfter.getCommitted(), muInitial.getCommitted(), String.format(
       
    81                 "committed free heap size is not less than committed full heap size, heap hasn't been shrunk?%n"
       
    82                 + "%s = %s%n%s = %s",
       
    83                 MIN_FREE_RATIO_FLAG_NAME,
       
    84                 ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class)
       
    85                     .getVMOption(MIN_FREE_RATIO_FLAG_NAME).getValue(),
       
    86                 MAX_FREE_RATIO_FLAG_NAME,
       
    87                 ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class)
       
    88                     .getVMOption(MAX_FREE_RATIO_FLAG_NAME).getValue()
       
    89         ));
       
    90     }
       
    91 }
       
    92 
       
    93 /**
       
    94  * Prints memory usage to standard output
       
    95  */
       
    96 class MemoryUsagePrinter {
       
    97 
       
    98     public static final NumberFormat NF = Helpers.numberFormatter();
       
    99 
       
   100     public static void printMemoryUsage(String label, MemoryUsage memusage) {
       
   101         float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted();
       
   102         System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n",
       
   103                 label,
       
   104                 NF.format(memusage.getInit()),
       
   105                 NF.format(memusage.getUsed()),
       
   106                 NF.format(memusage.getCommitted()),
       
   107                 freeratio * 100
       
   108         );
       
   109     }
       
   110 }