hotspot/test/gc/g1/TestHumongousShrinkHeap.java
author dfazunen
Mon, 07 Sep 2015 21:30:45 +0400
changeset 32624 42a9450265ae
parent 30604 b8d532cb6420
child 38152 80e5da81fb2c
permissions -rw-r--r--
8132709: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java might fail on embedded Reviewed-by: tschatzl, jwilhelm
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
/*
29678
dd2f3932c21e 8075586: Add @modules as needed to the open hotspot tests
ykantser
parents: 27694
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
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
     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
27692
11c231c781dc 8062537: [TESTBUG] Conflicting GC combinations in hotspot tests
eistepan
parents: 26695
diff changeset
    27
 * @requires vm.gc=="G1" | vm.gc=="null"
27605
a162df7acd89 8064716: TestHumongousShrinkHeap.java can not be run with -XX:+ExplicitGCInvokesConcurrent
azakharov
parents: 26695
diff changeset
    28
 * @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
    29
 * 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
    30
 * @library /testlibrary
29678
dd2f3932c21e 8075586: Add @modules as needed to the open hotspot tests
ykantser
parents: 27694
diff changeset
    31
 * @modules java.management/sun.management
27605
a162df7acd89 8064716: TestHumongousShrinkHeap.java can not be run with -XX:+ExplicitGCInvokesConcurrent
azakharov
parents: 26695
diff changeset
    32
 * @run main/othervm -XX:-ExplicitGCInvokesConcurrent -XX:MinHeapFreeRatio=10
a162df7acd89 8064716: TestHumongousShrinkHeap.java can not be run with -XX:+ExplicitGCInvokesConcurrent
azakharov
parents: 26695
diff changeset
    33
 * -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
    34
 * 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
    35
 */
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
30247
9925b07bba5b 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 29678
diff changeset
    37
import com.sun.management.HotSpotDiagnosticMXBean;
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
    38
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
    39
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
    40
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
    41
import java.util.List;
30604
b8d532cb6420 8067013: Rename the com.oracle.java.testlibary package
ykantser
parents: 30247
diff changeset
    42
import static jdk.test.lib.Asserts.*;
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
    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 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
    45
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
    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
    47
    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
    48
26695
18baf1a5a8ce 8056237: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java fails due to OOM
jwilhelm
parents: 26321
diff changeset
    49
    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
    50
    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
    51
    private static final int LISTS_COUNT = 10;
18baf1a5a8ce 8056237: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java fails due to OOM
jwilhelm
parents: 26321
diff changeset
    52
    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
    53
    private static final long AVAILABLE_MEMORY
18baf1a5a8ce 8056237: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java fails due to OOM
jwilhelm
parents: 26321
diff changeset
    54
                              = Runtime.getRuntime().freeMemory();
18baf1a5a8ce 8056237: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java fails due to OOM
jwilhelm
parents: 26321
diff changeset
    55
    private static final int HUMON_COUNT
18baf1a5a8ce 8056237: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java fails due to OOM
jwilhelm
parents: 26321
diff changeset
    56
                             = (int) ((AVAILABLE_MEMORY / HUMON_SIZE)
18baf1a5a8ce 8056237: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java fails due to OOM
jwilhelm
parents: 26321
diff changeset
    57
            / 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
    58
99374b567304 8037925: CMM Testing: an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    59
99374b567304 8037925: CMM Testing: an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    60
    public static void main(String[] args) {
32624
42a9450265ae 8132709: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java might fail on embedded
dfazunen
parents: 30604
diff changeset
    61
        if (HUMON_COUNT == 0) {
42a9450265ae 8132709: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java might fail on embedded
dfazunen
parents: 30604
diff changeset
    62
            System.out.println("Skipped. Heap is too small");
42a9450265ae 8132709: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java might fail on embedded
dfazunen
parents: 30604
diff changeset
    63
            return;
42a9450265ae 8132709: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java might fail on embedded
dfazunen
parents: 30604
diff changeset
    64
        }
26695
18baf1a5a8ce 8056237: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java fails due to OOM
jwilhelm
parents: 26321
diff changeset
    65
        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
    66
                + "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
    67
                MemoryUsagePrinter.humanReadableByteCount(AVAILABLE_MEMORY, false),
18baf1a5a8ce 8056237: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java fails due to OOM
jwilhelm
parents: 26321
diff changeset
    68
                MemoryUsagePrinter.humanReadableByteCount(HUMON_SIZE, false),
18baf1a5a8ce 8056237: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java fails due to OOM
jwilhelm
parents: 26321
diff changeset
    69
                HUMON_COUNT
18baf1a5a8ce 8056237: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java fails due to OOM
jwilhelm
parents: 26321
diff changeset
    70
        );
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
    71
        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
    72
    }
99374b567304 8037925: CMM Testing: an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    73
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
    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
    75
        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
    76
        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
    77
26695
18baf1a5a8ce 8056237: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java fails due to OOM
jwilhelm
parents: 26321
diff changeset
    78
        allocate();
18baf1a5a8ce 8056237: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java fails due to OOM
jwilhelm
parents: 26321
diff changeset
    79
        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
    80
        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
    81
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
        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
    83
        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
    84
        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
    85
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
        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
    87
                "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
    88
                + "%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
    89
                MIN_FREE_RATIO_FLAG_NAME,
30247
9925b07bba5b 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 29678
diff changeset
    90
                ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class)
9925b07bba5b 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 29678
diff changeset
    91
                    .getVMOption(MIN_FREE_RATIO_FLAG_NAME).getValue(),
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
    92
                MAX_FREE_RATIO_FLAG_NAME,
30247
9925b07bba5b 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 29678
diff changeset
    93
                ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class)
9925b07bba5b 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 29678
diff changeset
    94
                    .getVMOption(MAX_FREE_RATIO_FLAG_NAME).getValue()
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
    95
        ));
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
    }
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
26695
18baf1a5a8ce 8056237: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java fails due to OOM
jwilhelm
parents: 26321
diff changeset
    98
    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
    99
26695
18baf1a5a8ce 8056237: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java fails due to OOM
jwilhelm
parents: 26321
diff changeset
   100
        for (int i = 0; i < LISTS_COUNT; i++) {
18baf1a5a8ce 8056237: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java fails due to OOM
jwilhelm
parents: 26321
diff changeset
   101
            List<byte[]> stuff = new ArrayList();
18baf1a5a8ce 8056237: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java fails due to OOM
jwilhelm
parents: 26321
diff changeset
   102
            allocateList(stuff, HUMON_COUNT, HUMON_SIZE);
18baf1a5a8ce 8056237: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java fails due to OOM
jwilhelm
parents: 26321
diff changeset
   103
            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
   104
            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
   105
        }
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
    }
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
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
    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
   109
        // 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
   110
        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
   111
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
        // 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
   113
        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
   114
        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
   115
        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
   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
26695
18baf1a5a8ce 8056237: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java fails due to OOM
jwilhelm
parents: 26321
diff changeset
   118
    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
   119
        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
   120
            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
   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
    }
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
}
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
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
/**
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
 * 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
   127
 */
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
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
   129
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
    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
   131
        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
   132
        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
   133
            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
   134
        }
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
        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
   136
        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
   137
        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
   138
    }
99374b567304 8037925: CMM Testing: an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   139
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
    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
   141
        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
   142
        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
   143
        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
   144
                label,
26695
18baf1a5a8ce 8056237: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java fails due to OOM
jwilhelm
parents: 26321
diff changeset
   145
                humanReadableByteCount(memusage.getInit(), false),
18baf1a5a8ce 8056237: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java fails due to OOM
jwilhelm
parents: 26321
diff changeset
   146
                humanReadableByteCount(memusage.getUsed(), false),
18baf1a5a8ce 8056237: [TESTBUG] gc/g1/TestHumongousShrinkHeap.java fails due to OOM
jwilhelm
parents: 26321
diff changeset
   147
                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
   148
                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
   149
        );
99374b567304 8037925: CMM Testing: an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   150
    }
99374b567304 8037925: CMM Testing: an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   151
}