test/hotspot/jtreg/gc/g1/TestPeriodicCollection.java
author tschatzl
Mon, 10 Dec 2018 10:25:27 +0100
changeset 52918 f94c7929a44b
child 53523 4c5184c56dc2
permissions -rw-r--r--
8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1 Summary: Issue optional, default enabled, concurrent cycles when the VM is idle to reclaim unused internal and Java heap memory. Reviewed-by: sjohanss, sangheki Contributed-by: Rodrigo Bruno <rbruno@gsd.inesc-id.pt>, Ruslan Synytsky <rs@jelastic.com>, Thomas Schatzl <thomas.schatzl@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52918
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
     1
/*
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
     4
 *
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
     8
 *
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    13
 * accompanied this code).
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    14
 *
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    18
 *
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    21
 * questions.
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    22
 */
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    23
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    24
/**
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    25
 * @test TestPeriodicCollection
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    26
 * @requires vm.gc.G1
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    27
 * @requires vm.compMode != "Xcomp"
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    28
 * @summary Verify that heap shrinks when the application is idle.
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    29
 * @library /test/lib /
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    30
 * @modules java.base/jdk.internal.misc
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    31
 * @modules java.management/sun.management
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    32
 * @run main/othervm -XX:MaxNewSize=32M -XX:InitialHeapSize=48M -Xmx128M -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=25 -XX:+UseG1GC -XX:G1PeriodicGCInterval=3000 -XX:+G1PeriodicGCInvokesConcurrent -Xlog:gc,gc+periodic=debug,gc+ergo+heap=debug TestPeriodicCollection
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    33
 * @run main/othervm -XX:MaxNewSize=32M -XX:InitialHeapSize=48M -Xmx128M -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=25 -XX:+UseG1GC -XX:G1PeriodicGCInterval=3000 -XX:-G1PeriodicGCInvokesConcurrent -Xlog:gc,gc+periodic=debug,gc+ergo+heap=debug TestPeriodicCollection
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    34
 */
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    35
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    36
import com.sun.management.HotSpotDiagnosticMXBean;
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    37
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    38
import gc.testlibrary.Helpers;
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    39
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    40
import java.lang.management.GarbageCollectorMXBean;
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    41
import java.lang.management.ManagementFactory;
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    42
import java.lang.management.MemoryUsage;
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    43
import java.text.NumberFormat;
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    44
import static jdk.test.lib.Asserts.*;
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    45
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    46
public class TestPeriodicCollection {
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    47
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    48
    public static final String MIN_FREE_RATIO_FLAG_NAME = "MinHeapFreeRatio";
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    49
    public static final String MAX_FREE_RATIO_FLAG_NAME = "MaxHeapFreeRatio";
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    50
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    51
    private static final int IDLE_TIME = 7 * 1000;
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    52
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    53
    private static boolean gcOccurred() {
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    54
        for (GarbageCollectorMXBean b : ManagementFactory.getGarbageCollectorMXBeans()) {
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    55
            if (b.getCollectionCount() != 0) {
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    56
                return true;
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    57
            }
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    58
        }
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    59
        return false;
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    60
    }
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    61
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    62
    public static void main(String[] args) {
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    63
        MemoryUsage muInitial = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    64
        MemoryUsagePrinter.printMemoryUsage("initial", muInitial);
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    65
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    66
        if (gcOccurred()) {
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    67
          System.out.println("At least one garbage collection occurred. Exiting as this may have already shrunk the heap.");
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    68
          return;
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    69
        }
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    70
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    71
        try {
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    72
            Thread.sleep(IDLE_TIME);
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    73
        } catch (InterruptedException ie) {
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    74
            System.err.println("Skipped. Failed to wait for idle collection");
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    75
        }
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    76
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    77
        MemoryUsage muAfter = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    78
        MemoryUsagePrinter.printMemoryUsage("after", muAfter);
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    79
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    80
        assertLessThan(muAfter.getCommitted(), muInitial.getCommitted(), String.format(
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    81
                "committed free heap size is not less than committed full heap size, heap hasn't been shrunk?%n"
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    82
                + "%s = %s%n%s = %s",
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    83
                MIN_FREE_RATIO_FLAG_NAME,
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    84
                ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class)
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    85
                    .getVMOption(MIN_FREE_RATIO_FLAG_NAME).getValue(),
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    86
                MAX_FREE_RATIO_FLAG_NAME,
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    87
                ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class)
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    88
                    .getVMOption(MAX_FREE_RATIO_FLAG_NAME).getValue()
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    89
        ));
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    90
    }
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    91
}
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    92
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    93
/**
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    94
 * Prints memory usage to standard output
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    95
 */
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    96
class MemoryUsagePrinter {
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    97
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    98
    public static final NumberFormat NF = Helpers.numberFormatter();
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
    99
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
   100
    public static void printMemoryUsage(String label, MemoryUsage memusage) {
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
   101
        float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted();
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
   102
        System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n",
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
   103
                label,
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
   104
                NF.format(memusage.getInit()),
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
   105
                NF.format(memusage.getUsed()),
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
   106
                NF.format(memusage.getCommitted()),
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
   107
                freeratio * 100
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
   108
        );
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
   109
    }
f94c7929a44b 8212657: Implementation of JDK-8204089 Promptly Return Unused Committed Memory from G1
tschatzl
parents:
diff changeset
   110
}