KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > PrintGCStat


1 /*
2  * @(#)PrintGCStat.java 1.4 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)PrintGCStat.java 1.4 05/11/17
39  */

40
41 import static java.lang.management.ManagementFactory JavaDoc.*;
42 import java.lang.management.*;
43 import javax.management.*;
44 import java.io.*;
45 import java.util.*;
46
47 /**
48  * Example of using the java.lang.management API to monitor
49  * the memory usage and garbage collection statistics.
50  *
51  * @author Mandy Chung
52  * @version %% 11/17/05
53  */

54 public class PrintGCStat {
55     private RuntimeMXBean rmbean;
56     private MemoryMXBean mmbean;
57     private List<MemoryPoolMXBean> pools;
58     private List<GarbageCollectorMXBean> gcmbeans;
59     
60     /**
61      * Constructs a PrintGCStat object to monitor a remote JVM.
62      */

63     public PrintGCStat(MBeanServerConnection server) throws IOException {
64         // Create the platform mxbean proxies
65
this.rmbean = newPlatformMXBeanProxy(server,
66                                              RUNTIME_MXBEAN_NAME,
67                                              RuntimeMXBean.class);
68         this.mmbean = newPlatformMXBeanProxy(server,
69                                              MEMORY_MXBEAN_NAME,
70                                              MemoryMXBean.class);
71         ObjectName poolName = null;
72         ObjectName gcName = null;
73         try {
74             poolName = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE+",*");
75             gcName = new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE+",*");
76         } catch (MalformedObjectNameException e) {
77             // should not reach here
78
assert(false);
79         }
80
81         Set mbeans = server.queryNames(poolName, null);
82         if (mbeans != null) {
83             pools = new ArrayList<MemoryPoolMXBean>();
84             Iterator iterator = mbeans.iterator();
85             while (iterator.hasNext()) {
86                 ObjectName objName = (ObjectName) iterator.next();
87                 MemoryPoolMXBean p =
88                     newPlatformMXBeanProxy(server,
89                                            objName.getCanonicalName(),
90                                            MemoryPoolMXBean.class);
91                 pools.add(p);
92             }
93         }
94
95         mbeans = server.queryNames(gcName, null);
96         if (mbeans != null) {
97             gcmbeans = new ArrayList<GarbageCollectorMXBean>();
98             Iterator iterator = mbeans.iterator();
99             while (iterator.hasNext()) {
100                 ObjectName objName = (ObjectName) iterator.next();
101                 GarbageCollectorMXBean gc =
102                     newPlatformMXBeanProxy(server,
103                                            objName.getCanonicalName(),
104                                            GarbageCollectorMXBean.class);
105                 gcmbeans.add(gc);
106             }
107         }
108     }
109
110     /**
111      * Constructs a PrintGCStat object to monitor the local JVM.
112      */

113     public PrintGCStat() {
114         // Obtain the platform mxbean instances for the running JVM.
115
this.rmbean = getRuntimeMXBean();
116         this.mmbean = getMemoryMXBean();
117         this.pools = getMemoryPoolMXBeans();
118         this.gcmbeans = getGarbageCollectorMXBeans();
119     }
120
121     /**
122      * Prints the verbose GC log to System.out to list the memory usage
123      * of all memory pools as well as the GC statistics.
124      */

125     public void printVerboseGc() {
126         System.out.print("Uptime: " + formatMillis(rmbean.getUptime()));
127         for (GarbageCollectorMXBean gc : gcmbeans) {
128             System.out.print(" [" + gc.getName() + ": ");
129             System.out.print("Count=" + gc.getCollectionCount());
130             System.out.print(" GCTime=" + formatMillis(gc.getCollectionTime()));
131             System.out.print("]");
132         }
133         System.out.println();
134         for (MemoryPoolMXBean p : pools) {
135             System.out.print(" [" + p.getName() + ":");
136             MemoryUsage u = p.getUsage();
137             System.out.print(" Used=" + formatBytes(u.getUsed()));
138             System.out.print(" Committed=" + formatBytes(u.getCommitted()));
139             System.out.println("]");
140         }
141     }
142
143     private String JavaDoc formatMillis(long ms) {
144         return String.format("%.4fsec", ms / (double) 1000);
145     }
146     private String JavaDoc formatBytes(long bytes) {
147         long kb = bytes;
148         if (bytes > 0) {
149             kb = bytes / 1024;
150         }
151         return kb + "K";
152     }
153 }
154
Popular Tags