hotspot/test/gc/g1/TestShrinkDefragmentedHeap.java
author thartmann
Fri, 12 Dec 2014 09:37:38 +0100
changeset 28190 5a6b07edeb21
parent 27605 a162df7acd89
child 29678 dd2f3932c21e
permissions -rw-r--r--
8066433: Move Whitebox test library to top level repository Summary: Moved Whitebox test library to top level repository. Reviewed-by: iignatyev, sla, mr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26694
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
     1
/*
37ba4a5a3f9c 8041946: CMM Testing: 8u40 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.
37ba4a5a3f9c 8041946: CMM Testing: 8u40 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.
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
     4
 *
37ba4a5a3f9c 8041946: CMM Testing: 8u40 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
37ba4a5a3f9c 8041946: CMM Testing: 8u40 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
37ba4a5a3f9c 8041946: CMM Testing: 8u40 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.
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
     8
 *
37ba4a5a3f9c 8041946: CMM Testing: 8u40 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
37ba4a5a3f9c 8041946: CMM Testing: 8u40 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
37ba4a5a3f9c 8041946: CMM Testing: 8u40 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
37ba4a5a3f9c 8041946: CMM Testing: 8u40 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
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    13
 * accompanied this code).
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    14
 *
37ba4a5a3f9c 8041946: CMM Testing: 8u40 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
37ba4a5a3f9c 8041946: CMM Testing: 8u40 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,
37ba4a5a3f9c 8041946: CMM Testing: 8u40 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.
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    18
 *
37ba4a5a3f9c 8041946: CMM Testing: 8u40 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
37ba4a5a3f9c 8041946: CMM Testing: 8u40 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
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    21
 * questions.
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    22
 */
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    23
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    24
/**
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    25
 * @test TestShrinkDefragmentedHeap
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    26
 * @bug 8038423
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    27
 * @summary Verify that heap shrinks after GC in the presence of fragmentation due to humongous objects
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    28
 *     1. allocate small objects mixed with humongous ones
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    29
 *        "ssssHssssHssssHssssHssssHssssHssssH"
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    30
 *     2. release all allocated object except the last humongous one
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    31
 *        "..................................H"
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    32
 *     3. invoke gc and check that memory returned to the system (amount of committed memory got down)
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    33
 *
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    34
 * @library /testlibrary
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    35
 */
37ba4a5a3f9c 8041946: CMM Testing: 8u40 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.ManagementFactory;
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    37
import java.lang.management.MemoryUsage;
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    38
import java.util.ArrayList;
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    39
import java.util.List;
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    40
import sun.management.ManagementFactoryHelper;
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    41
import static com.oracle.java.testlibrary.Asserts.*;
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    42
import com.oracle.java.testlibrary.ProcessTools;
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    43
import com.oracle.java.testlibrary.OutputAnalyzer;
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    44
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    45
public class TestShrinkDefragmentedHeap {
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    46
    // Since we store all the small objects, they become old and old regions are also allocated at the bottom of the heap
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    47
    // together with humongous regions. So if there are a lot of old regions in the lower part of the heap,
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    48
    // the humongous regions will be allocated in the upper part of the heap anyway.
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    49
    // To avoid this the Eden needs to be big enough to fit all the small objects.
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    50
    private static final int INITIAL_HEAP_SIZE  = 200 * 1024 * 1024;
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    51
    private static final int MINIMAL_YOUNG_SIZE = 190 * 1024 * 1024;
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    52
    private static final int REGION_SIZE        = 1 * 1024 * 1024;
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    53
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    54
    public static void main(String[] args) throws Exception, Throwable {
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    55
        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    56
                "-XX:InitialHeapSize=" + INITIAL_HEAP_SIZE,
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    57
                "-Xmn" + MINIMAL_YOUNG_SIZE,
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    58
                "-XX:MinHeapFreeRatio=10",
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    59
                "-XX:MaxHeapFreeRatio=11",
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    60
                "-XX:+UseG1GC",
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    61
                "-XX:G1HeapRegionSize=" + REGION_SIZE,
27605
a162df7acd89 8064716: TestHumongousShrinkHeap.java can not be run with -XX:+ExplicitGCInvokesConcurrent
azakharov
parents: 26694
diff changeset
    62
                "-XX:-ExplicitGCInvokesConcurrent",
26694
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    63
                "-verbose:gc",
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    64
                GCTest.class.getName()
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    65
        );
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    66
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    67
        OutputAnalyzer output = ProcessTools.executeProcess(pb);
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    68
        output.shouldHaveExitValue(0);
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    69
    }
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    70
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    71
    static class GCTest {
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    72
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    73
        private static final String MIN_FREE_RATIO_FLAG_NAME = "MinHeapFreeRatio";
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    74
        private static final String MAX_FREE_RATIO_FLAG_NAME = "MaxHeapFreeRatio";
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    75
        private static final String NEW_SIZE_FLAG_NAME = "NewSize";
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    76
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    77
        private static final ArrayList<ArrayList<byte[]>> garbage = new ArrayList<>();
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    78
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    79
        private static final int SMALL_OBJS_SIZE  = 10 * 1024; // 10kB
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    80
        private static final int SMALL_OBJS_COUNT = MINIMAL_YOUNG_SIZE / (SMALL_OBJS_SIZE-1);
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    81
        private static final int ALLOCATE_COUNT = 3;
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    82
        // try to put all humongous object into gap between min young size and initial heap size
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    83
        // to avoid implicit GCs
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    84
        private static final int HUMONG_OBJS_SIZE = (int) Math.max(
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    85
                (INITIAL_HEAP_SIZE - MINIMAL_YOUNG_SIZE) / ALLOCATE_COUNT / 4,
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    86
                REGION_SIZE * 1.1
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    87
        );
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    88
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    89
        private static final long initialHeapSize = getHeapMemoryUsage().getUsed();
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    90
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    91
        public static void main(String[] args) throws InterruptedException {
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    92
            new GCTest().test();
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    93
        }
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    94
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    95
        private void test() throws InterruptedException {
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    96
            MemoryUsagePrinter.printMemoryUsage("init");
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    97
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    98
            allocate();
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
    99
            System.gc();
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   100
            MemoryUsage muFull = getHeapMemoryUsage();
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   101
            MemoryUsagePrinter.printMemoryUsage("allocated");
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   102
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   103
            free();
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   104
            //Thread.sleep(1000); // sleep before measures due lags in JMX
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   105
            MemoryUsage muFree = getHeapMemoryUsage();
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   106
            MemoryUsagePrinter.printMemoryUsage("free");
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   107
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   108
            assertLessThan(muFree.getCommitted(), muFull.getCommitted(), prepareMessageCommittedIsNotLess() );
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   109
        }
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   110
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   111
        private void allocate() {
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   112
            System.out.format("Will allocate objects of small size = %s and humongous size = %s",
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   113
                    MemoryUsagePrinter.humanReadableByteCount(SMALL_OBJS_SIZE, false),
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   114
                    MemoryUsagePrinter.humanReadableByteCount(HUMONG_OBJS_SIZE, false)
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   115
            );
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   116
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   117
            for (int i = 0; i < ALLOCATE_COUNT; i++) {
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   118
                ArrayList<byte[]> stuff = new ArrayList<>();
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   119
                allocateList(stuff, SMALL_OBJS_COUNT / ALLOCATE_COUNT, SMALL_OBJS_SIZE);
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   120
                garbage.add(stuff);
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   121
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   122
                ArrayList<byte[]> humongousStuff = new ArrayList<>();
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   123
                allocateList(humongousStuff, 4, HUMONG_OBJS_SIZE);
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   124
                garbage.add(humongousStuff);
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   125
            }
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   126
        }
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   127
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   128
        private void free() {
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   129
            // do not free last one list
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   130
            garbage.subList(0, garbage.size() - 1).clear();
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   131
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   132
            // do not free last one element from last list
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   133
            ArrayList stuff = garbage.get(garbage.size() - 1);
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   134
            if (stuff.size() > 1) {
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   135
                stuff.subList(0, stuff.size() - 1).clear();
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   136
            }
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   137
            System.gc();
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   138
        }
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   139
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   140
        private String prepareMessageCommittedIsNotLess() {
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   141
            return String.format(
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   142
                    "committed free heap size is not less than committed full heap size, heap hasn't been shrunk?%n"
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   143
                    + "%s = %s%n%s = %s",
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   144
                    MIN_FREE_RATIO_FLAG_NAME,
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   145
                    ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MIN_FREE_RATIO_FLAG_NAME).getValue(),
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   146
                    MAX_FREE_RATIO_FLAG_NAME,
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   147
                    ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MAX_FREE_RATIO_FLAG_NAME).getValue()
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   148
            );
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   149
        }
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   150
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   151
        private static void allocateList(List garbage, int count, int size) {
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   152
            for (int i = 0; i < count; i++) {
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   153
                garbage.add(new byte[size]);
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   154
            }
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   155
        }
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   156
    }
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   157
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   158
    static MemoryUsage getHeapMemoryUsage() {
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   159
        return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   160
    }
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   161
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   162
    /**
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   163
     * Prints memory usage to standard output
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   164
     */
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   165
    static class MemoryUsagePrinter {
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   166
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   167
        public static String humanReadableByteCount(long bytes, boolean si) {
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   168
            int unit = si ? 1000 : 1024;
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   169
            if (bytes < unit) {
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   170
                return bytes + " B";
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   171
            }
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   172
            int exp = (int) (Math.log(bytes) / Math.log(unit));
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   173
            String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   174
            return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   175
        }
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   176
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   177
        public static void printMemoryUsage(String label) {
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   178
            MemoryUsage memusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   179
            float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted();
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   180
            System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n",
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   181
                    label,
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   182
                    humanReadableByteCount(memusage.getInit(), false),
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   183
                    humanReadableByteCount(memusage.getUsed(), false),
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   184
                    humanReadableByteCount(memusage.getCommitted(), false),
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   185
                    freeratio * 100
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   186
            );
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   187
        }
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   188
    }
37ba4a5a3f9c 8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
jwilhelm
parents:
diff changeset
   189
}