通过jmap查看jvm采用的垃圾收集器
1 tomcat 的PID获得
ps -ef|grep tomcat
1 [root@iZ2zeapch8kbaw4bxnz8vxZ tomcat7]# ps -ef|grep tomcat 2 root 13296 1 0 10:35 pts/1 00:00:00 /usr/sbin/cronolog /usr/local/tomcat7/logs/catalina.%Y-%m-%d.out 3 root 13297 13295 87 10:35 pts/1 00:00:12 /usr/local/jdk1.8.0_77/bin/java -Djava.util.logging.config.file=/usr/local/tomcat7/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Xms1024m -Xmx1024m -Xss512K -XX:+UseConcMarkSweepGC -Djdk.tls.ephemeralDHKeySize=2048 -Djava.endorsed.dirs=/usr/local/tomcat7/endorsed -classpath /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat7 -Dcatalina.home=/usr/local/tomcat7 -Djava.io.tmpdir=/usr/local/tomcat7/temp org.apache.catalina.startup.Bootstrap start 4 root 13434 12792 0 10:35 pts/1 00:00:00 grep tomcat
2 查看java垃圾收集器 jmap -heap pid
其中 using thread-local object allocation下面就是采用的java垃圾收集器
下图事例中为:Concurrent Mark-Sweep GC
[root@iZ2zeapchxZ tomcat7]# jmap -heap 13297 Attaching to process ID 13297, please wait... Debugger attached successfully. Server compiler detected. JVM version is 25.77-b03 using parallel threads in the new generation. using thread-local object allocation. Concurrent Mark-Sweep GC Heap Configuration: MinHeapFreeRatio = 40 MaxHeapFreeRatio = 70 MaxHeapSize = 1073741824 (1024.0MB) NewSize = 87228416 (83.1875MB) MaxNewSize = 87228416 (83.1875MB) OldSize = 986513408 (940.8125MB) NewRatio = 2 SurvivorRatio = 8 MetaspaceSize = 21807104 (20.796875MB) CompressedClassSpaceSize = 1073741824 (1024.0MB) MaxMetaspaceSize = 17592186044415 MB G1HeapRegionSize = 0 (0.0MB) Heap Usage: New Generation (Eden + 1 Survivor Space): capacity = 78512128 (74.875MB) used = 17016048 (16.227767944335938MB) free = 61496080 (58.64723205566406MB) 21.673145835507096% used Eden Space: capacity = 69795840 (66.5625MB) used = 10923608 (10.417564392089844MB) free = 58872232 (56.144935607910156MB) 15.650800964642018% used From Space: capacity = 8716288 (8.3125MB) used = 6092440 (5.810203552246094MB) free = 2623848 (2.5022964477539062MB) 69.89718559093045% used To Space: capacity = 8716288 (8.3125MB) used = 0 (0.0MB) free = 8716288 (8.3125MB) 0.0% used concurrent mark-sweep generation: capacity = 986513408 (940.8125MB) used = 41311272 (39.397499084472656MB) free = 945202136 (901.4150009155273MB) 4.187603702594583% used 19916 interned Strings occupying 2422360 bytes.
Concurrent Mark-Sweep GC :CMS回收器
Mark Sweep Compact GC: 串行GC(Serial GC)
Parallel GC with 2 thread(s): 并行GC(ParNew)
如何修改GC回收器: 设置 JAVA_OPTS
3、JMAP介绍
打印出某个java进程(使用pid)内存内的,所有‘对象’的情况(如:产生那些对象,及其数量)。
可以输出所有内存中对象的工具,甚至可以将VM 中的heap,以二进制输出成文本。使用方法 jmap -histo pid。如果连用SHELL jmap -histo pid>a.log可以将其保存到文本中去,在一段时间后,使用文本对比工具,可以对比出GC回收了哪些对象。jmap -dump:format=b,file=outfile 3024可以将3024进程的内存heap输出出来到outfile文件里,再配合MAT(内存分析工具(Memory Analysis Tool),使用参见:http://blog.csdn.net/fenglibing/archive/2011/04/02/6298326.aspx)或与jhat (Java Heap Analysis Tool)一起使用,能够以图像的形式直观的展示当前内存是否有问题。
64位机上使用需要使用如下方式:
jmap -J-d64 -heap pid
2、命令格式
SYNOPSIS
jmap [ option ] pid
jmap [ option ] executable core
jmap [ option ] [server-id@]remote-hostname-or-IP
3、参数说明
1)、options:
executable Java executable from which the core dump was produced.
(可能是产生core dump的java可执行程序)
core 将被打印信息的core dump文件
remote-hostname-or-IP 远程debug服务的主机名或ip
server-id 唯一id,假如一台主机上多个远程debug服务
2)、基本参数:
-dump:[live,]format=b,file=<filename> 使用hprof二进制形式,输出jvm的heap内容到文件=. live子选项是可选的,假如指定live选项,那么只输出活的对象到文件.
-finalizerinfo 打印正等候回收的对象的信息.
-heap 打印heap的概要信息,GC使用的算法,heap的配置及wise heap的使用情况.
-histo[:live] 打印每个class的实例数目,内存占用,类全名信息. VM的内部类名字开头会加上前缀”*”. 如果live子参数加上后,只统计活的对象数量.
-permstat 打印classload和jvm heap长久层的信息. 包含每个classloader的名字,活泼性,地址,父classloader和加载的class数量. 另外,内部String的数量和占用内存数也会打印出来.
-F 强迫.在pid没有相应的时候使用-dump或者-histo参数. 在这个模式下,live子参数无效.
-h | -help 打印辅助信息
-J 传递参数给jmap启动的jvm.
pid 需要被打印配相信息的java进程id,可以用jps查问.
4、使用示例
jmap -histo 4939
[输出较多这里不贴了]
2)jmap -dump:format=b,file=test.bin 4939
Dumping heap to /home/fenglb/test.bin ...
Heap dump file created
4 jmap -histo pid|head -n 30
num #instances #bytes class name ---------------------------------------------- 1: 117098 170232696 [B 2: 204755 121325072 [I 3: 651042 82624448 [C 4: 414175 9940200 java.lang.String 5: 141560 8732760 [Ljava.lang.Object; 6: 40748 3585824 java.lang.reflect.Method 7: 78318 2506176 java.util.HashMap$Node 8: 30303 2477016 [S 9: 21451 1962016 [Ljava.util.HashMap$Node; 10: 54391 1855488 [Ljava.lang.String; 11: 23772 1521408 java.util.regex.Matcher 12: 53136 1275264 java.util.ArrayList 13: 29266 1170640 java.util.LinkedHashMap$Entry 14: 23997 1151856 java.util.StringTokenizer 15: 34861 1115552 java.awt.Color 16: 31430 1005760 java.lang.ref.WeakReference 17: 17900 1002400 org.apache.jasper.compiler.Mark 18: 41636 999264 java.lang.StringBuilder 19: 30441 974112 java.util.concurrent.ConcurrentHashMap$Node 20: 19761 948528 java.util.HashMap 21: 8217 931240 java.lang.Class 22: 41439 892720 [Ljava.lang.Class; 23: 13799 772744 java.util.LinkedHashMap 24: 10456 752832 java.util.regex.Pattern 25: 9480 682560 org.eclipse.jdt.internal.compiler.flow.UnconditionalFlowInfo 26: 19754 632128 java.util.Stack 27: 14611 621080 [[C
其中:
[C is a char[]
[S is a short[]
[I is a int[]
[B is a byte[]
[[I is a int[][]