1.jinfo
描述:输出给定 java 进程所有的配置信息。包括 java 系统属性和 jvm 命令行标记等。
用法:
jinfo [ option ] pid
jinfo [ option ] executable core
jinfo [ option ] [server-id@]remote-hostname-or-IP
例子:
jinfo pid
得到结果如下:
- JVM version is 14.0-b16
- Java System Properties:
- java.runtime.name = Java(TM) SE Runtime Environment
- sun.boot.library.path = /work/ProgramFiles/jdk1.6.0_14/jre/lib/i386
- java.vm.version = 14.0-b16
- java.vm.vendor = Sun Microsystems Inc.
- java.vendor.url = http://java.sun.com/
- path.separator = :
- java.vm.name = Java HotSpot(TM) Server VM
- file.encoding.pkg = sun.io
- sun.java.launcher = SUN_STANDARD
- user.country = CN
- sun.os.patch.level = unknown
- java.vm.specification.name = Java Virtual Machine Specification
- user.dir = /work/RateWorkspace/branches/rate/daemon
- java.runtime.version = 1.6.0_14-b08
- java.awt.graphicsenv = sun.awt.X11GraphicsEnvironment
- java.endorsed.dirs = /work/ProgramFiles/jdk1.6.0_14/jre/lib/endorsed
- os.arch = i386
- java.io.tmpdir = /tmp
- line.separator =
- java.vm.specification.vendor = Sun Microsystems Inc.
- os.name = Linux
- sun.jnu.encoding = UTF-8
- java.library.path = /work/ProgramFiles/jdk1.6.0_14/jre/lib/i386/server:/work/ProgramFiles/jdk1.6.0_14/jre/lib/i386:/work/ProgramFiles/jdk1.6.0_14/jre/../lib/i386:/usr/lib/jvm/java-6-openjdk/jre/lib/i386/client:/usr/lib/jvm/java-6-openjdk/jre/lib/i386:/usr/lib/xulrunner-addons:/usr/lib/xulrunner-addons:/usr/java/packages/lib/i386:/lib:/usr/lib
- java.specification.name = Java Platform API Specification
- java.class.version = 50.0
- sun.management.compiler = HotSpot Tiered Compilers
- os.version = 2.6.28-19-generic
- user.home = /home/yblin
- user.timezone = PRC
- java.awt.printerjob = sun.print.PSPrinterJob
- file.encoding = GBK
- java.specification.version = 1.6
- java.class.path =
- user.name = yblin
- java.vm.specification.version = 1.0
- java.home = /work/ProgramFiles/jdk1.6.0_14/jre
- sun.arch.data.model = 32
- user.language = zh
- java.specification.vendor = Sun Microsystems Inc.
- java.vm.info = mixed mode
- java.version = 1.6.0_14
- java.ext.dirs = /work/ProgramFiles/jdk1.6.0_14/jre/lib/ext:/usr/java/packages/lib/ext
- sun.boot.class.path = /work/ProgramFiles/jdk1.6.0_14/jre/lib/resources.jar:/work/ProgramFiles/jdk1.6.0_14/jre/lib/rt.jar:/work/ProgramFiles/jdk1.6.0_14/jre/lib/sunrsasign.jar:/work/ProgramFiles/jdk1.6.0_14/jre/lib/jsse.jar:/work/ProgramFiles/jdk1.6.0_14/jre/lib/jce.jar:/work/ProgramFiles/jdk1.6.0_14/jre/lib/charsets.jar:/work/ProgramFiles/jdk1.6.0_14/jre/classes
- java.vendor = Sun Microsystems Inc.
- file.separator = /
- java.vendor.url.bug = http://java.sun.com/cgi-bin/bugreport.cgi
- sun.io.unicode.encoding = UnicodeLittle
- sun.cpu.endian = little
- sun.desktop = gnome
- sun.cpu.isalist =
- VM Flags:
- -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:55948 -ea -Dfile.encoding=GBK
这个命令包含了 JDK 和 JVM 运行起来时的一些属性。
2.jmap
描述 :输出给定 java 进程所有的内存使用信息。
使用:
jmap [ option ] pid
jmap [ option ] executable core
jmap [ option ] [server-id@]remote-hostname-or-IP
<no option>
没有可选项时,会输出所有共享的对象映射。
开始地址,映射大小,全路径。
直接 jmap pid得到如下结果:
0x08048000 46K /work/ProgramFiles/jdk1.6.0_14/bin/java
0xb6763000 74K /work/ProgramFiles/jdk1.6.0_14/jre/lib/i386/libzip.so
0xb6774000 41K /lib/tls/i686/cmov/libnss_files-2.9.so
0xb6780000 37K /lib/tls/i686/cmov/libnss_nis-2.9.so
0xb678b000 29K /lib/tls/i686/cmov/libnss_compat-2.9.so
目前还说不是很理解这个执行结果是什么意思。经询问同事了解,是加载的文件的信息。
-heap 选项
jmap -heap pid : 查看堆的配置和大当前各个区块的使用情况,如伊甸区、老年代,永久代。可以用此来判断内存目前的使用情况以及垃圾回收情况:
jmap -heap pid,得到如下结果:
- <strong>Heap Configuration:
- MinHeapFreeRatio = 40
- MaxHeapFreeRatio = 70
- MaxHeapSize = 859832320 (820.0MB)
- NewSize = 1048576 (1.0MB)
- MaxNewSize = 4294901760 (4095.9375MB)
- OldSize = 4194304 (4.0MB)
- NewRatio = 8
- SurvivorRatio = 8
- PermSize = 16777216 (16.0MB)
- MaxPermSize = 67108864 (64.0MB)
- Heap Usage:
- PS Young Generation
- Eden Space:
- capacity = 9043968 (8.625MB)
- used = 6820384 (6.504425048828125MB)
- free = 2223584 (2.120574951171875MB)
- 75.41362375452898% used
- From Space:
- capacity = 720896 (0.6875MB)
- used = 704680 (0.6720352172851562MB)
- free = 16216 (0.01546478271484375MB)
- 97.7505770596591% used
- To Space:
- capacity = 720896 (0.6875MB)
- used = 0 (0.0MB)
- free = 720896 (0.6875MB)
- 0.0% used
- PS Old Generation
- capacity = 47710208 (45.5MB)
- used = 589184 (0.5618896484375MB)
- free = 47121024 (44.9381103515625MB)
- 1.2349223042582418% used
- PS Perm Generation
- capacity = 16777216 (16.0MB)
- used = 5179648 (4.939697265625MB)
- free = 11597568 (11.060302734375MB)
- 30.87310791015625% used
- </strong>
-histo 选项
内存使用的柱状图,包含每个类,对象使用的内存大小。 VM 内部的类名以 * 前缀开头。
- num #instances #bytes class name
- ---------------------------------------------
- 1: 57195 5639792 [C
- 2: 34680 3991104 <constMethodKlass>
- 3: 34680 2779112 <methodKlass>
- 4: 52947 2340272 <symbolKlass>
- 5: 3024 1641744 <constantPoolKlass>
- 6: 5182 1618040 [B
- 7: 3024 1290544 <instanceKlassKlass>
- 8: 12134 1267696 [I
- 9: 44622 1070928 java.lang.String
- 10: 2565 994128 <constantPoolCacheKlass>
- ………………
-permstat 选项
输出所有类装载器在堆里产生的对象
包括每个装载器的名字,活跃,地址,父装载器,和其总共加载的类大小。
jmap -permstat pid
得到如下结果:
- class_loader classes bytes parent_loader alive? type
- <bootstrap> 1102 4283352 null live <internal>
- 0x80753d50 1 1992 null dead sun/reflect/DelegatingClassLoader@0x7c2e6c28
- 0x8034d9e0 8 32272 null live sun/misc/Launcher$ExtClassLoader@0x7c432400
- 0x8095a4e0 1 1784 0x8034d998 dead sun/reflect/DelegatingClassLoader@0x7c2e6c28
- 0xadc25868 1 1784 0x8034d998 dead sun/reflect/DelegatingClassLoader@0x7c2e6c28
- 0xaebf74c0 1 1784 0x8034d998 dead sun/reflect/DelegatingClassLoader@0x7c2e6c28
- 0xadc2b5c8 1 1784 0x8034d998 dead sun/reflect/DelegatingClassLoader@0x7c2e6c28
- 0x808cd448 1 1768 0x8034d998 dead sun/reflect/DelegatingClassLoader@0x7c2e6c28
- 0x80758288 1 1768 0x8034d998 dead sun/reflect/DelegatingClassLoader@0x7c2e6c28
- 0x80959e80 1 1784 0x8034d998 dead sun/reflect/DelegatingClassLoader@0x7c2e6c28
- 0x80756248 1 1768 0x8034d998 dead sun/reflect/DelegatingClassLoader@0x7c2e6c28
- 0x808e6ae8 1 1784 0x8034d998 dead sun/reflect/DelegatingClassLoader@0x7c2e6c28
3.jstack - Stack Trace
为指定的线程输出 java 的线程堆栈信息,包括了进程里的所有线程。每一个线程 frame ,包括类全名,方法名,代码行。
java.lang.Thread.State : RUNNABLE BLOCKED TIMED_WATTING(sleep 后会进入这种状态 ) (如果是 BLOCKED 状态就要注意了,看看 blocked 的状态在等待什么?因为什么而阻塞?)最常见的情况是线程在等待网络的读写,比如当网络数据没有准备好读时,线程处于这种等待状态,而一旦有数据准备好读之后,线程会重新激活,读取并处理数据。
在线程中,有一些 JVM 内部的后台线程,来执行譬如垃圾回收,或者低内存的检测等等任务,这些线程往往在 JVM 初始化的时候就存在,如下所示:
"Low Memory Detector" daemon prio=10 tid=0x081465f8 nid=0x7 runnable [0x00000000..0x00000000]
"CompilerThread0" daemon prio=10 tid=0x08143c58 nid=0x6 waiting on condition [0x00000000..0xfb5fd798]
"Signal Dispatcher" daemon prio=10 tid=0x08142f08 nid=0x5 waiting on condition [0x00000000..0x00000000]
"Finalizer" daemon prio=10 tid=0x08137ca0 nid=0x4 in Object.wait() [0xfbeed000..0xfbeeddb8]
写了个代码, 100 个线程同时往控制台输出,代码如下:
- public class MyThread extends Thread {
- public MyThread(String name){
- super(name);
- }
- @Override
- public void run() {
- super.run();
- while (true) {
- try {
- System.out.println("1");
- } catch (Exception e) {
- }
- }
- }
- public static void main(String[] args) {
- for (int i = 0; i < 100; i++) {
- MyThread my = new MyThread("thrad" + i);
- my.start();
- }
- }
- }
通过 jstack pid, 得到如下部分结果:
- "thrad76" prio=10 tid=0x096ce400 nid=0x4305 waiting for monitor entry [0x7a280000]
- java.lang.Thread.State: BLOCKED (on object monitor)
- at java.io.PrintStream.println(PrintStream.java:755)
- - waiting to lock <0x805782c0> (a java.io.PrintStream)
- at org.jtester.service.MyThread.run(MyThread.java:15)
- "thrad75" prio=10 tid=0x096ccc00 nid=0x4304 runnable [0x7a2d1000]
- java.lang.Thread.State: RUNNABLE
- at java.io.PrintStream.write(PrintStream.java:428)
- - locked <0x805782c0> (a java.io.PrintStream)
- at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:202)
- at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:272)
- at sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:85)
- - locked <0x805783c8> (a java.io.OutputStreamWriter)
- at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:168)
- at java.io.PrintStream.write(PrintStream.java:477)
- - locked <0x805782c0> (a java.io.PrintStream)
- at java.io.PrintStream.print(PrintStream.java:619)
- at java.io.PrintStream.println(PrintStream.java:756)
- - locked <0x805782c0> (a java.io.PrintStream)
- at org.jtester.service.MyThread.run(MyThread.java:15)
- "thrad74" prio=10 tid=0x096cb000 nid=0x4303 waiting for monitor entry [0x7a322000]
- java.lang.Thread.State: BLOCKED (on object monitor)
- at java.io.PrintStream.println(PrintStream.java:755)
- - waiting to lock <0x805782c0> (a java.io.PrintStream)
- at org.jtester.service.MyThread.run(MyThread.java:15)
可以看到只有线程 thrad75 是 RUNNABLE 状态的,其他的都是 BLOCKED 状态。因为其他的线程在竞争控制台输出的时候都被阻塞了。
其他命令 http://uule.iteye.com/blog/2114292
OOM分析--heapdump
要dump堆的内存镜像,可以采用如下两种方式:
设置JVM参数-XX:+HeapDumpOnOutOfMemoryError,设定当发生OOM时自动dump出堆信息。不过该方法需要JDK5以上版本。
使用JDK自带的jmap命令。"jmap -dump:format=b,file=heap.bin <pid>" 其中pid可以通过jps获取。
dump堆内存信息后,需要对dump出的文件进行分析,从而找到OOM的原因。常用的工具有:
mat: eclipse memory analyzer, 基于eclipse RCP的内存分析工具。详细信息参见:http://www.eclipse.org/mat/,推荐使用。
jhat:JDK自带的java
heap analyze
tool,可以将堆中的对象以html的形式显示出来,包括对象的数量,大小等等,并支持对象查询语言OQL,分析相关的应用后,可以通过http://localhost:7000来访问分析结果。不推荐使用,因为在实际的排查过程中,一般是先在生产环境
dump出文件来,然后拉到自己的开发机器上分析,所以,不如采用高级的分析工具比如前面的mat来的高效。
这个链接:http://www.ibm.com/developerworks/cn/opensource/os-cn-ecl-ma/index.html中提供了一个采用mat分析的例子 。
注意:因为JVM规范没有对dump出的文件的格式进行定义,所以不同的虚拟机产生的dump文件并不是一样的。在分析时,需要针对不同的虚拟机的输出采用不同的分析工具(当然,有的工具可以兼容多个虚拟机的格式)。IBM
HeapAnalyzer也是分析heap的一个常用的工具。
原文链接:[http://wely.iteye.com/blog/2331049]