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  * @key gc
  27  * @bug 8037924
  28  * @summary Verify that heap shrinking after full gc and dynamically changed free ratio
  29  * @author Andrey Zakharov /andrey.x.zakharov@oracle.com/
  30  * @library /testlibrary
  31  * @run main/othervm -XX:+UseAdaptiveSizePolicyWithSystemGC -XX:+UseParallelGC -XX:MinHeapFreeRatio=1
  32  * -XX:MaxHeapFreeRatio=100 -Xloggc:gc.log TestDynShrinkHeap
  33  */
  34 import java.lang.management.ManagementFactory;
  35 import java.lang.management.MemoryUsage;
  36 import java.util.ArrayList;
  37 import com.oracle.java.testlibrary.TestDynamicVMOption;
  38 import com.oracle.java.testlibrary.MemoryUsagePrinter;
  39 import sun.management.ManagementFactoryHelper;
  40 
  41 public class TestDynShrinkHeap {
  42 
  43     public static final String MinFreeRatioFlagName = "MinHeapFreeRatio";
  44     public static final String MaxFreeRatioFlagName = "MaxHeapFreeRatio";
  45 
  46     public static ArrayList<byte[]> list = new ArrayList<byte[]>(0);
  47     public static byte[] temp;
  48     public static final int M = 1024 * 1024; // to make heap more manageable by test code
  49 
  50     private final TestDynamicVMOption minRatioOption;
  51     private final TestDynamicVMOption maxRatioOption;
  52     
  53     public final class Labels {
  54         public static final String beforeAllocation = "before allocation";
  55         public static final String arrayHalfed      = "array halfed";
  56         public static final String underPressure    = "under pressure";
  57         public static final String beforeGc         = "before full GC";
  58         public static final String afterGc          = "after full GC";
  59         
  60     }
  61     
  62 
  63     public TestDynShrinkHeap() {
  64         minRatioOption = new TestDynamicVMOption(MinFreeRatioFlagName);
  65         maxRatioOption = new TestDynamicVMOption(MaxFreeRatioFlagName);
  66     }
  67 
  68     public void test() {
  69         int minHeapFreeValue = minRatioOption.getIntValue();
  70         int maxHeapFreeValue = maxRatioOption.getIntValue();
  71 
  72         // with maxHeapFreeRatio = 100 heap will not shrink at all, so
  73         if (maxHeapFreeValue == 100) {
  74             maxHeapFreeValue = 95;
  75             maxRatioOption.setIntValue(maxHeapFreeValue);
  76         }
  77 
  78         MemoryUsagePrinter.printMemoryUsage(Labels.beforeAllocation);
  79 
  80         for (int i = 0; i < M; i++) {
  81             list.add(new byte[1024]);
  82         }
  83 
  84         MemoryUsagePrinter.printMemoryUsage("allocated " + M + " arrays");
  85 
  86         list.subList(0, M / 2).clear();
  87         gc();
  88         MemoryUsagePrinter.printMemoryUsage(Labels.arrayHalfed);
  89         MemoryUsage muFull = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
  90 
  91         maxRatioOption.setIntValue(minHeapFreeValue + 1);
  92         gc();
  93         MemoryUsagePrinter.printMemoryUsage(Labels.underPressure);
  94         MemoryUsage muPressure = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
  95 
  96         if (!(muPressure.getCommitted() < muFull.getCommitted())) {
  97             throw new RuntimeException(
  98                     String.format("committed heap size under pressure is not less than committed full heap size, heap hasn't been shrunk?"
  99                             + "%n%s = %s%n%s = %s",
 100                             MinFreeRatioFlagName,
 101                             ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MinFreeRatioFlagName).getValue(), 
 102                             MaxFreeRatioFlagName,
 103                             ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MaxFreeRatioFlagName).getValue()
 104                     )
 105             );
 106         }
 107     }
 108 
 109     public static void main(String[] args) {
 110         new TestDynShrinkHeap().test();
 111     }
 112 
 113     public static void gc() {
 114         MemoryUsagePrinter.printMemoryUsage(Labels.beforeGc);
 115         System.gc();
 116         MemoryUsagePrinter.printMemoryUsage(Labels.afterGc);
 117     }
 118 
 119 }