jdk/test/java/lang/management/MemoryMXBean/MemoryUtil.java
author mtobiass
Fri, 04 Apr 2014 13:01:26 +0200
changeset 23721 83da5b498504
parent 5506 202f599c92aa
permissions -rw-r--r--
8038822: java/lang/management/MemoryMXBean/LowMemoryTest2.sh still fails with OutOfMemoryError: Metaspace Summary: Force a GC when usage above threshold. Add more logging. Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug     4982289
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Utility class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @author Mandy Chung
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class MemoryUtil {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    private static String INDENT = "    ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private static String formatSize(String name, long value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        StringBuffer buf = new StringBuffer(name + " = " + value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        if (value > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
            buf.append(" (" + (value >> 10) + "K)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public static void printMemoryUsage(MemoryUsage usage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        System.out.println(INDENT + formatSize("Initial size  ", usage.getInit()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        System.out.println(INDENT + formatSize("Used size     ", usage.getUsed()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        System.out.println(INDENT + formatSize("Committd size ", usage.getCommitted()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        System.out.println(INDENT + formatSize("Max size      ", usage.getMax()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public static void printMemoryPool(MemoryPoolMXBean pool) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        System.out.println(INDENT + "Memory Pool name: " + pool.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        System.out.println(INDENT + "Type: " + pool.getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        System.out.println(INDENT + "Memory Usage: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            pool.getUsage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        System.out.println(INDENT + "Threshold: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            (pool.isUsageThresholdSupported() ? pool.getUsageThreshold() : -1));
23721
83da5b498504 8038822: java/lang/management/MemoryMXBean/LowMemoryTest2.sh still fails with OutOfMemoryError: Metaspace
mtobiass
parents: 5506
diff changeset
    57
        System.out.println(INDENT + "ThresholdCount: " +
83da5b498504 8038822: java/lang/management/MemoryMXBean/LowMemoryTest2.sh still fails with OutOfMemoryError: Metaspace
mtobiass
parents: 5506
diff changeset
    58
            (pool.isUsageThresholdSupported() ? pool.getUsageThresholdCount() : -1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        System.out.print(INDENT + "Manager = [");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        String[] mgrs = pool.getMemoryManagerNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        for (int i = 0; i < mgrs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            System.out.print(mgrs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            if (i < (mgrs.length - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                System.out.print(" | ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        System.out.println("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public static void printMemoryPools(List pools) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        ListIterator iter = pools.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        System.out.println(INDENT + "Number of memory pools = " + pools.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            printMemoryPool(pool);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public static void printMemoryManager(MemoryManagerMXBean mgr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (mgr instanceof GarbageCollectorMXBean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            GarbageCollectorMXBean gc = (GarbageCollectorMXBean) mgr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            System.out.println(INDENT + "Garbage Collector name: " + gc.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            System.out.println(INDENT + "GC Count: " + gc.getCollectionCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            System.out.println(INDENT + "GC Time : " + gc.getCollectionTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            System.out.println(INDENT + "Memory Manager name: " + mgr.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        System.out.print("Pool = [");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        String[] pools = mgr.getMemoryPoolNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        for (int i = 0; i < pools.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            System.out.print(INDENT + pools[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            if (i < (pools.length - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                System.out.print(" | ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        System.out.println("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static void printMemoryManagers(List managers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        ListIterator iter = managers.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        System.out.println(INDENT + "Number of memory managers = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            managers.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            MemoryManagerMXBean mgr = (MemoryManagerMXBean) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            printMemoryManager(mgr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public static void printMemoryNotificationInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        (MemoryNotificationInfo minfo, String type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        System.out.print("Notification for " + minfo.getPoolName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        System.out.print(" [type = " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        System.out.println(" count = " + minfo.getCount() + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        System.out.println(INDENT + "usage = " + minfo.getUsage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
}