Mon, 06 Nov 2017 16:51:47 +0800
[Code Reorganization] remove trailing whitespace to pass jcheck test
jwilhelm@7095 | 1 | /* |
jwilhelm@7095 | 2 | * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
jwilhelm@7095 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jwilhelm@7095 | 4 | * |
jwilhelm@7095 | 5 | * This code is free software; you can redistribute it and/or modify it |
jwilhelm@7095 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jwilhelm@7095 | 7 | * published by the Free Software Foundation. |
jwilhelm@7095 | 8 | * |
jwilhelm@7095 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jwilhelm@7095 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jwilhelm@7095 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jwilhelm@7095 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jwilhelm@7095 | 13 | * accompanied this code). |
jwilhelm@7095 | 14 | * |
jwilhelm@7095 | 15 | * You should have received a copy of the GNU General Public License version |
jwilhelm@7095 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jwilhelm@7095 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jwilhelm@7095 | 18 | * |
jwilhelm@7095 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jwilhelm@7095 | 20 | * or visit www.oracle.com if you need additional information or have any |
jwilhelm@7095 | 21 | * questions. |
jwilhelm@7095 | 22 | */ |
jwilhelm@7095 | 23 | |
jwilhelm@7095 | 24 | /** |
jwilhelm@7095 | 25 | * @test TestHumongousShrinkHeap |
tschatzl@7096 | 26 | * @bug 8036025 8056043 |
jwilhelm@7785 | 27 | * @requires vm.gc=="G1" | vm.gc=="null" |
azakharov@7337 | 28 | * @summary Verify that heap shrinks after GC in the presence of fragmentation |
azakharov@7337 | 29 | * due to humongous objects |
jwilhelm@7095 | 30 | * @library /testlibrary |
azakharov@7337 | 31 | * @run main/othervm -XX:-ExplicitGCInvokesConcurrent -XX:MinHeapFreeRatio=10 |
azakharov@7337 | 32 | * -XX:MaxHeapFreeRatio=12 -XX:+UseG1GC -XX:G1HeapRegionSize=1M -verbose:gc |
azakharov@7337 | 33 | * TestHumongousShrinkHeap |
jwilhelm@7095 | 34 | */ |
jwilhelm@7095 | 35 | |
jwilhelm@7095 | 36 | import java.lang.management.ManagementFactory; |
jwilhelm@7095 | 37 | import java.lang.management.MemoryUsage; |
jwilhelm@7095 | 38 | import java.util.ArrayList; |
jwilhelm@7095 | 39 | import java.util.List; |
jwilhelm@7095 | 40 | import sun.management.ManagementFactoryHelper; |
jwilhelm@7095 | 41 | import static com.oracle.java.testlibrary.Asserts.*; |
jwilhelm@7095 | 42 | |
jwilhelm@7095 | 43 | public class TestHumongousShrinkHeap { |
jwilhelm@7095 | 44 | |
jwilhelm@7095 | 45 | public static final String MIN_FREE_RATIO_FLAG_NAME = "MinHeapFreeRatio"; |
jwilhelm@7095 | 46 | public static final String MAX_FREE_RATIO_FLAG_NAME = "MaxHeapFreeRatio"; |
jwilhelm@7095 | 47 | |
jwilhelm@7175 | 48 | private static final List<List<byte[]>> garbage = new ArrayList(); |
jwilhelm@7175 | 49 | private static final int REGION_SIZE = 1024 * 1024; // 1M |
jwilhelm@7175 | 50 | private static final int LISTS_COUNT = 10; |
jwilhelm@7175 | 51 | private static final int HUMON_SIZE = Math.round(.9f * REGION_SIZE); |
jwilhelm@7175 | 52 | private static final long AVAILABLE_MEMORY |
jwilhelm@7175 | 53 | = Runtime.getRuntime().freeMemory(); |
jwilhelm@7175 | 54 | private static final int HUMON_COUNT |
jwilhelm@7175 | 55 | = (int) ((AVAILABLE_MEMORY / HUMON_SIZE) |
jwilhelm@7175 | 56 | / LISTS_COUNT); |
jwilhelm@7095 | 57 | |
jwilhelm@7095 | 58 | |
jwilhelm@7095 | 59 | public static void main(String[] args) { |
jwilhelm@7175 | 60 | System.out.format("Running with %s max heap size. " |
jwilhelm@7175 | 61 | + "Will allocate humongous object of %s size %d times.%n", |
jwilhelm@7175 | 62 | MemoryUsagePrinter.humanReadableByteCount(AVAILABLE_MEMORY, false), |
jwilhelm@7175 | 63 | MemoryUsagePrinter.humanReadableByteCount(HUMON_SIZE, false), |
jwilhelm@7175 | 64 | HUMON_COUNT |
jwilhelm@7175 | 65 | ); |
jwilhelm@7095 | 66 | new TestHumongousShrinkHeap().test(); |
jwilhelm@7095 | 67 | } |
jwilhelm@7095 | 68 | |
jwilhelm@7095 | 69 | private final void test() { |
jwilhelm@7095 | 70 | System.gc(); |
jwilhelm@7095 | 71 | MemoryUsagePrinter.printMemoryUsage("init"); |
jwilhelm@7095 | 72 | |
jwilhelm@7175 | 73 | allocate(); |
jwilhelm@7175 | 74 | MemoryUsagePrinter.printMemoryUsage("allocated"); |
jwilhelm@7095 | 75 | MemoryUsage muFull = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); |
jwilhelm@7095 | 76 | |
jwilhelm@7095 | 77 | free(); |
jwilhelm@7095 | 78 | MemoryUsagePrinter.printMemoryUsage("free"); |
jwilhelm@7095 | 79 | MemoryUsage muFree = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); |
jwilhelm@7095 | 80 | |
jwilhelm@7095 | 81 | assertLessThan(muFree.getCommitted(), muFull.getCommitted(), String.format( |
jwilhelm@7095 | 82 | "committed free heap size is not less than committed full heap size, heap hasn't been shrunk?%n" |
jwilhelm@7095 | 83 | + "%s = %s%n%s = %s", |
jwilhelm@7095 | 84 | MIN_FREE_RATIO_FLAG_NAME, |
jwilhelm@7095 | 85 | ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MIN_FREE_RATIO_FLAG_NAME).getValue(), |
jwilhelm@7095 | 86 | MAX_FREE_RATIO_FLAG_NAME, |
jwilhelm@7095 | 87 | ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MAX_FREE_RATIO_FLAG_NAME).getValue() |
jwilhelm@7095 | 88 | )); |
jwilhelm@7095 | 89 | } |
jwilhelm@7095 | 90 | |
jwilhelm@7175 | 91 | private void allocate() { |
jwilhelm@7095 | 92 | |
jwilhelm@7175 | 93 | for (int i = 0; i < LISTS_COUNT; i++) { |
jwilhelm@7175 | 94 | List<byte[]> stuff = new ArrayList(); |
jwilhelm@7175 | 95 | allocateList(stuff, HUMON_COUNT, HUMON_SIZE); |
jwilhelm@7175 | 96 | MemoryUsagePrinter.printMemoryUsage("allocate #" + (i+1)); |
jwilhelm@7095 | 97 | garbage.add(stuff); |
jwilhelm@7095 | 98 | } |
jwilhelm@7095 | 99 | } |
jwilhelm@7095 | 100 | |
jwilhelm@7095 | 101 | private void free() { |
jwilhelm@7095 | 102 | // do not free last one list |
jwilhelm@7095 | 103 | garbage.subList(0, garbage.size() - 1).clear(); |
jwilhelm@7095 | 104 | |
jwilhelm@7095 | 105 | // do not free last one element from last list |
jwilhelm@7175 | 106 | List stuff = garbage.get(garbage.size() - 1); |
jwilhelm@7095 | 107 | stuff.subList(0, stuff.size() - 1).clear(); |
jwilhelm@7095 | 108 | System.gc(); |
jwilhelm@7095 | 109 | } |
jwilhelm@7095 | 110 | |
jwilhelm@7175 | 111 | private static void allocateList(List garbage, int count, int size) { |
jwilhelm@7095 | 112 | for (int i = 0; i < count; i++) { |
jwilhelm@7095 | 113 | garbage.add(new byte[size]); |
jwilhelm@7095 | 114 | } |
jwilhelm@7095 | 115 | } |
jwilhelm@7095 | 116 | } |
jwilhelm@7095 | 117 | |
jwilhelm@7095 | 118 | /** |
jwilhelm@7095 | 119 | * Prints memory usage to standard output |
jwilhelm@7095 | 120 | */ |
jwilhelm@7095 | 121 | class MemoryUsagePrinter { |
jwilhelm@7095 | 122 | |
jwilhelm@7095 | 123 | public static String humanReadableByteCount(long bytes, boolean si) { |
jwilhelm@7095 | 124 | int unit = si ? 1000 : 1024; |
jwilhelm@7095 | 125 | if (bytes < unit) { |
jwilhelm@7095 | 126 | return bytes + " B"; |
jwilhelm@7095 | 127 | } |
jwilhelm@7095 | 128 | int exp = (int) (Math.log(bytes) / Math.log(unit)); |
jwilhelm@7095 | 129 | String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i"); |
jwilhelm@7095 | 130 | return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre); |
jwilhelm@7095 | 131 | } |
jwilhelm@7095 | 132 | |
jwilhelm@7095 | 133 | public static void printMemoryUsage(String label) { |
jwilhelm@7095 | 134 | MemoryUsage memusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); |
jwilhelm@7095 | 135 | float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted(); |
jwilhelm@7095 | 136 | System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n", |
jwilhelm@7095 | 137 | label, |
jwilhelm@7175 | 138 | humanReadableByteCount(memusage.getInit(), false), |
jwilhelm@7175 | 139 | humanReadableByteCount(memusage.getUsed(), false), |
jwilhelm@7175 | 140 | humanReadableByteCount(memusage.getCommitted(), false), |
jwilhelm@7095 | 141 | freeratio * 100 |
jwilhelm@7095 | 142 | ); |
jwilhelm@7095 | 143 | } |
jwilhelm@7095 | 144 | } |