-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的数量和占用内存数也会打印出来.
1、 -heap
打印当前heap的概要信息:heap的总量,年轻代,老年代,Perm Generation的量
GC使用的算法
heap的配置:总量
当前heap的使用情况:占用量,总量,百分比
.
sudo jmap -heap 27409
输出:
Attaching to process ID 27409, please wait... Debugger attached successfully. Server compiler detected. JVM version is 24.45-b08 using thread-local object allocation. Parallel GC with 4 thread(s) Heap Configuration: MinHeapFreeRatio = 40 MaxHeapFreeRatio = 70 MaxHeapSize = 2147483648 (2048.0MB) NewSize = 268435456 (256.0MB) MaxNewSize = 17592186044415 MB OldSize = 5439488 (5.1875MB) NewRatio = 2 SurvivorRatio = 8 PermSize = 268435456 (256.0MB) MaxPermSize = 268435456 (256.0MB) G1HeapRegionSize = 0 (0.0MB) Heap Usage: PS Young Generation Eden Space: capacity = 704643072 (672.0MB) used = 429546320 (409.6472930908203MB) free = 275096752 (262.3527069091797MB) 60.959418614705406% used From Space: capacity = 5767168 (5.5MB) used = 4327344 (4.1268768310546875MB) free = 1439824 (1.3731231689453125MB) 75.03412420099431% used To Space: capacity = 5767168 (5.5MB) used = 0 (0.0MB) free = 5767168 (5.5MB) 0.0% used PS Old Generation capacity = 1431830528 (1365.5MB) used = 574251648 (547.6490478515625MB) free = 857578880 (817.8509521484375MB) 40.106118480524536% used PS Perm Generation capacity = 268435456 (256.0MB) used = 72128064 (68.78668212890625MB) free = 196307392 (187.21331787109375MB) 26.869797706604004% used 31742 interned Strings occupying 3432376 bytes.
2、-histo[:live] 打印每个class的实例数目,内存占用,类全名信息. VM的内部类名字开头会加上前缀”*”. 如果live子参数加上后,只统计活的对象数量.
sudo jmap -histo 27409>a.log
输出:
Object Histogram: num #instances #bytes Class description -------------------------------------------------------------------------- 1: 164294 173445488 byte[] 2: 4426453 106234872 java.util.concurrent.ConcurrentSkipListMap$Node 3: 448524 74384360 char[] 4: 2215688 53176512 java.lang.Double 5: 96356 33816408 int[] 6: 965084 23162016 java.util.concurrent.ConcurrentSkipListMap$Index 7: 125211 18655880 * ConstMethodKlass 8: 733892 17613408 java.lang.Long 9: 125211 16039712 * MethodKlass 10: 444730 14231360 java.util.HashMap$Entry 11: 11617 13094656 * ConstantPoolKlass 12: 426560 10237440 java.lang.String 13: 11611 8346592 * InstanceKlassKlass 14: 115808 7411712 com.mysql.jdbc.ConnectionPropertiesImpl$BooleanConnectionProperty 15: 9526 7374880 * ConstantPoolCacheKlass 。。。。
另外继续分析日志,每个对象存在的数量以及占用内存的大小:
grep "qunar" data.txt |grep "afare"
输出:
17: 28137 4051728 com.gym.afare.dubbo.model.AfareBlackList 664: 30 1680 com.gym.afare.dubbo.model.AfareBlackListPlus 977: 10 720 com.gym.flight.afare.model.RefreshCmRequest 1055: 12 576 com.gym.flight.afare.model.ErrorEnum 1099: 6 528 com.gym.afare.dubbo.model.request.AfareResultRequest 1146: 12 480 com.gym.flight.afare.model.RefreshTask 1151: 10 480 com.gym.flight.afare.model.RefreshPriceRequest 1206: 13 416 com.gym.flight.afare.model.ApiResponse 1480: 4 224 com.gym.afare.dubbo.model.AfareBlackList$FlightType[] 1544: 5 200 com.gym.afare.dubbo.model.AfareBlackList$UnLockTypeEnum 1574: 3 192 com.gym.afare.dubbo.model.AfareBlackList$UnLockTypeEnum[] 1596: 4 192 com.gym.afare.dubbo.model.AfareBlackList$FlightType 1666: 3 168 com.gym.afare.dubbo.model.AfareBlackList$LockTypeEnum[] 1841: 4 160 com.gym.afare.dubbo.model.AfareBlackList$LockTypeEnum 2015: 3 120 com.gym.afare.dubbo.model.AfareBlackList$LockStatusEnum[] 2022: 1 120 com.gym.flight.afare.model.ErrorEnum[] 2305: 2 80 com.gym.afare.dubbo.model.AfareBlackList$LockStatusEnum 2669: 1 56 com.gym.flight.afare.task.DeleteUnlockBlackListTask 2713: 1 48 com.gym.flight.afare.dubbo.provider.AfareBlackListServiceImpl 2952: 1 40 com.gym.flight.afare.dao.impl.AfareBlackListDAOImpl 2961: 1 40 com.gym.flight.afare.service.impl.RefreshPriceServiceImpl 3012: 1 40 com.gym.flight.afare.service.impl.RefreshPriceServiceImpl$RefreshThreadFactory 3036: 1 40 com.gym.flight.afare.dao.impl.AfareBlackListPlusDAOImpl 3182: 1 32 com.gym.flight.afare.dubbo.provider.AfareResultQueryServiceImpl 3197: 1 32 com.gym.flight.afare.dao.CacheManager 3282: 1 32 com.gym.flight.afare.dao.impl.AfareResultDaoImpl 3289: 1 32 com.gym.flight.afare.dubbo.provider.AfareResultUpdateServiceImpl 3292: 1 32 com.gym.flight.afare.service.Qc 3297: 1 32 com.gym.flight.afare.web.RefreshPriceController 3476: 1 24 com.gym.flight.afare.model.TaskListener 3479: 1 24 com.gym.flight.afare.task.ReloadBlacklistCacheTask 3543: 1 24 com.gym.flight.afare.task.ArchiveAfareResultTask 3676: 1 24 com.gym.flight.afare.dao.CacheManager$1 3720: 1 24 com.gym.flight.afare.web.BackController 4058: 1 16 com.gym.flight.afare.web.interceptor.TraceInterceptor 4119: 1 16 com.gym.flight.afare.util.SpringUtil
从中不难看出,SpringUtil 等类都是单例的,所以只有一个对象,而AfareBlackList 是系统内的对象,数量存在较多
3、-dump:[live,]format=b,file=<filename> 使用hprof二进制形式,输出jvm的heap内容到文件=. live子选项是可选的,假如指定live选项,那么只输出活的对象到文件.
sudo jmap -F -dump:live,format=b,file=heap.bin 27409
输出到heap.bin文件里
eg
class C{ byte b = 100; } class B { List<C[]> d = new ArrayList<C[]>(); public B() { for (int i = 0; i < 100000; i++) { d.add(new C[i]); } } } class A { List<B> bList = new ArrayList<B>(); public A() { for (int i = 0; i < 1000; i++) { bList.add(new B()); } } } public class Test { public static void main(String[] args) throws Exception { A a = new A(); new Thread() {}.start(); a.toString(); } }
运行参数:
-server -Xms1024m -Xmx1024m -Xmn512m -XX:+UseParallelOldGC -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=c:/heap.bin
![](http://dl2.iteye.com/upload/attachment/0119/1717/a3897ebe-22c9-39f4-86ef-8b8089be813d.jpg)
![](http://dl2.iteye.com/upload/attachment/0119/1719/33710d9f-ef7f-3c9d-ae11-26a43d03b98d.jpg)
其他:
-permstat 打印classload和jvm heap长久层的信息. 包含每个classloader的名字,活泼性,地址,父classloader和加载的class数量. 另外,内部String的数量和占用内存数也会打印出来.
-dump:[live,]format=b,file=<filename> 使用hprof二进制形式,输出jvm的heap内容到文件=. live子选项是可选的,假如指定live选项,那么只输出活的对象到文件.
参考资料:
http://www.tuicool.com/articles/VzYziu