Java内存管理-监控工具篇

1.JConsole

 

Jconsole,Java Monitoring and Management Console。

java监控和管理控制台,从java5开始,在JDK中提供。

用于对JVM中内存,线程、类和虚拟机的运行情况等的监控。提供了本地监控和远程监控两种功能。

 

 

Java内存管理-监控工具篇_第1张图片

 

 

2.jps

 

[root@host bin]# ./jps

17515 Standalone
1487 Jps

 

 

 

3.jmap

 

jmap是一个可以输出所有内存中对象的工具,甚至可以将VM 中的heap,以二进制输出成文本。使用方法 jmap -histo pid。如果连用 SHELL jmap -histo pid > a.log可以将其保存到文本中去(windows下也可以使用),在一段时间后,使用文本对比工具,可以对比出GC回收了哪些对象。jmap -dump:format=b,file=f1 3024可以将3024进程的内存heap输出出来到f1文件里。 

 

jmap的常用命令:

 

3.1 jmap -finalizerinfo JVM的进程号

这个表示有多少对象正在finalization队列中,等待finalizer thread进行finalizer,对于finalizer的原理,请参考我的博客文章OutOfMemory总汇

例如:

 

[root@tomy tomcat] # /usr/java/latest/bin/jmap -finalizerinfo  25757
Attaching to process ID 25757, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 14.0-b16
Number of objects pending for finalization: 0

 

3.2 jmap -heap JVM的进程号

打印堆的使用情况,以及GC采用什么垃圾回收器

例如:

 

[root@labs-dev01 tomcat]# /usr/java/latest/bin/jmap -heap  25757
Attaching to process ID 25757, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 14.0-b16

using thread-local object allocation.
Parallel GC with 2 thread(s)

Heap Configuration:
   MinHeapFreeRatio = 40
   MaxHeapFreeRatio = 70
   MaxHeapSize      = 402653184 (384.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 = 42598400 (40.625MB)
   used     = 21145576 (20.165992736816406MB)
   free     = 21452824 (20.459007263183594MB)
   49.63936673677885% used
From Space:
   capacity = 1048576 (1.0MB)
   used     = 652736 (0.62249755859375MB)
   free     = 395840 (0.37750244140625MB)
   62.249755859375% used
To Space:
   capacity = 1048576 (1.0MB)
   used     = 0 (0.0MB)
   free     = 1048576 (1.0MB)
   0.0% used
PS Old Generation
   capacity = 357957632 (341.375MB)
   used     = 23543768 (22.453086853027344MB)
   free     = 334413864 (318.92191314697266MB)
   6.577249902021924% used
PS Perm Generation
   capacity = 24903680 (23.75MB)
   used     = 24817640 (23.667945861816406MB)
   free     = 86040 (0.08205413818359375MB)
   99.65450889185855% used

 

3.3 jmap -dump:file=<fileName> JVM的进程号(java5版本不支持这个参数)

[root@labs-dev01 tomcat]# /usr/java/latest/bin/jmap -dump:file=heap.bin  -F 25757

Attaching to process ID 25757, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 14.0-b16
Dumping heap to mashup ...
Finding object size using Printezis bits and skipping over...
Finding object size using Printezis bits and skipping over...
Finding object size using Printezis bits and skipping over...
Finding object size using Printezis bits and skipping over...
Finding object size using Printezis bits and skipping over...
Finding object size using Printezis bits and skipping over...
Finding object size using Printezis bits and skipping over...
Finding object size using Printezis bits and skipping over...
Finding object size using Printezis bits and skipping over...
Finding object size using Printezis bits and skipping over...
Heap dump file created

 

dump下来的二进制文件,需要用专门的工具进行查看,sun提供了jhat工具进行查看:

[root@tomy tomcat]# /usr/java/latest/bin/jhat heap.bin
Reading from heap.bin...
Dump file created Wed Apr 14 08:57:06 GMT+00:00 2010
Snapshot read, resolving...
Resolving 443412 objects...
Chasing references, expect 88 dots........................................................................................
Eliminating duplicate references........................................................................................
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.

 

由上面可以看到,jhat查看器,是启动了7000端口,让客户端可以通过web方式进行查看,dump出来的内容包含:

1)所有在内存中的类信息

2)所有类的实例数目

3)GC的root set

 

客户端可以通过浏览器访问http://ip:7000就可以访问到heap里面的内容了。

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(java,jvm,thread,tomcat,虚拟机)