java memory viewing and analysis

Quote: http://jameswxx.iteye.com/blog/731763

 

There are many powerful java profile tools in the industry, such as Jporfiler and yourkit. I don't want to talk about these charging things. What I want to say is that Java itself provides many small tools for memory monitoring. The tools listed below are only a small one. Part, carefully study the tools of jdk, it is quite interesting :)

   

1: gc log output

       Add -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimestamps -XX:+PrintGCApplicationStopedTime to the jvm startup parameters, and the jvm will output gc summary information, detailed information, gc time information, and applications caused by gc in the order of these parameters. Pause time. If the parameter -Xloggc: file path is added after the previous parameter, the gc information will be output to the specified file. Other parameters are

-verbose:gc and -XX:+PrintTenuringDistribution etc.

 

2:jconsole

      jconsole is a memory analysis tool that comes with jdk, which provides a graphical interface. You can view the memory information, thread information, class loading information, and MBean information of the monitored jvm.

      jconsole is located in the bin directory under the jdk directory. It is jconsole.exe under windows and jconsole.sh under unix and linux. jconsole can monitor local applications and remote applications. To monitor the local application, execute jconsole pid, which is the id of the running java process. If the pid parameter is not included, after executing the jconsole command, a dialog box will pop up, which lists the local java processes, you can choose one monitor. If you want to monitor remotely, you need to add something to the jvm parameter of the remote server, because the remote monitoring of jconsole is based on jmx. For the detailed usage of jconsole, please refer to the article dedicated to jconsle, and I will also introduce jconsole in detail in the blog. .

 

3:jviusalvm

      After JDK6 update 7, jdk launched another tool: jvisualvm, java visual virtual machine, which not only provides jconsole similar functions, but also provides jvm memory and cpu real-time diagnosis, as well as manual dump out jvm memory situation, manual execution gc.

     Like jconsole, run jviusalvm, execute jviusalvm in the bin directory of jdk, jviusalvm.exe under windows, jviusalvm.sh under linux and unix.

 

4:jmap

    jmap is a tool for jvm memory analysis that comes with jdk, located in the bin directory of jdk. Usage of jmap command in jdk1.6:

Html code   Favorite code
  1. Usage:  
  2. jmap -histo <pid> (to connect to running process and print histogram of java object heap   
  3. jmap -dump:<dump-options> <pid>  (to connect to running process and dump java heap)  
  4. dump-options: format=b binary default file=<file>   
  5. dump heap to <file>    
  6. Example: jmap -dump:format=b,file=heap.bin <pid>   

 

    jmap -histo <pid>在屏幕上显示出指定pid的jvm内存状况。以我本机为例,执行该命令,屏幕显示:

Html代码   Favorite code
  1. 1:         24206        2791864  < constMethodKlass >    
  2. 2:         22371        2145216  [C   
  3. 3:         24206        1940648  < methodKlass >    
  4. 4:          1951        1364496  < constantPoolKlass >    
  5. 5:         26543        1282560  < symbolKlass >    
  6. 6:          6377        1081744  [B   
  7. 7:          1793         909688  < constantPoolCacheKlass >    
  8. 8:          1471         614624  < instanceKlassKlass >    
  9. 9:         14581         548336  [Ljava.lang.Object;   
  10. 10:          3863         513640  [I   
  11. 11:         20677         496248  java.lang.String      
  12. 12:          3621         312776  [Ljava.util.HashMap$Entry;   
  13. 13:          3335         266800  java.lang.reflect.Method   
  14. 14:          8256         264192  java.io.ObjectStreamClass$WeakClassKey   
  15. 15:          7066         226112  java.util.TreeMap$Entry   
  16. 16:          2355         173304  [S   
  17. 17:          1687         161952  java.lang.Class   
  18. 18:          2769         150112  [[I   
  19. 19:          3563         142520  java.util.HashMap   
  20. 20:          5562         133488  java.util.HashMap$Entry   
  21. Total        239019       17140408   

  为了方便查看,我删掉了一些行。从上面的信息很容易看出,#instance指的是对象数量,#bytes指的是这些对象占用的内存大小,class name指的是对象类型。

     再看jmap的dump选项,这个选项是将jvm的堆中内存信息输出到一个文件中,在我本机执行

jmap -dump:file=c:\dump.txt 340  

注意340是我本机的java进程pid,dump出来的文件比较大有10几M,而且我只是开了tomcat,跑了一个很简单的应用,且没有任何访问,可以想象,大型繁忙的服务器上,dump出来的文件该有多大。需要知道的是,dump出来的文件信息是很原始的,绝不适合人直接观看,而jmap -histo显示的内容又太简单,例如只显示某些类型的对象占用多大内存,以及这些对象的数量,但是没有更详细的信息,例如这些对象分别是由谁创建的。那这么说,dump出来的文件有什么用呢?当然有用,因为有专门分析jvm的内存dump文件的工具。

 

5:jhat

    上面说了,有很多工具都能分析jvm的内存dump文件,jhat就是sun jdk6及以上版本自带的工具,位于jdk的bin目录,执行 jhat -J -Xmx512m [file] ,file就是dump文件路径。jhat内置一个简单的web服务器,此命令执行后,jhat在命令行里显示分析结果的访问地址,可以用-port选项指定端口,具体用法可以执行jhat -heap查看帮助信息。访问指定地址后,就能看到页面上显示的信息,比jmap -histo命令显示的丰富得多,更为详细。

 

6:eclipse内存分析器

    上面说了jhat,它能分析jvm的dump文件,但是全部是文字显示,eclipse memory analyzer,是一个eclipse提供用于分析jvm 堆dump的插件,网址为 http://www.eclipse.org/mat ,它的分析速度比jhat快,分析结果是图形界面显示,比jhat的可读性更高。其实jvisualvm也可以分析dump文件,也是有图形界面显示的。

 

7:jstat

      如果说jmap倾向于分析jvm内存中对象信息的话,那么jsta就是倾向于分析jvm内存的gc情况。都是jvm内存分析工具,但显然,它们是从不同维度来分析的。jsat常用的参数有很多,如 -gc,-gcutil,-gccause,这些选项具体作用可查看jsat帮助信息,我经常用-gcutil,这个参数的作用不断的显示当前指定的jvm内存的垃圾收集的信息。

      我在本机执行 jstat -gcutil 340 10000,这个命令是每个10秒钟输出一次jvm的gc信息,10000指的是间隔时间为10000毫秒。屏幕上显示如下信息(我只取了第一行,因为是按的一定频率显示,所以实际执行的时候,会有很多行):

  S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT   
 54.62   0.00  42.87  43.52  86.24   1792    5.093    33    7.670   12.763

        额。。。怎么说呢,要看懂这些信息代表什么意思,还必须对jvm的gc机制有一定的了解才行啊。其实如果对sun的 hot spot jvm的gc比较了解的人,应该很容易看懂这些信息,但是不清楚gc机制的人,有点莫名其妙,所以在这里我还是先讲讲sun的jvm的gc机制吧。说到gc,其实不仅仅只是java的概念,其实在java之前,就有很多语言有gc的概念了,gc嘛就是垃圾收集的意思,更多的是一种算法性的东西,而跟具体语言没太大关系,所以关于gc的历史,gc的主流算法我就不讲了,那扯得太远了,扯得太远了就是扯淡。sun现在的jvm,内存的管理模型是分代模型,所以gc当然是分代收集了。分代是什么意思呢?就是将对象按照生命周期分成三个层次,分别是:新生代,旧生代,持久代。对象刚开始分配的时候,大部分都在新生代,当新生代gc提交被触发后了,执行一次新生代范围内的gc,这叫minor gc,如果执行了几次minor gc后,还有对象存活,将这些对象转入旧生代,因为这些对象已经经过了组织的重重考验了哇。旧生代的gc频率会更低一些,如果旧生代执行了gc,那就是full gc,因为不是局部gc,而是全内存范围的gc,这会造成应用停顿,因为全内存收集,必须封锁内存,不许有新的对象分配到内存,持久代就是一些jvm期间,基本不会消失的对象,例如class的定义,jvm方法区信息,例如静态块。需要主要的是,新生代里又分了三个空间:eden,susvivor0,susvivor1,按字面上来理解,就是伊甸园区,幸存1区,幸存2区。新对象分配在eden区中,eden区满时,采用标记-复制算法,即检查出eden区存活 的对象,并将这些对象复制到是s0或s1中,然后清空eden区。jvm的gc说开来,不只是这么简单,例如还有串行收集,并行收集,并发收集,还有著名的火车算法,不过那说得太远了,现在对这个有大致了解就好。说到这里,再来看一下上面输出的信息:

   S0 S1 E O P YGC YGCT FGC FGCT GCT   
 54.62 0.00 42.87 43.52 86.24 1792 5,093 33 7,670 12,763

S0: The susvivor0 area of ​​the new generation, the space utilization rate is 54..62%

S1: The susvivor1 area of ​​the new generation, the space utilization rate is 0.00% (because the second minor collection has not been performed)

E:eden area, the space utilization rate is 42.87%

O: Old generation, the space utilization rate is 43.52%

P: Persistent belt, space utilization rate 86.24%

YGC: minor gc execution times 1792 times

YGCT: Minor gc takes 5.093 ms

FGC: full gc execution times 33

FGCT: 7.670 ms for full gc

GCT: The total time spent by gc is 12.763 milliseconds

 

 How to choose a tool

     Some of the tools listed above have their own pros and cons. In fact, if you are in a development environment, it doesn't matter what kind of tools you use, as long as you can get results. However, in a production environment, you cannot choose randomly, because these tools will consume a lot of system resources. If you rashly execute these tools when a production server is under great pressure, it may cause unexpected situations. It is best not to monitor locally on the server. Remote monitoring is better. However, if you want to monitor remotely, you need to add some jvm parameters to the server-side startup script. For example, using jconsloe to remotely monitor tomcat or jboss, etc., you need to set the jmx parameter of jvm. If you just analyze the server's memory allocation and gc information, it is strongly recommended to use jmap to export the server-side jvm heap dump file, and then use jhat, or jvisualvm, or eclipse memory analyzer to analyze the memory status.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326392418&siteId=291194637