java监控之ManagementFactory分析
ManagementFactory
class is a factory class for getting managed beans for the Java platform. This class consists of static methods each of which returns one or more platform MXBeans representing the management interface of a component of the Java virtual machine.
Platform MXBeans
A platform MXBean is a managed bean that conforms to the JMX Instrumentation Specification and only uses a set of basic data types. A JMX management application and the platform MBeanServer can interoperate without requiring classes for MXBean specific data types. The data types being transmitted between the JMX connector server and the connector client are open types and this allows interoperation across versions. See the specification of MXBeans for details.
Each platform MXBean is a PlatformManagedObject
and it has a unique ObjectName
for registration in the platform MBeanServer
as returned by by the getObjectName
method.
An application can access a platform MXBean in the following ways:
1. Direct access to an MXBean interface
- Get an MXBean instance by calling the
getPlatformMXBean
orgetPlatformMXBeans
method and access the MXBean locally in the running virtual machine.- Construct an MXBean proxy instance that forwards the method calls to a given
MBeanServer
by calling thegetPlatformMXBean(MBeanServerConnection, Class)
orgetPlatformMXBeans(MBeanServerConnection, Class)
method. ThenewPlatformMXBeanProxy
method can also be used to construct an MXBean proxy instance of a givenObjectName
. A proxy is typically constructed to remotely access an MXBean of another running virtual machine.2. Indirect access to an MXBean interface via MBeanServer
- Go through the platform
MBeanServer
to access MXBeans locally or a specific MBeanServerConnection to access MXBeans remotely. The attributes and operations of an MXBean use only JMX open types which include basic data types,CompositeData
, andTabularData
defined inOpenType
. The mapping is specified in theMXBean specification for details.
The getPlatformManagementInterfaces
method returns all management interfaces supported in the Java virtual machine including the standard management interfaces listed in the tables below as well as the management interfaces extended by the JDK implementation.
A Java virtual machine has a single instance of the following management interfaces:
A Java virtual machine has zero or a single instance of the following management interfaces.
Management Interface ObjectName CompilationMXBean
java.lang:type=Compilation
A Java virtual machine may have one or more instances of the following management interfaces.
Management Interface ObjectName GarbageCollectorMXBean
java.lang:type=GarbageCollector
,name=collector's nameMemoryManagerMXBean
java.lang:type=MemoryManager
,name=manager's nameMemoryPoolMXBean
java.lang:type=MemoryPool
,name=pool's nameBufferPoolMXBean
java.nio:type=BufferPool,name=
pool name
实例1:
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.Set;
import javax.management.AttributeNotFoundException;
import javax.management.BadAttributeValueExpException;
import javax.management.BadBinaryOpValueExpException;
import javax.management.BadStringOperationException;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.InvalidApplicationException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.QueryExp;
import javax.management.ReflectionException;
import javax.management.RuntimeMBeanException; public class JMXUtils { private final static MBeanServer DEFAULT_MBEAN_SERVER = ManagementFactory
.getPlatformMBeanServer(); public static long getYongGC() {
return getYoungGC(DEFAULT_MBEAN_SERVER);
} public static long getFullGC() {
return getFullGC(DEFAULT_MBEAN_SERVER);
} public static long findLoadedClass() {
return findLoadedClass(DEFAULT_MBEAN_SERVER);
} public static long getYoungGC(MBeanServerConnection mbeanServer) {
try {
ObjectName objectName;
if (mbeanServer.isRegistered(new ObjectName("java.lang:type=GarbageCollector,name=ParNew"))) {
objectName = new ObjectName("java.lang:type=GarbageCollector,name=ParNew");
} else if (mbeanServer.isRegistered(new ObjectName("java.lang:type=GarbageCollector,name=Copy"))) {
objectName = new ObjectName("java.lang:type=GarbageCollector,name=Copy");
} else {
objectName = new ObjectName("java.lang:type=GarbageCollector,name=PS Scavenge");
}
return (Long) mbeanServer.getAttribute(objectName , "CollectionCount");
} catch (Exception e) {
throw new RuntimeException(e);
}
} public static long getFullGC(MBeanServerConnection mbeanServer) {
try {
ObjectName objectName;
if (mbeanServer.isRegistered(new ObjectName("java.lang:type=GarbageCollector,name=ConcurrentMarkSweep"))) {
objectName = new ObjectName("java.lang:type=GarbageCollector,name=ConcurrentMarkSweep");
} else if (mbeanServer.isRegistered(new ObjectName("java.lang:type=GarbageCollector,name=MarkSweepCompact"))) {
objectName = new ObjectName("java.lang:type=GarbageCollector,name=MarkSweepCompact");
} else {
objectName = new ObjectName("java.lang:type=GarbageCollector,name=PS MarkSweep");
}
return (Long) mbeanServer.getAttribute(objectName , "CollectionCount");
} catch (Exception e) {
throw new RuntimeException(e);
}
} public static long findLoadedClass(MBeanServerConnection mBeanServer) {
try {
return (Long) (mBeanServer.getAttribute(new ObjectName(
"java.lang:type=ClassLoading"), "TotalLoadedClassCount"));
} catch (Exception e) {
throw new RuntimeException(e);
}
} public static void traceOneDomain(String doMain,
MBeanServerConnection mBeanServer)
throws MalformedObjectNameException, IntrospectionException,
InstanceNotFoundException, AttributeNotFoundException,
ReflectionException, MBeanException, IOException { Set<ObjectInstance> set = mBeanServer.queryMBeans(new ObjectName(doMain + ":*"), new QueryExp() {
private static final long serialVersionUID = 1L; @Override
public boolean apply(ObjectName name)
throws BadStringOperationException,
BadBinaryOpValueExpException,
BadAttributeValueExpException, InvalidApplicationException {
return true;
} @Override
public void setMBeanServer(MBeanServer s) {}
});
for (ObjectInstance objectInstance : set) {
System.out.println("\t\t\t" + objectInstance.getObjectName() + "\t"
+ objectInstance.getClassName());
traceMebeanInfo(mBeanServer, objectInstance.getObjectName());
}
} public static void traceMebeanInfo(MBeanServerConnection mBeanServer,
ObjectName objectName) throws IntrospectionException,
InstanceNotFoundException, MalformedObjectNameException,
ReflectionException, AttributeNotFoundException, MBeanException,
IOException {
MBeanInfo mBeanInfo = mBeanServer.getMBeanInfo(objectName);
MBeanAttributeInfo[] mBeanAttributes = mBeanInfo.getAttributes(); System.out.println("\t\t\tMBeanInfos : ");
for (MBeanAttributeInfo mBeanAttribute : mBeanAttributes) {
try {
System.out.println("\t\t\t\t\t"
+ mBeanAttribute.getName()
+ "\t"
+ mBeanAttribute.getType()
+ "\tvalue = >"
+ mBeanServer.getAttribute(objectName,
mBeanAttribute.getName()));
} catch (RuntimeMBeanException e) {
if (e.getCause() instanceof UnsupportedOperationException) {
System.out.println("\t\t\t\t\t" + mBeanAttribute.getName()
+ "\t" + mBeanAttribute.getType()
+ "\tvalue = > value not supported");
}
} }
} public static void traceAll(MBeanServerConnection mBeanServer)
throws MalformedObjectNameException, IntrospectionException,
InstanceNotFoundException, AttributeNotFoundException,
ReflectionException, MBeanException, IOException {
System.out.println("MBean count = " + mBeanServer.getMBeanCount());
String[] domains = mBeanServer.getDomains();
for (String domain : domains) {
System.out.println("\tbegin trace domain -> " + domain);
traceOneDomain(domain, mBeanServer);
}
}
}
实例2:
<%@ page import="java.lang.management.*" %>
<%@ page import="java.util.*" %>
<html>
<head>
<title>JVM Memory Monitor</title>
</head>
<body>
<table border="0" width="100%">
<tr><td colspan="2" align="center"><h3>Memory MXBean</h3></td></tr>
<tr><td width="200">Heap Memory Usage</td><td><%=ManagementFactory.getMemoryMXBean().getHeapMemoryUsage()%></td></tr>
<tr><td>Non-Heap Memory Usage</td><td><%=ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage()%></td></tr>
<tr><td colspan="2"> </td></tr>
<tr><td colspan="2" align="center"><h3>Memory Pool MXBeans</h3></td></tr>
<%
Iterator iter = ManagementFactory.getMemoryPoolMXBeans().iterator();
while (iter.hasNext()) {
MemoryPoolMXBean item = (MemoryPoolMXBean) iter.next();
%>
<tr><td colspan="2">
<table border="0" width="100%" style="border: 1px #98AAB1 solid;">
<tr><td colspan="2" align="center"><b><%= item.getName() %></b></td></tr>
<tr><td width="200">Type</td><td><%= item.getType() %></td></tr>
<tr><td>Usage</td><td><%= item.getUsage() %></td></tr>
<tr><td>Peak Usage</td><td><%= item.getPeakUsage() %></td></tr>
<tr><td>Collection Usage</td><td><%= item.getCollectionUsage() %></td></tr>
</table>
</td></tr>
<tr><td colspan="2"> </td></tr>
<%} %>
</table>
</body>
</html>
另外的方式:
内存监控的方法: 1. jmap -heap pid
查看java 堆(heap)使用情况 using thread-local object allocation.
Parallel GC with 4 thread(s) //GC 方式 Heap Configuration: //堆内存初始化配置
MinHeapFreeRatio=40 //对应jvm启动参数-XX:MinHeapFreeRatio设置JVM堆最小空闲比率(default 40)
MaxHeapFreeRatio=70 //对应jvm启动参数 -XX:MaxHeapFreeRatio设置JVM堆最大空闲比率(default 70)
MaxHeapSize=512.0MB //对应jvm启动参数-XX:MaxHeapSize=设置JVM堆的最大大小
NewSize = 1.0MB //对应jvm启动参数-XX:NewSize=设置JVM堆的‘新生代’的默认大小
MaxNewSize =4095MB //对应jvm启动参数-XX:MaxNewSize=设置JVM堆的‘新生代’的最大大小
OldSize = 4.0MB //对应jvm启动参数-XX:OldSize=<value>:设置JVM堆的‘老生代’的大小
NewRatio = 8 //对应jvm启动参数-XX:NewRatio=:‘新生代’和‘老生代’的大小比率
SurvivorRatio = 8 //对应jvm启动参数-XX:SurvivorRatio=设置年轻代中Eden区与Survivor区的大小比值
PermSize= 16.0MB //对应jvm启动参数-XX:PermSize=<value>:设置JVM堆的‘永生代’的初始大小
MaxPermSize=64.0MB //对应jvm启动参数-XX:MaxPermSize=<value>:设置JVM堆的‘永生代’的最大大小 Heap Usage: //堆内存分步
PS Young Generation
Eden Space: //Eden区内存分布
capacity = 20381696 (19.4375MB) //Eden区总容量
used = 20370032 (19.426376342773438MB) //Eden区已使用
free = 11664 (0.0111236572265625MB) //Eden区剩余容量
99.94277218147106% used //Eden区使用比率
From Space: //其中一个Survivor区的内存分布
capacity = 8519680 (8.125MB)
used = 32768 (0.03125MB)
free = 8486912 (8.09375MB)
0.38461538461538464% used
To Space: //另一个Survivor区的内存分布
capacity = 9306112 (8.875MB)
used = 0 (0.0MB)
free = 9306112 (8.875MB)
0.0% used
PS Old Generation //当前的Old区内存分布
capacity = 366280704 (349.3125MB)
used = 322179848 (307.25464630126953MB)
free = 44100856 (42.05785369873047MB)
87.95982001825573% used
PS Perm Generation //当前的 “永生代” 内存分布
capacity = 32243712 (30.75MB)
used = 28918584 (27.57891082763672MB)
free = 3325128 (3.1710891723632812MB)
89.68751488662348% used
参考文献
【1】http://docs.oracle.com/javase/7/docs/api/java/lang/management/ManagementFactory.html
【2】http://blog.csdn.net/xieyuooo/article/details/9817231
【3】http://xstarcd.github.io/wiki/Java/JVM_Heap_Non-heap.html
【4】https://zhidao.baidu.com/question/1989362303218106827.html
java监控之ManagementFactory分析的更多相关文章
- Java 监控基础 - 使用 JMX 监控和管理 Java 程序
点赞再看,动力无限.Hello world : ) 微信搜「程序猿阿朗 」. 本文 Github.com/niumoo/JavaNotes 和 未读代码网站 已经收录,有很多知识点和系列文章. 此篇文 ...
- Java应用常用性能分析工具
Java应用常用性能分析工具 好的工具有能有效改善和提高工作效率或加速分析问题的进度,笔者将从事Java工作中常用的性能工具和大家分享下,如果感觉有用记得投一票哦,如果你有好的工具也可以分享给我 工具 ...
- 【Java线程与内存分析工具】VisualVM与MAT简明教程
目录 前言 VisualVM 安装与配置 本地使用 远程监控 MAT 使用场景 安装与配置 获得堆转储文件 分析堆转储文件 窥探对象内存值 堆转储文件对比分析 总结 前言 本文将简要介绍Java线程与 ...
- Java 类反射机制分析
Java 类反射机制分析 一.反射的概念及在Java中的类反射 反射主要是指程序可以访问.检测和修改它本身状态或行为的一种能力.在计算机科学领域,反射是一类应用,它们能够自描述和自控制.这类应用通过某 ...
- Java 动态代理机制分析及扩展
Java 动态代理机制分析及扩展,第 1 部分 王 忠平, 软件工程师, IBM 何 平, 软件工程师, IBM 简介: 本文通过分析 Java 动态代理的机制和特点,解读动态代理类的源代码,并且模拟 ...
- JAVA 从GC日志分析堆内存 第七节
JAVA 从GC日志分析堆内存 第七节 在上一章中,我们只设置了整个堆的内存大小.但是我们知道,堆又分为了新生代,年老代.他们之间的内存怎么分配呢?新生代又分为Eden和Survivor,他们的比 ...
- java监控函数执行时间
java监控函数执行时间 http://blog.csdn.net/ycg01/article/details/1467542 java监控函数执行时间 标签: javathreadclassstri ...
- Java Reference 源码分析
@(Java)[Reference] Java Reference 源码分析 Reference对象封装了其它对象的引用,可以和普通的对象一样操作,在一定的限制条件下,支持和垃圾收集器的交互.即可以使 ...
- Java 线程池原理分析
1.简介 线程池可以简单看做是一组线程的集合,通过使用线程池,我们可以方便的复用线程,避免了频繁创建和销毁线程所带来的开销.在应用上,线程池可应用在后端相关服务中.比如 Web 服务器,数据库服务器等 ...
随机推荐
- webpack之傻瓜式教程
接触webpack也有挺长一段时间了,公司的项目也是一直用着webpack在打包处理,但前几天在教新人的情况下,遇到了一个问题,那就是:尽管网上的webpack教程满天飞,但是却很难找到一个能让新人快 ...
- Cassandra简介
在前面的一篇文章<图形数据库Neo4J简介>中,我们介绍了一种非常流行的图形数据库Neo4J的使用方法.而在本文中,我们将对另外一种类型的NoSQL数据库——Cassandra进行简单地介 ...
- 探索ASP.NET MVC5系列之~~~4.模型篇---包含模型常用特性和过度提交防御
其实任何资料里面的任何知识点都无所谓,都是不重要的,重要的是学习方法,自行摸索的过程(不妥之处欢迎指正) 汇总:http://www.cnblogs.com/dunitian/p/4822808.ht ...
- DBSCAN密度聚类算法
DBSCAN(Density-Based Spatial Clustering of Applications with Noise,具有噪声的基于密度的聚类方法)是一种很典型的密度聚类算法,和K-M ...
- 关于BAPI_PATIENT_CREATE(病患主数据创建)
第一次使用BAPI,遇到几个问题.现总结如下. CALL FUNCTION 'BAPI_PATIENT_CREATE' EXPORTING client = * INSTITUTION = '*' * ...
- iOS从零开始学习直播之3.美颜
任何一款直播软件都必须进行美颜,不然哪来的那么多美女,所以技术改变世界,不只是说说而已.美颜在采集的时候就得就行,让主播实时看到直播的效果. 1.美颜原理 其实美颜的本质就是美白和磨皮,分别通 ...
- 简述我的SOA服务治理
SOA服务治理 1.解决业务部门服务冲突和纠纷2.版本定义与版本管理3.服务备案与服务管理4.业务监督与服务监控 SOA的战略目的 一.业务价值胜过技术策略 二.战略目标胜过具体项目的效益 三.内置的 ...
- DevOps对于企业IT的价值
其实从敏捷延展开的 DevOps 概念很早就已经被提出,不过由于配套的技术成熟度水平层次不齐, DevOps 的价值一直没有有效地发挥出来.现如今,随着容器技术的发展, DevOps 在企业中的实践难 ...
- postgresql无法安装pldbgapi的问题
要对函数进行调试需要安装插件pldbgapi,当初在windows上面的postgresql实例中执行了一下语句就安装上了: create extension pldbgapi; 但是在linux中执 ...
- oracle常用的快捷键
最近在开发过程中,遇到一些麻烦,就是开发效率问题,有时候其他同事使用PLSQL 编程效率明显高于自己,观察了好久,才发现他使用PLSQL 已经很长时间了而且,他自己也在其中添加了好多快捷方式, 1.登 ...