2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @bug 4982289
|
|
26 |
* @summary Utility class.
|
|
27 |
* @author Mandy Chung
|
|
28 |
*/
|
|
29 |
|
|
30 |
import java.lang.management.*;
|
|
31 |
import java.util.*;
|
|
32 |
|
|
33 |
public 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.print(INDENT + "Manager = [");
|
|
58 |
String[] mgrs = pool.getMemoryManagerNames();
|
|
59 |
for (int i = 0; i < mgrs.length; i++) {
|
|
60 |
System.out.print(mgrs[i]);
|
|
61 |
if (i < (mgrs.length - 1)) {
|
|
62 |
System.out.print(" | ");
|
|
63 |
}
|
|
64 |
}
|
|
65 |
System.out.println("]");
|
|
66 |
}
|
|
67 |
|
|
68 |
public static void printMemoryPools(List pools) {
|
|
69 |
ListIterator iter = pools.listIterator();
|
|
70 |
System.out.println(INDENT + "Number of memory pools = " + pools.size());
|
|
71 |
while (iter.hasNext()) {
|
|
72 |
MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next();
|
|
73 |
printMemoryPool(pool);
|
|
74 |
}
|
|
75 |
}
|
|
76 |
|
|
77 |
public static void printMemoryManager(MemoryManagerMXBean mgr) {
|
|
78 |
if (mgr instanceof GarbageCollectorMXBean) {
|
|
79 |
GarbageCollectorMXBean gc = (GarbageCollectorMXBean) mgr;
|
|
80 |
System.out.println(INDENT + "Garbage Collector name: " + gc.getName());
|
|
81 |
System.out.println(INDENT + "GC Count: " + gc.getCollectionCount());
|
|
82 |
System.out.println(INDENT + "GC Time : " + gc.getCollectionTime());
|
|
83 |
} else {
|
|
84 |
System.out.println(INDENT + "Memory Manager name: " + mgr.getName());
|
|
85 |
}
|
|
86 |
|
|
87 |
System.out.print("Pool = [");
|
|
88 |
String[] pools = mgr.getMemoryPoolNames();
|
|
89 |
for (int i = 0; i < pools.length; i++) {
|
|
90 |
System.out.print(INDENT + pools[i]);
|
|
91 |
if (i < (pools.length - 1)) {
|
|
92 |
System.out.print(" | ");
|
|
93 |
}
|
|
94 |
}
|
|
95 |
System.out.println("]");
|
|
96 |
}
|
|
97 |
|
|
98 |
public static void printMemoryManagers(List managers) {
|
|
99 |
ListIterator iter = managers.listIterator();
|
|
100 |
System.out.println(INDENT + "Number of memory managers = " +
|
|
101 |
managers.size());
|
|
102 |
while (iter.hasNext()) {
|
|
103 |
MemoryManagerMXBean mgr = (MemoryManagerMXBean) iter.next();
|
|
104 |
printMemoryManager(mgr);
|
|
105 |
}
|
|
106 |
}
|
|
107 |
|
|
108 |
public static void printMemoryNotificationInfo
|
|
109 |
(MemoryNotificationInfo minfo, String type) {
|
|
110 |
System.out.print("Notification for " + minfo.getPoolName());
|
|
111 |
System.out.print(" [type = " + type);
|
|
112 |
System.out.println(" count = " + minfo.getCount() + "]");
|
|
113 |
System.out.println(INDENT + "usage = " + minfo.getUsage());
|
|
114 |
}
|
|
115 |
}
|