1 /*
   2  * Copyright (c) 2014, 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 TestDynShrinkHeap
  26  * @bug 8016479
  27  * @summary Verify that the heap shrinks after full GC according to the current values of the Min/MaxHeapFreeRatio flags
  28  * @library /testlibrary
  29  * @run main/othervm -XX:+UseAdaptiveSizePolicyWithSystemGC -XX:+UseParallelGC -XX:MinHeapFreeRatio=0 -XX:MaxHeapFreeRatio=100 -Xmx1g -verbose:gc TestDynShrinkHeap
  30  */
  31 import com.oracle.java.testlibrary.DynamicVMOption;
  32 import java.lang.management.ManagementFactory;
  33 import java.lang.management.MemoryUsage;
  34 import java.util.ArrayList;
  35 import sun.management.ManagementFactoryHelper;
  36 import static com.oracle.java.testlibrary.Asserts.*;
  37 
  38 public class TestDynShrinkHeap {
  39 
  40     public static final String MIN_FREE_RATIO_FLAG_NAME = "MinHeapFreeRatio";
  41     public static final String MAX_FREE_RATIO_FLAG_NAME = "MaxHeapFreeRatio";
  42 
  43     private static ArrayList<byte[]> list = new ArrayList<>(0);
  44     private static final int LEN = 512 * 1024 + 1;
  45 
  46     public TestDynShrinkHeap() {
  47     }
  48 
  49     private final void test() {
  50         System.gc();
  51         MemoryUsagePrinter.printMemoryUsage("init");
  52 
  53         eat();
  54         MemoryUsagePrinter.printMemoryUsage("eaten");
  55         MemoryUsage muFull = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
  56 
  57         free();
  58         MemoryUsagePrinter.printMemoryUsage("free");
  59         MemoryUsage muFree = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
  60 
  61         assertLessThan(muFree.getCommitted(), muFull.getCommitted(), String.format(
  62                 "committed free heap size is not less than committed full heap size, heap hasn't been shrunk?%n"
  63                 + "%s = %s%n%s = %s",
  64                 MIN_FREE_RATIO_FLAG_NAME,
  65                 ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MIN_FREE_RATIO_FLAG_NAME).getValue(),
  66                 MAX_FREE_RATIO_FLAG_NAME,
  67                 ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MAX_FREE_RATIO_FLAG_NAME).getValue()
  68         ));
  69     }
  70 
  71     private void eat() {
  72         for (int i = 0; i < LEN; i++) {
  73             list.add(new byte[1024]);
  74         }
  75         MemoryUsagePrinter.printMemoryUsage("allocated " + LEN + " arrays");
  76 
  77         list.subList(0, LEN / 2).clear();
  78         System.gc();
  79         MemoryUsagePrinter.printMemoryUsage("array halved");
  80     }
  81 
  82     private void free() {
  83         int min = DynamicVMOption.getInt(MIN_FREE_RATIO_FLAG_NAME);
  84         DynamicVMOption.setInt(MAX_FREE_RATIO_FLAG_NAME, min);
  85         System.gc();
  86         MemoryUsagePrinter.printMemoryUsage("under pressure");
  87     }
  88 
  89     public static void main(String[] args) {
  90         new TestDynShrinkHeap().test();
  91     }
  92 }
  93 
  94 /**
  95  * Prints memory usage to standard output
  96  */
  97 class MemoryUsagePrinter {
  98 
  99     public static String humanReadableByteCount(long bytes, boolean si) {
 100         int unit = si ? 1000 : 1024;
 101         if (bytes < unit) {
 102             return bytes + " B";
 103         }
 104         int exp = (int) (Math.log(bytes) / Math.log(unit));
 105         String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
 106         return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
 107     }
 108 
 109     public static void printMemoryUsage(String label) {
 110         MemoryUsage memusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
 111         float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted();
 112         System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n",
 113                 label,
 114                 humanReadableByteCount(memusage.getInit(), true),
 115                 humanReadableByteCount(memusage.getUsed(), true),
 116                 humanReadableByteCount(memusage.getCommitted(), true),
 117                 freeratio * 100
 118         );
 119     }
 120 }