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 REGION_SIZE        = 1 * 1024 * 1024;
  55 
  56     public static void main(String[] args) throws Exception, Throwable {
  57         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  58                 "-XX:InitialHeapSize=" + INITIAL_HEAP_SIZE,
  59                 "-Xmn" + MINIMAL_YOUNG_SIZE,
  60                 "-XX:MinHeapFreeRatio=10",
  61                 "-XX:MaxHeapFreeRatio=11",
  62                 "-XX:+UseG1GC",
  63                 "-XX:G1HeapRegionSize=" + REGION_SIZE,
  64                 "-XX:-ExplicitGCInvokesConcurrent",
  65                 "-verbose:gc",
  66                 GCTest.class.getName()
  67         );
  68 
  69         OutputAnalyzer output = ProcessTools.executeProcess(pb);
  70         output.shouldHaveExitValue(0);
  71     }
  72 
  73     static class GCTest {
  74 
  75         private static final String MIN_FREE_RATIO_FLAG_NAME = "MinHeapFreeRatio";
  76         private static final String MAX_FREE_RATIO_FLAG_NAME = "MaxHeapFreeRatio";
  77         private static final String NEW_SIZE_FLAG_NAME = "NewSize";
  78 
  79         private static final ArrayList<ArrayList<byte[]>> garbage = new ArrayList<>();
  80 
  81         private static final int SMALL_OBJS_SIZE  = 10 * 1024; // 10kB
  82         private static final int SMALL_OBJS_COUNT = MINIMAL_YOUNG_SIZE / (SMALL_OBJS_SIZE-1);
  83         private static final int ALLOCATE_COUNT = 3;
  84         // try to put all humongous object into gap between min young size and initial heap size
  85         // to avoid implicit GCs
  86         private static final int HUMONG_OBJS_SIZE = (int) Math.max(
  87                 (INITIAL_HEAP_SIZE - MINIMAL_YOUNG_SIZE) / ALLOCATE_COUNT / 4,
  88                 REGION_SIZE * 1.1
  89         );
  90 
  91         private static final long initialHeapSize = getHeapMemoryUsage().getUsed();
  92 
  93         public static void main(String[] args) throws InterruptedException {
  94             new GCTest().test();
  95         }
  96 
  97         private void test() throws InterruptedException {
  98             MemoryUsagePrinter.printMemoryUsage("init");
  99 
 100             allocate();
 101             System.gc();
 102             MemoryUsage muFull = getHeapMemoryUsage();
 103             MemoryUsagePrinter.printMemoryUsage("allocated");
 104 
 105             free();
 106             //Thread.sleep(1000); // sleep before measures due lags in JMX
 107             MemoryUsage muFree = getHeapMemoryUsage();
 108             MemoryUsagePrinter.printMemoryUsage("free");
 109 
 110             assertLessThan(muFree.getCommitted(), muFull.getCommitted(), prepareMessageCommittedIsNotLess() );
 111         }
 112 
 113         private void allocate() {
 114             System.out.format("Will allocate objects of small size = %s and humongous size = %s",
 115                     MemoryUsagePrinter.humanReadableByteCount(SMALL_OBJS_SIZE, false),
 116                     MemoryUsagePrinter.humanReadableByteCount(HUMONG_OBJS_SIZE, false)
 117             );
 118 
 119             for (int i = 0; i < ALLOCATE_COUNT; i++) {
 120                 ArrayList<byte[]> stuff = new ArrayList<>();
 121                 allocateList(stuff, SMALL_OBJS_COUNT / ALLOCATE_COUNT, SMALL_OBJS_SIZE);
 122                 garbage.add(stuff);
 123 
 124                 ArrayList<byte[]> humongousStuff = new ArrayList<>();
 125                 allocateList(humongousStuff, 4, HUMONG_OBJS_SIZE);
 126                 garbage.add(humongousStuff);
 127             }
 128         }
 129 
 130         private void free() {
 131             // do not free last one list
 132             garbage.subList(0, garbage.size() - 1).clear();
 133 
 134             // do not free last one element from last list
 135             ArrayList stuff = garbage.get(garbage.size() - 1);
 136             if (stuff.size() > 1) {
 137                 stuff.subList(0, stuff.size() - 1).clear();
 138             }
 139             System.gc();
 140         }
 141 
 142         private String prepareMessageCommittedIsNotLess() {
 143             return String.format(
 144                     "committed free heap size is not less than committed full heap size, heap hasn't been shrunk?%n"
 145                     + "%s = %s%n%s = %s",
 146                     MIN_FREE_RATIO_FLAG_NAME,
 147                     ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class)
 148                         .getVMOption(MIN_FREE_RATIO_FLAG_NAME).getValue(),
 149                     MAX_FREE_RATIO_FLAG_NAME,
 150                     ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class)
 151                         .getVMOption(MAX_FREE_RATIO_FLAG_NAME).getValue()
 152             );
 153         }
 154 
 155         private static void allocateList(List garbage, int count, int size) {
 156             for (int i = 0; i < count; i++) {
 157                 garbage.add(new byte[size]);
 158             }
 159         }
 160     }
 161 
 162     static MemoryUsage getHeapMemoryUsage() {
 163         return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
 164     }
 165 
 166     /**
 167      * Prints memory usage to standard output
 168      */
 169     static class MemoryUsagePrinter {
 170 
 171         public static String humanReadableByteCount(long bytes, boolean si) {
 172             int unit = si ? 1000 : 1024;
 173             if (bytes < unit) {
 174                 return bytes + " B";
 175             }
 176             int exp = (int) (Math.log(bytes) / Math.log(unit));
 177             String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
 178             return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
 179         }
 180 
 181         public static void printMemoryUsage(String label) {
 182             MemoryUsage memusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
 183             float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted();
 184             System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n",
 185                     label,
 186                     humanReadableByteCount(memusage.getInit(), false),
 187                     humanReadableByteCount(memusage.getUsed(), false),
 188                     humanReadableByteCount(memusage.getCommitted(), false),
 189                     freeratio * 100
 190             );
 191         }
 192     }
 193 }