TestHumongousShrinkHeap.java 5.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/**
 * @test TestHumongousShrinkHeap
26
 * @bug 8036025 8056043
27 28
 * @summary Verify that heap shrinks after GC in the presence of fragmentation
 * due to humongous objects
29
 * @library /testlibrary
30 31 32
 * @run main/othervm -XX:-ExplicitGCInvokesConcurrent -XX:MinHeapFreeRatio=10
 * -XX:MaxHeapFreeRatio=12 -XX:+UseG1GC -XX:G1HeapRegionSize=1M -verbose:gc
 * TestHumongousShrinkHeap
33 34 35 36 37 38 39 40 41 42 43 44 45 46
 */

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryUsage;
import java.util.ArrayList;
import java.util.List;
import sun.management.ManagementFactoryHelper;
import static com.oracle.java.testlibrary.Asserts.*;

public class TestHumongousShrinkHeap {

    public static final String MIN_FREE_RATIO_FLAG_NAME = "MinHeapFreeRatio";
    public static final String MAX_FREE_RATIO_FLAG_NAME = "MaxHeapFreeRatio";

47 48 49 50 51 52 53 54 55
    private static final List<List<byte[]>> garbage = new ArrayList();
    private static final int REGION_SIZE = 1024 * 1024; // 1M
    private static final int LISTS_COUNT = 10;
    private static final int HUMON_SIZE = Math.round(.9f * REGION_SIZE);
    private static final long AVAILABLE_MEMORY
                              = Runtime.getRuntime().freeMemory();
    private static final int HUMON_COUNT
                             = (int) ((AVAILABLE_MEMORY / HUMON_SIZE)
            / LISTS_COUNT);
56 57 58


    public static void main(String[] args) {
59 60 61 62 63 64
        System.out.format("Running with %s max heap size. "
                + "Will allocate humongous object of %s size %d times.%n",
                MemoryUsagePrinter.humanReadableByteCount(AVAILABLE_MEMORY, false),
                MemoryUsagePrinter.humanReadableByteCount(HUMON_SIZE, false),
                HUMON_COUNT
        );
65 66 67 68 69 70 71
        new TestHumongousShrinkHeap().test();
    }

    private final void test() {
        System.gc();
        MemoryUsagePrinter.printMemoryUsage("init");

72 73
        allocate();
        MemoryUsagePrinter.printMemoryUsage("allocated");
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
        MemoryUsage muFull = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();

        free();
        MemoryUsagePrinter.printMemoryUsage("free");
        MemoryUsage muFree = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();

        assertLessThan(muFree.getCommitted(), muFull.getCommitted(), String.format(
                "committed free heap size is not less than committed full heap size, heap hasn't been shrunk?%n"
                + "%s = %s%n%s = %s",
                MIN_FREE_RATIO_FLAG_NAME,
                ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MIN_FREE_RATIO_FLAG_NAME).getValue(),
                MAX_FREE_RATIO_FLAG_NAME,
                ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MAX_FREE_RATIO_FLAG_NAME).getValue()
        ));
    }

90
    private void allocate() {
91

92 93 94 95
        for (int i = 0; i < LISTS_COUNT; i++) {
            List<byte[]> stuff = new ArrayList();
            allocateList(stuff, HUMON_COUNT, HUMON_SIZE);
            MemoryUsagePrinter.printMemoryUsage("allocate #" + (i+1));
96 97 98 99 100 101 102 103 104
            garbage.add(stuff);
        }
    }

    private void free() {
        // do not free last one list
        garbage.subList(0, garbage.size() - 1).clear();

        // do not free last one element from last list
105
        List stuff = garbage.get(garbage.size() - 1);
106 107 108 109
        stuff.subList(0, stuff.size() - 1).clear();
        System.gc();
    }

110
    private static void allocateList(List garbage, int count, int size) {
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
        for (int i = 0; i < count; i++) {
            garbage.add(new byte[size]);
        }
    }
}

/**
 * Prints memory usage to standard output
 */
class MemoryUsagePrinter {

    public static String humanReadableByteCount(long bytes, boolean si) {
        int unit = si ? 1000 : 1024;
        if (bytes < unit) {
            return bytes + " B";
        }
        int exp = (int) (Math.log(bytes) / Math.log(unit));
        String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
        return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
    }

    public static void printMemoryUsage(String label) {
        MemoryUsage memusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
        float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted();
        System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n",
                label,
137 138 139
                humanReadableByteCount(memusage.getInit(), false),
                humanReadableByteCount(memusage.getUsed(), false),
                humanReadableByteCount(memusage.getCommitted(), false),
140 141 142 143
                freeratio * 100
        );
    }
}