1 /*
   2  * Copyright (c) 2014, 2015, 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 TestShrinkDefragmentedHeap
  26  * @bug 8038423
  27  * @summary Verify that heap shrinks after GC in the presence of fragmentation due to humongous objects
  28  *     1. allocate small objects mixed with humongous ones
  29  *        "ssssHssssHssssHssssHssssHssssHssssH"
  30  *     2. release all allocated object except the last humongous one
  31  *        "..................................H"
  32  *     3. invoke gc and check that memory returned to the system (amount of committed memory got down)
  33  *
  34  * @library /testlibrary
  35  * @modules java.base/sun.misc
  36  *          java.management/sun.management
  37  */
  38 import java.lang.management.ManagementFactory;
  39 import java.lang.management.MemoryUsage;
  40 import java.util.ArrayList;
  41 import java.util.List;
  42 import static jdk.test.lib.Asserts.*;
  43 import jdk.test.lib.ProcessTools;
  44 import jdk.test.lib.OutputAnalyzer;
  45 import com.sun.management.HotSpotDiagnosticMXBean;
  46 
  47 public class TestShrinkDefragmentedHeap {
  48     // Since we store all the small objects, they become old and old regions are also allocated at the bottom of the heap
  49     // together with humongous regions. So if there are a lot of old regions in the lower part of the heap,
  50     // the humongous regions will be allocated in the upper part of the heap anyway.
  51     // To avoid this the Eden needs to be big enough to fit all the small objects.
  52     private static final int INITIAL_HEAP_SIZE  = 200 * 1024 * 1024;
  53     private static final int MINIMAL_YOUNG_SIZE = 190 * 1024 * 1024;
  54     private static final int MAXIMUM_HEAP_SIZE  = 256 * 1024 * 1024;
  55     private static final int REGION_SIZE        = 1 * 1024 * 1024;
  56 
  57     public static void main(String[] args) throws Exception, Throwable {
  58         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  59                 "-XX:InitialHeapSize=" + INITIAL_HEAP_SIZE,
  60                 "-Xmn" + MINIMAL_YOUNG_SIZE,
  61                 "-Xmx" + MAXIMUM_HEAP_SIZE,
  62                 "-XX:MinHeapFreeRatio=10",
  63                 "-XX:MaxHeapFreeRatio=11",
  64                 "-XX:+UseG1GC",
  65                 "-XX:G1HeapRegionSize=" + REGION_SIZE,
  66                 "-XX:-ExplicitGCInvokesConcurrent",
  67                 "-verbose:gc",
  68                 GCTest.class.getName()
  69         );
  70 
  71         OutputAnalyzer output = ProcessTools.executeProcess(pb);
  72         output.shouldHaveExitValue(0);
  73     }
  74 
  75     static class GCTest {
  76 
  77         private static final String MIN_FREE_RATIO_FLAG_NAME = "MinHeapFreeRatio";
  78         private static final String MAX_FREE_RATIO_FLAG_NAME = "MaxHeapFreeRatio";
  79         private static final String NEW_SIZE_FLAG_NAME = "NewSize";
  80 
  81         private static final ArrayList<ArrayList<byte[]>> garbage = new ArrayList<>();
  82 
  83         private static final int SMALL_OBJS_SIZE  = 10 * 1024; // 10kB
  84         private static final int SMALL_OBJS_COUNT = MINIMAL_YOUNG_SIZE / (SMALL_OBJS_SIZE-1);
  85         private static final int ALLOCATE_COUNT = 3;
  86         // try to put all humongous object into gap between min young size and initial heap size
  87         // to avoid implicit GCs
  88         private static final int HUMONG_OBJS_SIZE = (int) Math.max(
  89                 (INITIAL_HEAP_SIZE - MINIMAL_YOUNG_SIZE) / ALLOCATE_COUNT / 4,
  90                 REGION_SIZE * 1.1
  91         );
  92 
  93         private static final long initialHeapSize = getHeapMemoryUsage().getUsed();
  94 
  95         public static void main(String[] args) throws InterruptedException {
  96             new GCTest().test();
  97         }
  98 
  99         private void test() throws InterruptedException {
 100             MemoryUsagePrinter.printMemoryUsage("init");
 101 
 102             allocate();
 103             System.gc();
 104             MemoryUsage muFull = getHeapMemoryUsage();
 105             MemoryUsagePrinter.printMemoryUsage("allocated");
 106 
 107             free();
 108             //Thread.sleep(1000); // sleep before measures due lags in JMX
 109             MemoryUsage muFree = getHeapMemoryUsage();
 110             MemoryUsagePrinter.printMemoryUsage("free");
 111 
 112             assertLessThan(muFree.getCommitted(), muFull.getCommitted(), prepareMessageCommittedIsNotLess() );
 113         }
 114 
 115         private void allocate() {
 116             System.out.format("Will allocate objects of small size = %s and humongous size = %s",
 117                     MemoryUsagePrinter.humanReadableByteCount(SMALL_OBJS_SIZE, false),
 118                     MemoryUsagePrinter.humanReadableByteCount(HUMONG_OBJS_SIZE, false)
 119             );
 120 
 121             for (int i = 0; i < ALLOCATE_COUNT; i++) {
 122                 ArrayList<byte[]> stuff = new ArrayList<>();
 123                 allocateList(stuff, SMALL_OBJS_COUNT / ALLOCATE_COUNT, SMALL_OBJS_SIZE);
 124                 garbage.add(stuff);
 125 
 126                 ArrayList<byte[]> humongousStuff = new ArrayList<>();
 127                 allocateList(humongousStuff, 4, HUMONG_OBJS_SIZE);
 128                 garbage.add(humongousStuff);
 129             }
 130         }
 131 
 132         private void free() {
 133             // do not free last one list
 134             garbage.subList(0, garbage.size() - 1).clear();
 135 
 136             // do not free last one element from last list
 137             ArrayList stuff = garbage.get(garbage.size() - 1);
 138             if (stuff.size() > 1) {
 139                 stuff.subList(0, stuff.size() - 1).clear();
 140             }
 141             System.gc();
 142         }
 143 
 144         private String prepareMessageCommittedIsNotLess() {
 145             return String.format(
 146                     "committed free heap size is not less than committed full heap size, heap hasn't been shrunk?%n"
 147                     + "%s = %s%n%s = %s",
 148                     MIN_FREE_RATIO_FLAG_NAME,
 149                     ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class)
 150                         .getVMOption(MIN_FREE_RATIO_FLAG_NAME).getValue(),
 151                     MAX_FREE_RATIO_FLAG_NAME,
 152                     ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class)
 153                         .getVMOption(MAX_FREE_RATIO_FLAG_NAME).getValue()
 154             );
 155         }
 156 
 157         private static void allocateList(List garbage, int count, int size) {
 158             for (int i = 0; i < count; i++) {
 159                 garbage.add(new byte[size]);
 160             }
 161         }
 162     }
 163 
 164     static MemoryUsage getHeapMemoryUsage() {
 165         return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
 166     }
 167 
 168     /**
 169      * Prints memory usage to standard output
 170      */
 171     static class MemoryUsagePrinter {
 172 
 173         public static String humanReadableByteCount(long bytes, boolean si) {
 174             int unit = si ? 1000 : 1024;
 175             if (bytes < unit) {
 176                 return bytes + " B";
 177             }
 178             int exp = (int) (Math.log(bytes) / Math.log(unit));
 179             String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
 180             return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
 181         }
 182 
 183         public static void printMemoryUsage(String label) {
 184             MemoryUsage memusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
 185             float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted();
 186             System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n",
 187                     label,
 188                     humanReadableByteCount(memusage.getInit(), false),
 189                     humanReadableByteCount(memusage.getUsed(), false),
 190                     humanReadableByteCount(memusage.getCommitted(), false),
 191                     freeratio * 100
 192             );
 193         }
 194     }
 195 }