Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/demo/management/VerboseGC/PrintGCStat.java
38829 views
1
/*
2
* Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
3
*
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
6
* are met:
7
*
8
* - Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
*
11
* - Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
*
15
* - Neither the name of Oracle nor the names of its
16
* contributors may be used to endorse or promote products derived
17
* from this software without specific prior written permission.
18
*
19
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
*/
31
32
/*
33
* This source code is provided to illustrate the usage of a given feature
34
* or technique and has been deliberately simplified. Additional steps
35
* required for a production-quality application, such as security checks,
36
* input validation and proper error handling, might not be present in
37
* this sample code.
38
*/
39
40
41
/*
42
*/
43
44
import static java.lang.management.ManagementFactory.*;
45
import java.lang.management.*;
46
import javax.management.*;
47
import java.io.*;
48
import java.util.*;
49
50
/**
51
* Example of using the java.lang.management API to monitor
52
* the memory usage and garbage collection statistics.
53
*
54
* @author Mandy Chung
55
*/
56
public class PrintGCStat {
57
private RuntimeMXBean rmbean;
58
private MemoryMXBean mmbean;
59
private List<MemoryPoolMXBean> pools;
60
private List<GarbageCollectorMXBean> gcmbeans;
61
62
/**
63
* Constructs a PrintGCStat object to monitor a remote JVM.
64
*/
65
public PrintGCStat(MBeanServerConnection server) throws IOException {
66
// Create the platform mxbean proxies
67
this.rmbean = newPlatformMXBeanProxy(server,
68
RUNTIME_MXBEAN_NAME,
69
RuntimeMXBean.class);
70
this.mmbean = newPlatformMXBeanProxy(server,
71
MEMORY_MXBEAN_NAME,
72
MemoryMXBean.class);
73
ObjectName poolName = null;
74
ObjectName gcName = null;
75
try {
76
poolName = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE+",*");
77
gcName = new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE+",*");
78
} catch (MalformedObjectNameException e) {
79
// should not reach here
80
assert(false);
81
}
82
83
Set<ObjectName> mbeans = server.queryNames(poolName, null);
84
if (mbeans != null) {
85
pools = new ArrayList<MemoryPoolMXBean>();
86
for (ObjectName objName : mbeans) {
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
for (ObjectName objName : mbeans) {
99
GarbageCollectorMXBean gc =
100
newPlatformMXBeanProxy(server,
101
objName.getCanonicalName(),
102
GarbageCollectorMXBean.class);
103
gcmbeans.add(gc);
104
}
105
}
106
}
107
108
/**
109
* Constructs a PrintGCStat object to monitor the local JVM.
110
*/
111
public PrintGCStat() {
112
// Obtain the platform mxbean instances for the running JVM.
113
this.rmbean = getRuntimeMXBean();
114
this.mmbean = getMemoryMXBean();
115
this.pools = getMemoryPoolMXBeans();
116
this.gcmbeans = getGarbageCollectorMXBeans();
117
}
118
119
/**
120
* Prints the verbose GC log to System.out to list the memory usage
121
* of all memory pools as well as the GC statistics.
122
*/
123
public void printVerboseGc() {
124
System.out.println("Uptime: " + formatMillis(rmbean.getUptime()));
125
System.out.println("Heap usage: " + mmbean.getHeapMemoryUsage());
126
System.out.println("Non-Heap memory usage: " + mmbean.getNonHeapMemoryUsage());
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 formatMillis(long ms) {
144
return String.format("%.4fsec", ms / (double) 1000);
145
}
146
private String formatBytes(long bytes) {
147
long kb = bytes;
148
if (bytes > 0) {
149
kb = bytes / 1024;
150
}
151
return kb + "K";
152
}
153
}
154
155