Path: blob/jdk8u272-b10-aarch32-20201026/hotspot/test/gc/g1/TestHumongousShrinkHeap.java
48799 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);51private static final long AVAILABLE_MEMORY52= Runtime.getRuntime().freeMemory();53private static final int HUMON_COUNT54= (int) ((AVAILABLE_MEMORY / HUMON_SIZE)55/ LISTS_COUNT);565758public static void main(String[] args) {59System.out.format("Running with %s max heap size. "60+ "Will allocate humongous object of %s size %d times.%n",61MemoryUsagePrinter.humanReadableByteCount(AVAILABLE_MEMORY, false),62MemoryUsagePrinter.humanReadableByteCount(HUMON_SIZE, false),63HUMON_COUNT64);65new TestHumongousShrinkHeap().test();66}6768private final void test() {69System.gc();70MemoryUsagePrinter.printMemoryUsage("init");7172allocate();73MemoryUsagePrinter.printMemoryUsage("allocated");74MemoryUsage muFull = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();7576free();77MemoryUsagePrinter.printMemoryUsage("free");78MemoryUsage muFree = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();7980assertLessThan(muFree.getCommitted(), muFull.getCommitted(), String.format(81"committed free heap size is not less than committed full heap size, heap hasn't been shrunk?%n"82+ "%s = %s%n%s = %s",83MIN_FREE_RATIO_FLAG_NAME,84ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MIN_FREE_RATIO_FLAG_NAME).getValue(),85MAX_FREE_RATIO_FLAG_NAME,86ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MAX_FREE_RATIO_FLAG_NAME).getValue()87));88}8990private void allocate() {9192for (int i = 0; i < LISTS_COUNT; i++) {93List<byte[]> stuff = new ArrayList();94allocateList(stuff, HUMON_COUNT, HUMON_SIZE);95MemoryUsagePrinter.printMemoryUsage("allocate #" + (i+1));96garbage.add(stuff);97}98}99100private void free() {101// do not free last one list102garbage.subList(0, garbage.size() - 1).clear();103104// do not free last one element from last list105List stuff = garbage.get(garbage.size() - 1);106stuff.subList(0, stuff.size() - 1).clear();107System.gc();108}109110private static void allocateList(List garbage, int count, int size) {111for (int i = 0; i < count; i++) {112garbage.add(new byte[size]);113}114}115}116117/**118* Prints memory usage to standard output119*/120class MemoryUsagePrinter {121122public static String humanReadableByteCount(long bytes, boolean si) {123int unit = si ? 1000 : 1024;124if (bytes < unit) {125return bytes + " B";126}127int exp = (int) (Math.log(bytes) / Math.log(unit));128String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");129return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);130}131132public static void printMemoryUsage(String label) {133MemoryUsage memusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();134float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted();135System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n",136label,137humanReadableByteCount(memusage.getInit(), false),138humanReadableByteCount(memusage.getUsed(), false),139humanReadableByteCount(memusage.getCommitted(), false),140freeratio * 100141);142}143}144145146