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 服务器,数据库服务器等 ...
随机推荐
- 浅谈WEB页面提速(前端向)
记得面试现在这份工作的时候,一位领导语重心长地谈道——当今的世界是互联网的世界,IT企业之间的竞争是很激烈的,如果一个网页的加载和显示速度,相比别人的站点页面有那么0.1秒的提升,那也是很大的一个成就 ...
- nodejs进阶(5)—接收请求参数
1. get请求参数接收 我们简单举一个需要接收参数的例子 如果有个查找功能,查找关键词需要从url里接收,http://localhost:8000/search?keyword=地球.通过前面的进 ...
- ajax
常见的HTTP状态码状态码:200 请求成功.一般用于GET和POST方法 OK301 资源移动.所请求资源移动到新的URL,浏览器自动跳转到新的URL Moved Permanently304 未修 ...
- 这些.NET开源项目你知道吗?.NET平台开源文档与报表处理组件集合(三)
在前2篇文章这些.NET开源项目你知道吗?让.NET开源来得更加猛烈些吧 和这些.NET开源项目你知道吗?让.NET开源来得更加猛烈些吧!(第二辑)中,大伙热情高涨.再次拿出自己的私货,在.NET平台 ...
- js:给定两个数组,如何判断他们的相对应下标的元素类型是一样的
题目: 给Array对象原型上添加一个sameStructureAs方法,该方法接收一个任意类型的参数,要求返回当前数组与传入参数数组(假定是)相对应下标的元素类型是否一致. 假设已经写好了Array ...
- CRL快速开发框架系列教程三(更新数据)
本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...
- 使用SwingBench 对Oracle RAC DB性能 压力测试
我们可以使用swingbench这个工具对数据库性能进行压力测试,得到一些性能指标作为参考. SwingBench下载: http://www.dominicgiles.com/downloads.h ...
- WPF 普通属性变化通知
问题描述:使用ObservableCollection<OrderItem> source 给Datagrid.ItemsSource赋值,在后台更新source集合后,前台Datagri ...
- 5.0 JS中引用类型介绍
其实,在前面的"js的六大数据类型"文章中稍微说了一下引用类型.前面我们说到js中有六大数据类型(五种基本数据类型 + 一种引用类型).下面的章节中,我们将详细讲解引用类型. 1. ...
- GIT笔记命令行(1)
Git简单易用,只要输入git就可以列出他的所有参数 C:\Users\spu>git usage: git [--version] [--help] [-C <path>] [-c ...