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