关于堆内存的诊断,我们主要用下面的工具:
- jps工具,可以查看当前系统中有哪些java进程
- jmap工具,查看堆内存占用的情况
- jconsole工具,图形化的页面还能反映出内存随时间变化的情况
我们用具体的案例来说明工具的使用:
package com.blogs; public class Jvm1_4 { public static void main(String[] args) throws InterruptedException{ System.out.println("step1..."); Thread.sleep(30000); byte[] array=new byte[1024*1024*10];//10M System.out.println("step2..."); Thread.sleep(10000); array=null; System.gc(); System.out.println("stept3..."); Thread.sleep(100000000L); } }
使用jmap内存诊断
我们在linux下启动程序:
java -Xmx70M Jvm1_4
我们用jps可以发现进程:
jps
25526 Jvm1_4
我们在每一次输出的时候分别用jmap去获取信息:
第一次抓取:
jmap -heap 25526
Attaching to process ID 25526, please wait... Debugger attached successfully. Server compiler detected. JVM version is 25.102-b14 using thread-local object allocation. Parallel GC with 18 thread(s) Heap Configuration: MinHeapFreeRatio = 0 MaxHeapFreeRatio = 100 MaxHeapSize = 73400320 (70.0MB) NewSize = 24117248 (23.0MB) MaxNewSize = 24117248 (23.0MB) OldSize = 49283072 (47.0MB) NewRatio = 2 SurvivorRatio = 8 MetaspaceSize = 21807104 (20.796875MB) CompressedClassSpaceSize = 1073741824 (1024.0MB) MaxMetaspaceSize = 17592186044415 MB G1HeapRegionSize = 0 (0.0MB) Heap Usage: PS Young Generation Eden Space: capacity = 18874368 (18.0MB) used = 11618304 (11.080078125MB) free = 7256064 (6.919921875MB) 61.555989583333336% used From Space: capacity = 2621440 (2.5MB) used = 0 (0.0MB) free = 2621440 (2.5MB) 0.0% used To Space: capacity = 2621440 (2.5MB) used = 0 (0.0MB) free = 2621440 (2.5MB) 0.0% used PS Old Generation capacity = 49283072 (47.0MB) used = 0 (0.0MB) free = 49283072 (47.0MB) 0.0% used 724 interned Strings occupying 48560 bytes
第二次抓取:
Attaching to process ID 25526, please wait... Debugger attached successfully. Server compiler detected. JVM version is 25.102-b14 using thread-local object allocation. Parallel GC with 18 thread(s) Heap Configuration: MinHeapFreeRatio = 0 MaxHeapFreeRatio = 100 MaxHeapSize = 73400320 (70.0MB) NewSize = 24117248 (23.0MB) MaxNewSize = 24117248 (23.0MB) OldSize = 49283072 (47.0MB) NewRatio = 2 SurvivorRatio = 8 MetaspaceSize = 21807104 (20.796875MB) CompressedClassSpaceSize = 1073741824 (1024.0MB) MaxMetaspaceSize = 17592186044415 MB G1HeapRegionSize = 0 (0.0MB) Heap Usage: PS Young Generation Eden Space: capacity = 18874368 (18.0MB) used = 11618304 (11.080078125MB) free = 7256064 (6.919921875MB) 61.555989583333336% used From Space: capacity = 2621440 (2.5MB) used = 0 (0.0MB) free = 2621440 (2.5MB) 0.0% used To Space: capacity = 2621440 (2.5MB) used = 0 (0.0MB) free = 2621440 (2.5MB) 0.0% used PS Old Generation capacity = 49283072 (47.0MB) used = 0 (0.0MB) free = 49283072 (47.0MB) 0.0% used 724 interned Strings occupying 48560 bytes.
第三次抓取:
Attaching to process ID 25526, please wait... Debugger attached successfully. Server compiler detected. JVM version is 25.102-b14 using thread-local object allocation. Parallel GC with 18 thread(s) Heap Configuration: MinHeapFreeRatio = 0 MaxHeapFreeRatio = 100 MaxHeapSize = 73400320 (70.0MB) NewSize = 24117248 (23.0MB) MaxNewSize = 24117248 (23.0MB) OldSize = 49283072 (47.0MB) NewRatio = 2 SurvivorRatio = 8 MetaspaceSize = 21807104 (20.796875MB) CompressedClassSpaceSize = 1073741824 (1024.0MB) MaxMetaspaceSize = 17592186044415 MB G1HeapRegionSize = 0 (0.0MB) Heap Usage: PS Young Generation Eden Space: capacity = 18874368 (18.0MB) used = 365240 (0.34832000732421875MB) free = 18509128 (17.65167999267578MB) 1.9351111518012152% used From Space: capacity = 2621440 (2.5MB) used = 0 (0.0MB) free = 2621440 (2.5MB) 0.0% used To Space: capacity = 2621440 (2.5MB) used = 0 (0.0MB) free = 2621440 (2.5MB) 0.0% used PS Old Generation capacity = 49283072 (47.0MB) used = 268808 (0.25635528564453125MB) free = 49014264 (46.74364471435547MB) 0.5454367779670878% used 710 interned Strings occupying 47584 bytes.
我们看到MaxHeapSize 是我们指定的大小,Eden Space在第二次的时候变成了11.080078125MB,第三次抓取的时候释放了,我们可以看到代码中第三次执行了Gc的操作,内存也被释放了,通过jmap的跟踪,我们便可以看到内存信息了。
使用jconsole内存诊断
启动程序,指定jvm参数
程序启动之后,直接在命令行键入命令:
jsonsole • 1
然后弹出界面,我们选择我们的程序名
我们很直观的看到我们的堆内存变化过程
内存变化过程