需求:经常会因为OOM而导致系统挂掉,很多服务无法连接,所以准备了解一下。
参考:http://www.open-open.com/lib/view/open1390916852007.html
一、简介
JDK本身提供了很多方便的JVM性能调优监控工具,除了集成式的VisualVM和jConsole外,还有jps、jstack、jmap、jhat、jstat等小巧的工具。现实企业级Java开发中,有时候我们会碰到下面这些问题:
-
OutOfMemoryError,内存不足
-
内存泄露
-
线程死锁
-
锁争用(Lock Contention)
-
Java进程消耗CPU过高
-
......
这些问题在日常开发中可能被很多人忽视(比如有的人遇到上面的问题只是重启服务器或者调大内存,而不会深究问题根源),但能够理解并解决这些问题是Java程序员进阶的必备要求。
二、jmap
本文主要介绍我自己的jmap学习。jmap用来查看堆内存使用状况,一般结合jhat使用。jmap的预发格式如下:
1 Attaching to process ID 29382, please wait... 2 Debugger attached successfully. 3 Server compiler detected. 4 JVM version is 24.55-b03 5 6 using thread-local object allocation. 7 Parallel GC with 13 thread(s) 8 9 Heap Configuration: 10 MinHeapFreeRatio = 40 11 MaxHeapFreeRatio = 70 12 MaxHeapSize = 23678943232 (22582.0MB) 13 NewSize = 1310720 (1.25MB) 14 MaxNewSize = 17592186044415 MB 15 OldSize = 5439488 (5.1875MB) 16 NewRatio = 2 17 SurvivorRatio = 8 18 PermSize = 21757952 (20.75MB) 19 MaxPermSize = 85983232 (82.0MB) 20 G1HeapRegionSize = 0 (0.0MB) 21 22 Heap Usage: 23 PS Young Generation 24 Eden Space: 25 capacity = 370671616 (353.5MB) 26 used = 44484200 (42.423439025878906MB) 27 free = 326187416 (311.0765609741211MB) 28 12.000972850319352% used 29 From Space: 30 capacity = 61341696 (58.5MB) 31 used = 0 (0.0MB) 32 free = 61341696 (58.5MB) 33 0.0% used 34 To Space: 35 capacity = 61341696 (58.5MB) 36 used = 0 (0.0MB) 37 free = 61341696 (58.5MB) 38 0.0% used 39 PS Old Generation 40 capacity = 986185728 (940.5MB) 41 used = 0 (0.0MB) 42 free = 986185728 (940.5MB) 43 0.0% used 44 PS Perm Generation 45 capacity = 22020096 (21.0MB) 46 used = 5771216 (5.5038604736328125MB) 47 free = 16248880 (15.496139526367188MB) 48 26.208859398251487% used 49 50 2957 interned Strings occupying 217312 bytes.