Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/g1/TestHumongousShrinkHeap.java
32284 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test TestHumongousShrinkHeap25* @bug 8036025 805604326* @requires vm.gc=="G1" | vm.gc=="null"27* @summary Verify that heap shrinks after GC in the presence of fragmentation28* due to humongous objects29* @library /testlibrary30* @run main/othervm -XX:-ExplicitGCInvokesConcurrent -XX:MinHeapFreeRatio=1031* -XX:MaxHeapFreeRatio=12 -XX:+UseG1GC -XX:G1HeapRegionSize=1M -verbose:gc32* TestHumongousShrinkHeap33*/3435import java.lang.management.ManagementFactory;36import java.lang.management.MemoryUsage;37import java.util.ArrayList;38import java.util.List;39import sun.management.ManagementFactoryHelper;40import static com.oracle.java.testlibrary.Asserts.*;4142public class TestHumongousShrinkHeap {4344public static final String MIN_FREE_RATIO_FLAG_NAME = "MinHeapFreeRatio";45public static final String MAX_FREE_RATIO_FLAG_NAME = "MaxHeapFreeRatio";4647private static final List<List<byte[]>> garbage = new ArrayList();48private static final int REGION_SIZE = 1024 * 1024; // 1M49private static final int LISTS_COUNT = 10;50private static final int HUMON_SIZE = Math.round(.9f * REGION_SIZE);5152private static final long TOTAL_MEMORY = Runtime.getRuntime().totalMemory();53private static final long MAX_MEMORY = Runtime.getRuntime().maxMemory();5455private static final int HUMON_COUNT = (int) ((TOTAL_MEMORY / HUMON_SIZE) / LISTS_COUNT);5657public static void main(String[] args) {58if (HUMON_COUNT == 0) {59System.out.println("Skipped. Heap is too small");60return;61}6263if (TOTAL_MEMORY + REGION_SIZE * HUMON_COUNT > MAX_MEMORY) {64System.out.println("Skipped. Initial heap size is to close to max heap size.");65return;66}6768System.out.format("Running with %s initial heap size of %s maximum heap size. "69+ "Will allocate humongous object of %s size %d times.%n",70MemoryUsagePrinter.humanReadableByteCount(TOTAL_MEMORY, false),71MemoryUsagePrinter.humanReadableByteCount(MAX_MEMORY, false),72MemoryUsagePrinter.humanReadableByteCount(HUMON_SIZE, false),73HUMON_COUNT74);75new TestHumongousShrinkHeap().test();76}7778private final void test() {79System.gc();80MemoryUsagePrinter.printMemoryUsage("init");8182allocate();83MemoryUsagePrinter.printMemoryUsage("allocated");84MemoryUsage muFull = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();8586free();87MemoryUsagePrinter.printMemoryUsage("free");88MemoryUsage muFree = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();8990assertLessThan(muFree.getCommitted(), muFull.getCommitted(), String.format(91"committed free heap size is not less than committed full heap size, heap hasn't been shrunk?%n"92+ "%s = %s%n%s = %s",93MIN_FREE_RATIO_FLAG_NAME,94ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MIN_FREE_RATIO_FLAG_NAME).getValue(),95MAX_FREE_RATIO_FLAG_NAME,96ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MAX_FREE_RATIO_FLAG_NAME).getValue()97));98}99100private void allocate() {101102for (int i = 0; i < LISTS_COUNT; i++) {103List<byte[]> stuff = new ArrayList();104allocateList(stuff, HUMON_COUNT, HUMON_SIZE);105MemoryUsagePrinter.printMemoryUsage("allocate #" + (i+1));106garbage.add(stuff);107}108}109110private void free() {111// do not free last one list112garbage.subList(0, garbage.size() - 1).clear();113114// do not free last one element from last list115List stuff = garbage.get(garbage.size() - 1);116stuff.subList(0, stuff.size() - 1).clear();117System.gc();118}119120private static void allocateList(List garbage, int count, int size) {121for (int i = 0; i < count; i++) {122garbage.add(new byte[size]);123}124}125}126127/**128* Prints memory usage to standard output129*/130class MemoryUsagePrinter {131132public static String humanReadableByteCount(long bytes, boolean si) {133int unit = si ? 1000 : 1024;134if (bytes < unit) {135return bytes + " B";136}137int exp = (int) (Math.log(bytes) / Math.log(unit));138String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");139return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);140}141142public static void printMemoryUsage(String label) {143MemoryUsage memusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();144float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted();145System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n",146label,147humanReadableByteCount(memusage.getInit(), false),148humanReadableByteCount(memusage.getUsed(), false),149humanReadableByteCount(memusage.getCommitted(), false),150freeratio * 100151);152}153}154155156