2009年7月17日星期五

Java Performance Tuning and Troubleshooting

1. Jconsole
两种监控方法:
1) Local Monitor : 在 JDK6 以前,需要增加 java 启动参数 com.sun.management.jmxremote
ex: Java -Dcom.sun.management.jmxremote -jar test.jar
2) Remote Monitor: java -Dcom.sun.management.jmxremote.port=portNum -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false
注意:如果不用 ssl 访问,应该增加参数 -Dcom.sun.management.jmxremote.ssl=false,刚开始我没有加这个参数的,远程连接总是不成功

3) 此外,远程监控还可以通过用户名/口令进行安全设置,也可以通过 SSL 来加密访问,以增加安全性

Jconsole 可以做什么?

1) 主要是总体观察,以大体确定问题在哪里。比如内存是否持续增长(内存泄漏?),CPU时间是否持续升高?

2) 检测线程是否有死锁,以及线程的堆栈查看。只有线程发生死锁的时候才会检测出来,而不是检测所有可能发生死锁的代码

3) 可以查看 load 的类是否持续在增加

4) VM 参数

2. JPS
类似于 unix 的 ps,列出所有进程,这个是列出所有 Java 进程
-q
Suppress the output of the class name, JAR file name, and arguments passed to the main method, producing only a list of local VM identifiers.
-m
Output the arguments passed to the main method. The output may be null for embedded JVMs.
-l
Output the full package name for the application's main class or the full path name to the application's JAR file.
-v
Output the arguments passed to the JVM.
-V
Output the arguments passed to the JVM through the flags file (the .hotspotrc file or the file specified by the -XX:Flags= argument).
-Joption
Pass option to the java launcher called by jps. For example, -J-Xms48m sets the startup memory to 48 megabytes. It is a common convention for -J to pass options to the underlying VM executing applications written in Java.

3. Jstat - Java VM stastics Monitor Tool
The jstat tool displays performance statistics for an instrumented HotSpot Java virtual machine (JVM).

-statOption
Determines the statistics information that jstat displays. The following table lists the available options. Use the -options general option to display the list of options for a particular platform installation.

Option Displays...
class Statistics on the behavior of the class loader.
compiler Statistics of the behavior of the HotSpot Just-in-Time compiler.
gc Statistics of the behavior of the garbage collected heap.
gccapacity Statistics of the capacities of the generations and their corresponding spaces.
gccause Summary of garbage collection statistics (same as -gcutil), with the cause of the last and current (if applicable) garbage collection events.
gcnew Statistics of the behavior of the new generation.
gcnewcapacity Statistics of the sizes of the new generations and its corresponding spaces.
gcold Statistics of the behavior of the old and permanent generations.
gcoldcapacity Statistics of the sizes of the old generation.
gcpermcapacity Statistics of the sizes of the permanent generation.
gcutil Summary of garbage collection statistics.
printcompilation HotSpot compilation method statistics.

-h n
Display a column header every n samples (output rows), where n is a positive integer. Default value is 0, which displays the column header above the first row of data.
-t n
Display a timestamp column as the first column of output. The timestamp is the the time since the start time of the target JVM.
-JjavaOption
Pass javaOption to the java application launcher. For example, -J-Xms48m sets the startup memory to 48 megabytes. For a complete list of options, see java - the Java application launcher


举例:
>jps
3672
2884 PluginMain
2648 LauncherServlet
2312
5800 Jps

>jstat -compiler 2648 5s 3

4. Jinfo
显示 Java 配置信息
prints Java configuration information for a given Java process or core file or a remote debug server. Configuration information includes Java System properties and Java virtual machine command line flags.
注意:目前 不支持 linux/Windows

5. Jstack
Statck Trace
jstack prints Java stack traces of Java threads for a given Java process or core file or a remote debug server
ex. jstatck -l 2648

6. HPROF - a heap/cpu Profilling Tool
hprof usage: java -agentlib:hprof=[help]|[