JVM监测&工具[转]
通过工具及Java api来监测JVM的运行状态, 需要监测的数据:(内存使用情况 谁使用了内存 GC的状况)
内存使用情况--heap&PermGen
@ 表示通过jmap –heap pid 可以获取的值
# 表示通过jstat –gcutil pid 可以获取的值
参数的查看可以通过多种方法 本文中只随机列出一种。
描述 | 最大值 | 当前值 | 报警值 |
堆内存 | @Heap Configuration::MaxHeapSize sum(eden+servivor+old) |
sum(eden+servivor+old) | 自设 |
非堆内存 | sum(perm+native) | 无 | |
Eden | @Eden Space::capacity | @Eden Space::used | 无 |
Survivor0 | @From Space::capacity | @From Space::used | 无 |
Survivor1 | @To Space::capacity | @To Space::used | 无 |
New gen (注意区别于Xmn参数设置) |
@New Generation::capacity Eden + 1 Survivor Space |
@New Generation::used | 无 |
Old gen | @concurrent mark-sweep generation::capacity (CMS是对old区的gc,所以此处即表示old gen) |
@concurrent mark-sweep generation::capacity(CMS)::used | 自设 |
Perm Gen | @Perm Generation::capacity | @Perm Generation::used | 自设 |
内存使用情况--config
描述 | 配置值 |
MaxTenuringThreshold | jinfo -flag MaxTenuringThreshold pid |
MinHeapFreeRatio | @Heap Configuration::MinHeapFreeRatio |
MaxHeapFreeRatio | @Heap Configuration::MaxHeapFreeRatio |
new gen gc | @using … in the new generation |
old gen gc | new gen gc声明下方 |
类总数统计 | ?? |
内存使用情况—C heap
- top or ps aux
谁使用了内存
- Heap
jmap –histo
jmap –dump ,then mat - C heap
google perftools
GC的状况
描述 | 收集次数 | 收集时间 | 应用暂停时间 |
Full GC | #FGC | #FGCT | 设置-XX:+PrintGCApplicationStoppedTime后在日志中查看 |
Young GC | #YGC | #YGCT | 同上 |
-XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -XX:+PrintGCApplicationStoppedTime -Xloggc:logs/gc.log
常用工具介绍:jinfo jmap jstack jstat
- 可
以从一个给定的java进程或core文件或远程debug服务器上获取java配置信息。包括java系统属性及JVM参数(command line
flags)。注意在jvm启动参数中没有配置的参数也可使用jinfo –flag xxx
pid输出默认值(很有用,但貌似一些简写的参数查不出来)。 - 可以修改运行时的java 进程的opts。
- 只有solaris和linux的JDK版本里有。
- 使用方式可使用jinfo –h 查询。
观察运行中的jvm物理内存的占用情况。
如果连用SHELL jmap -histo pid>a.log可以将其保存到文本中去,在一段时间后,使用文本对比工具,可以对比出GC回收了哪些对象。
参数很简单,直接查看jmap -h
举例:
jmap -heap pid
jmap -dump:format=b,file=heap.hprof <pid>
观察jvm中当前所有线程的运行情况和线程当前状态
如果java程序崩溃生成core文件,jstack工具可以用来获得core文件的java stack和native stack的信息,从而可以轻松地知道java程序是如何崩溃和在程序何处发生问题。另外,jstack工具还可以附属到正在运行的java程序中,看到 当时运行的java程序的java stack和native stack的信息, 如果现在运行的java程序呈现hung的状态,jstack是非常有用的。目前只有在Solaris和Linux的JDK版本里面才有。
参数很简单,直接查看jstack -h
举例:
jstack pid
JVM监测工具(Java Virtual Machine Statistics Monitoring Tool)。利用了JVM内建的指令对Java应用程序的资源和性能进行实时的命令行的监控,包括各种堆和非堆的大小及其内存使用量、 classloader、compiler、垃圾回收状况等。
举例:
jstat –printcompilation -h10 3024 250 600
每250毫秒打印一次,一共打印600次 每隔10行显示一次head
Usage: jstat -help|-options
jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]
参数介绍:
- -h n 每隔几行输出标题
- vmid VM的进程号,即当前运行的java进程号
- -t 在第一列显示自JVM启动以来的时间戳
- -J 修改java进程的参数。类似jinfo -flag <name>=<value>。例如-J-Xms48m 设置初始堆为48M。详见这里。这个参数挺有用的,可以在运行中调整参数以方便测试、监测。
- -option option为要检测的参数。参数列表可通过jstat –options 获取。下面将分别介绍每个参数及输出字段的含义。
class | 统计class loader行为信息 |
compiler | 统计编译行为信息 |
gc | 统计jdk gc时heap信息 |
gccapacity | 统计堆内存不同代的heap容量信息 |
gccause | 统计gc的情况(同-gcutil)和引起gc的事件 |
gcnew | 统计gc时新生代的信息(相比gcutil更详细) |
gcnewcapacity | 统计gc时新生代heap容量 |
gcold | 统计gc时,老年区的情况 |
gcoldcapacity | 统计gc时,老年区heap容量 |
gcpermcapacity | 统计gc时,permanent区heap容量 |
gcutil | 统计gc时,heap情况 |
printcompilation | 统计编译行为信息 |
-class option:Class Loader Statistics
Column | Description |
---|---|
Loaded | Number of classes loaded. |
Bytes | Number of Kbytes loaded. |
Unloaded | Number of classes unloaded. |
Bytes | Number of Kbytes unloaded. |
Time | Time spent performing class load and unload operations. |
-compiler:HotSpot Just-In-Time Compiler Statistics
Column | Description |
---|---|
Compiled | Number of compilation tasks performed. |
Failed | Number of compilation tasks that failed. |
Invalid | Number of compilation tasks that were invalidated. |
Time | Time spent performing compilation tasks. |
FailedType | Compile type of the last failed compilation. |
FailedMethod | Class name and method for the last failed compilation. |
-gc Option:Garbage-collected heap statistics
Column | Description |
---|---|
S0C | Current survivor space 0 capacity (KB). |
S1C | Current survivor space 1 capacity (KB). |
S0U | Survivor space 0 utilization (KB). |
S1U | Survivor space 1 utilization (KB). |
EC | Current eden space capacity (KB). |
EU | Eden space utilization (KB). |
OC | Current old space capacity (KB). |
OU | Old space utilization (KB). |
PC | Current permanent space capacity (KB). |
PU | Permanent space utilization (KB). |
YGC | Number of young generation GC Events. |
YGCT | Young generation garbage collection time. |
FGC | Number of full GC events. |
FGCT | Full garbage collection time. |
GCT | Total garbage collection time. |
-gccapacity Option:Memory Pool Generation and Space Capacities
Column | Description |
---|---|
NGCMN | Minimum new generation capacity (KB). |
NGCMX | Maximum new generation capacity (KB). |
NGC | Current new generation capacity (KB). |
S0C | Current survivor space 0 capacity (KB). |
S1C | Current survivor space 1 capacity (KB). |
EC | Current eden space capacity (KB). |
OGCMN | Minimum old generation capacity (KB). |
OGCMX | Maximum old generation capacity (KB). |
OGC | Current old generation capacity (KB). |
OC | Current old space capacity (KB). |
PGCMN | Minimum permanent generation capacity (KB). |
PGCMX | Maximum Permanent generation capacity (KB). |
PGC | Current Permanent generation capacity (KB). |
PC | Current Permanent space capacity (KB). |
YGC | Number of Young generation GC Events. |
FGC | Number of Full GC Events. |
-gccause Option:Garbage Collection Statistics, Including GC Events
Column | Description |
---|---|
LGCC | Cause of last Garbage Collection. |
GCC | Cause of current Garbage Collection. |
前面的字段与gcutil相同.
-gcnew Option:New Generation Statistics
Column | Description |
---|---|
S0C | Current survivor space 0 capacity (KB). |
S1C | Current survivor space 1 capacity (KB). |
S0U | Survivor space 0 utilization (KB). |
S1U | Survivor space 1 utilization (KB). |
TT | Tenuring threshold. |
MTT | Maximum tenuring threshold. |
DSS | Desired survivor size (KB). |
EC | Current eden space capacity (KB). |
EU | Eden space utilization (KB). |
YGC | Number of young generation GC events. |
YGCT | Young generation garbage collection time. |
-gcnewcapacity Option:New Generation Space Size Statistics
Column | Description |
---|---|
NGCMN | Minimum new generation capacity (KB). |
NGCMX | Maximum new generation capacity (KB). |
NGC | Current new generation capacity (KB). |
S0CMX | Maximum survivor space 0 capacity (KB). |
S0C | Current survivor space 0 capacity (KB). |
S1CMX | Maximum survivor space 1 capacity (KB). |
S1C | Current survivor space 1 capacity (KB). |
ECMX | Maximum eden space capacity (KB). |
EC | Current eden space capacity (KB). |
YGC | Number of young generation GC events. |
FGC | Number of Full GC Events. |
-gcold Option:Old and Permanent Generation Statistics
Column | Description |
---|---|
PC | Current permanent space capacity (KB). |
PU | Permanent space utilization (KB). |
OC | Current old space capacity (KB). |
OU | old space utilization (KB). |
YGC | Number of young generation GC events. |
FGC | Number of full GC events. |
FGCT | Full garbage collection time. |
GCT | Total garbage collection time. |
-gcoldcapacity Option:Old Generation Statistics
Column | Description |
---|---|
OGCMN | Minimum old generation capacity (KB). |
OGCMX | Maximum old generation capacity (KB). |
OGC | Current old generation capacity (KB). |
OC | Current old space capacity (KB). |
YGC | Number of young generation GC events. |
FGC | Number of full GC events. |
FGCT | Full garbage collection time. |
GCT | Total garbage collection time. |
-gcpermcapacity Option: Permanent Generation Statistics
Column | Description |
---|---|
PGCMN | Minimum permanent generation capacity (KB). |
PGCMX | Maximum permanent generation capacity (KB). |
PGC | Current permanent generation capacity (KB). |
PC | Current permanent space capacity (KB). |
YGC | Number of young generation GC events. |
FGC | Number of full GC events. |
FGCT | Full garbage collection time. |
GCT | Total garbage collection time. |
-gcutil Option:Summary of Garbage Collection Statistics
Column | Description |
---|---|
S0 | Survivor space 0 utilization as a percentage of the space's current capacity. |
S1 | Survivor space 1 utilization as a percentage of the space's current capacity. |
E | Eden space utilization as a percentage of the space's current capacity. |
O | Old space utilization as a percentage of the space's current capacity. |
P | Permanent space utilization as a percentage of the space's current capacity. |
YGC | Number of young generation GC events. |
YGCT | Young generation garbage collection time. |
FGC | Number of full GC events. |
FGCT | Full garbage collection time. |
GCT | Total garbage collection time. |
-printcompilation Option: HotSpot Compiler Method Statistics
Column | Description |
---|---|
Compiled | Number of compilation tasks performed. |
Size | Number of bytes of bytecode for the method. |
Type | Compilation type. |
Method | Class name and method name identifying the compiled method. Class name uses "/" instead of "." as namespace separator. Method name is the method within the given class. The format for these two fields is consistent with the HotSpot - XX:+PrintComplation option. |
jhat
The jhat tool provides a convenient means to browse the object topology in a heap snapshot. This tool replaces the Heap Analysis Tool (HAT). The tool parses a heap dump in binary format (for example, a heap dump produced by jcmd). This utility can help debug unintentional object relation. This term is used to describe an object that is no longer needed but is kept alive due to references through some path from the rootset. This can happen, for example, if an unintentional static reference to an object remains after the object is no longer needed, if an observer or listener fails to unregister itself from its subject when it is no longer needed, or if a thread that refers to an object does not terminate when it should. Unintentional object relation is the Java language equivalent of a memory leak.
We can analyze heap dump using jhat with the following command
jhat <HPROF_FILE>
This command reads the .hprof file and starts a server on port 7000.
When we are connected to the server using http://localhost:7000, we can execute a standard query or create an Object Query Language (OQL). The All Classes query is displayed by default. This default page displays all of the classes present in the heap, excluding platform classes. This list is sorted by fully qualified class name, and broken out by package. Click the name of a class to go to the Class query. The second variant of this query includes the platform classes. Platform classes include classes whose fully qualified names start with prefixes such as java, sun or javax.swing. On the other hand, the class query displays the information about a class. This includes its superclass, any subclasses, instance data members, and static data members. From this page you can navigate to any of the classes that are referenced, or you can navigate to an instance query. The instance query displays all instances of a given class.
Java api方式监测
jre中提供了一些查看运行中的jvm内部信息的api,这些api包含在java.lang.management包中,此包中的接口是在jdk 5中引入的,所以只有在jdk 5及其以上版本中才能通过这种方式访问这些信息。下面简单介绍一下这包括哪些信息,以及如何访问。
可以通过此api访问到运行中的jvm的类加载的信息、jit编译器的信息、内存分配的情况、线程的相关信息以及运行jvm的操作系统的信息。java.lang.management包中提供了9个接口来访问这些信息,使用ManagementFactory的静态get方法可以获得相应接口的实例,可以通过这些实例来获取你需要的相关信息。
更详细的关于MBean的介绍参见Java SE 6 新特性: JMX 与系统管理
demo1:查看一下当前运行的jvm中加载了多少个类。想详细了解如何使用这些api,可以参考java.lang.management包中的详细api文档。
public class ClassLoaderChecker {
public static void main( String[] args ) throws Exception {
ClassLoadingMXBean bean = ManagementFactory.getClassLoadingMXBean();
System.out.println( bean.getLoadedClassCount() );
}
}
//工具方法
public static ObjectName register(String name, Object mbean) {
try {
ObjectName objectName = new ObjectName(name); MBeanServer mbeanServer = ManagementFactory
.getPlatformMBeanServer(); try {
mbeanServer.registerMBean(mbean, objectName);
} catch (InstanceAlreadyExistsException ex) {
mbeanServer.unregisterMBean(objectName);
mbeanServer.registerMBean(mbean, objectName);
} return objectName;
} catch (JMException e) {
throw new IllegalArgumentException(name, e);
}
}
//步骤一:定义Mbean接口:
//随便定义
public interface DemoMBean {
public AtomicLong getInvokeCount();
}
//步骤二:实现接口,并注册:
public class DemoImpl implements DemoMBean{
public final static String DEFAULT_OBJECT_NAME_PREFIX = "com.redcreen.demo:type=demo";
register("com.redcreen.demo:type=demo",DemoImpl.instance);
}
//可以通过jconsole中查看数据了
相关推荐文章:
参考:
http://www.51testing.com/?uid-183198-action-viewspace-itemid-185174
JVM监控工具介绍jstack, jconsole, jinfo, jmap, jdb, jstat
http://stl-www.htw-saarland.de/syst-lab/java/jdk-1_5_0/docs/tooldocs/share/jinfo.html
http://www.coderanch.com/t/329407/java/java/find-all-loaded-classes-classloaders
JVM监测&工具[转]的更多相关文章
- JVM系列五:JVM监测&工具[整理中]
前几篇篇文章介绍了介绍了JVM的参数设置并给出了一些生产环境的JVM参数配置参考方案.正如之前文章中提到的JVM参数的设置需要根据应用的特性来进行设置,每个参数的设置都需要对JVM进行长时间的监测,并 ...
- JVM系列五:JVM监测&工具
JVM系列五:JVM监测&工具[整理中] http://www.cnblogs.com/redcreen/archive/2011/05/09/2040977.html 前几篇篇文章介绍了介 ...
- [转]JVM系列五:JVM监测&工具[整理中]
原文地址:http://www.cnblogs.com/redcreen/archive/2011/05/09/2040977.html 前几篇篇文章介绍了介绍了JVM的参数设置并给出了一些生产环境的 ...
- JVM性能监测工具——VisualVM
Java本身自带了有好几个jvm监测工具,其中jconsole和jvisualvm这两个工具具有图形化界面,可以监测到cpu.类.线程.堆等一些参数,而且具有远程监控的能力. 启动:打开cmd命令窗口 ...
- JVM检测&工具
前几篇篇文章介绍了介绍了JVM的参数设置并给出了一些生产环境的JVM参数配置参考方案.正如之前文章中提到的JVM参数的设置需要根据应用的特性来进行设置,每个参数的设置都需要对JVM进行长时间的监测,并 ...
- JDK内置性能监测工具使用
Java自带的性能监测工具用法简介——jstack.jconsole.jinfo.jmap.jdb.jsta.jvisualvmJDK内置工具使用 一.javah命令(C Header and Stu ...
- JVM故障处理工具,使用总结
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 用都用不到怎么学? 没有场景.没有诉求,怎么学习这些似乎用不上知识点. 其实最好的方 ...
- Android 内存监测工具 DDMS --> Heap
一.什么是内存泄露 内存泄露是指程序中间动态分配了内存,但是在程序结束时没有释放这部分内存,从而造成那一部分内存不可用.导致系统运行变慢或应用程序崩溃.二.如何检测Android中的内存泄露 ...
- paip.提升性能---jvm java 工具使用.
paip.提升性能---jvm java 工具使用. 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn ...
随机推荐
- Android 监听锁屏、解锁、开屏 操作
1.首先定义 ScreenListener package com.app.lib; import android.content.BroadcastReceiver; import android ...
- iOS中的小知识点
1.tableView隐藏滚动条 self.tableView.showsVerticalScrollIndicator = NO; 2.关于属性 使用assign: 对基础数据类型 (NSI ...
- Tableau——BI software
Tableau 8权威指南 (权威的数据可视化实战手册,中国传媒大学教授沈浩.北京大学研究员袁晓如 联袂推荐) 触手可及的大数据分析工具——Tableau案例集 写给专业数据分析师的丛书,无门槛的大数 ...
- [Linux 性能检测工具]VMSTAT
VMSTAT NAME: Vmstat: 报告虚拟内存统计 语法 : vmstat [-a] [-n] [-t] [-S unit] [delay [ count]] ...
- Javascript之旅——第四站:parseInt中要注意的坑
前些天信用卡站点要接入一个新功能,不过还真比较坑爹,asp站点,大家都知道信用卡的背面是有一个有效期的,在对接银行中这个信息 一定是要传给银行做数据校验,用户在语音输入信用卡有效期后,系统会做一个有效 ...
- Ngigx+Tomcat配置动静分离,负载均衡
由于公司使用过Ngnix,对于刚接触Nginx来说,感觉有些好奇,于是研究了下. 本人在windows下使用的版本是nginx-1.8.1: 1. 启动Ngnix 双击nginx-1.8.1文件夹中n ...
- 一致性算法Paxos详解
分布式系统除了能提升整个系统的性能外还有一个重要的特性就是提高系统的可靠性,可靠性指的是当分布式系统中一台或N台机器宕掉后都不会导致系统不可用,分布式系统是state machine replicat ...
- eclipse出现的JRE运行环境错误
adt-bundle-linux eclipse启动运行错误,提示如下: A Java RunTime Environment (JRE) or Java Development Kit (JDK) ...
- Linux 多线程信号量同步
PV原子操作 P操作: 如果有可用的资源(信号量值>0),则此操作所在的进程占用一个资源(此时信号量值减1,进入临界区代码); 如果没有可用的资源(信号量值=0),则此操作所在的进程被阻塞直到系 ...
- 一个PHP混淆后门的分析
洒家的朋友的公司的某个站发现最近被上传了一个后门程序.为了取证我们抓取了HTTP请求流量,看到了一堆莫名其妙看似经过混淆的请求,响应也是看似base64的乱码.洒家用了2个小时静态分析了一遍,并写了利 ...