jdk/test/java/lang/management/MemoryMXBean/MemoryUtil.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2004 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        System.out.print(INDENT + "Manager = [");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        String[] mgrs = pool.getMemoryManagerNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        for (int i = 0; i < mgrs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            System.out.print(mgrs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            if (i < (mgrs.length - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                System.out.print(" | ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        System.out.println("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static void printMemoryPools(List pools) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        ListIterator iter = pools.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        System.out.println(INDENT + "Number of memory pools = " + pools.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            printMemoryPool(pool);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public static void printMemoryManager(MemoryManagerMXBean mgr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (mgr instanceof GarbageCollectorMXBean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            GarbageCollectorMXBean gc = (GarbageCollectorMXBean) mgr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            System.out.println(INDENT + "Garbage Collector name: " + gc.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            System.out.println(INDENT + "GC Count: " + gc.getCollectionCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            System.out.println(INDENT + "GC Time : " + gc.getCollectionTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            System.out.println(INDENT + "Memory Manager name: " + mgr.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        System.out.print("Pool = [");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        String[] pools = mgr.getMemoryPoolNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        for (int i = 0; i < pools.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            System.out.print(INDENT + pools[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if (i < (pools.length - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                System.out.print(" | ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        System.out.println("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public static void printMemoryManagers(List managers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        ListIterator iter = managers.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        System.out.println(INDENT + "Number of memory managers = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            managers.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            MemoryManagerMXBean mgr = (MemoryManagerMXBean) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            printMemoryManager(mgr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public static void printMemoryNotificationInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        (MemoryNotificationInfo minfo, String type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        System.out.print("Notification for " + minfo.getPoolName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        System.out.print(" [type = " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        System.out.println(" count = " + minfo.getCount() + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        System.out.println(INDENT + "usage = " + minfo.getUsage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
}