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