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