jvm实战

11:45 [[email protected]]$ jstat -class 24837              
Loaded  Bytes  Unloaded  Bytes     Time   
  4779 10066.7        0     0.0       3.54
tty:[0] jobs:[0] cwd:[~]
11:45 [[email protected]]$ jinfo -flag MaxPermSize 24837          
-XX:MaxPermSize=536870912

11:57 [[email protected]]$ jmap -dump:format=b,file=jmapformat.txt 24837
Dumping heap to /home/cafe/jmapformat.txt ...
Heap dump file created
tty:[0] jobs:[0] cwd:[~]
12:00 [[email protected]]$ more jmapformat.txt 
JAVA PROFILE 1.0.1kson/map/JsonSerializer;)VoyObjectteInsertRowWithDefaultValues/springframework/core/ResolvableType; switch to standist<Lorg/apache/zookeeper/ClientCnxn$Packet;>;Z)Lorg/apache/zookeeper/ClientCnxn$Packet;ject;>;ng/Class<*>;Ljava/lang/Class<*>;)Vr;ema-typeing$Entry;)IData;peSQLring;Ljava/io/File;)Ljava/lang/Process;
mer Time
Lorg/apache/zookeeper/proto/RequestHeader;Lorg/apache/zookeeper/proto/ReplyHeader;Lorg/apache/jute/Record;Lorg/apache/jute/Record;Lorg/apa
che/zookeeper/AsyncCallback;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;Lorg/apache/zookeeper/ZooKeeper$WatchRegistration;)Lorg/
am;)V/zookeeper/ClientCnxn$Packet;--More--(0%)
ar:' URL: (0%)teArrayPropertyEditor--More--(0%)

e.org/xml/properties/rties: --More--(0%)Object;JJ)Ljava/util/Set;--More--(0%)
/jackson/map/ser/std/SerializerBase<TT;>;/management/ObjectName$Property;>;II)V--More--(0%)
TMLManagerServlet$7%)rvlet$4--More--(0%)ersers/XSDSimpleTypeTraverser;--More--(0%)

jmap-dump:live,format=b,file=heap.txt24837

jmap-dump:format=b,file=jmapformat.txt24837

12:20[[email protected]]$jhatheap.txt

Readingfromheap.txt...

DumpfilecreatedTueJan1212:20:18CST2016

Snapshotread,resolving...

Resolving244567objects...

Chasingreferences,expect48dots................................................

Eliminatingduplicatereferences................................................

Snapshotresolved.

StartedHTTPserveronport7000

Serverisready.

1、top

2、shift+H在终端1,按下“H”键或者“shift+h”,top视图会切换到线程视图,其中PID是线程号

nginx150269m29m924S0.70.0133:43.06nginx

29077nginx150269m28m872S0.30.0134:12.98nginx1root15010368684572S0.00.00:01.25init

2rootRT-5000S0.00.00:10.92migration/03root3419000S0.00.00:00.00ksoftirqd/0

4rootRT-5000S0.00.00:00.00watchdog/05rootRT-5000S0.00.00:12.51migration/1

6root3419000S0.00.00:00.00ksoftirqd/17rootRT-5000S0.00.00:00.00watchdog/1

8rootRT-5000S0.00.00:03.06migration/29root3419000S0.00.00:00.67ksoftirqd/2

10rootRT-5000S0.00.00:00.00watchdog/211rootRT-5000S0.00.00:02.12migration/3

12root3419000S0.00.00:01.00ksoftirqd/313rootRT-5000S0.00.00:00.00watchdog/3

14rootRT-5000S0.00.00:01.83migration/415root3419000S0.00.00:00.06ksoftirqd/4

16rootRT-5000S0.00.00:00.00watchdog/417rootRT-5000S0.00.00:00.73migration/5

18root3419000S0.00.00:00.04ksoftirqd/519rootRT-5000S0.00.00:00.00watchdog/5

20rootRT-5000S0.00.00:01.28migration/621root3419000S0.00.00:00.03ksoftirqd/6

22rootRT-5000S0.00.00:00.00watchdog/623rootRT-5000S0.00.00:00.52migration/7

24root3419000S0.00.00:00.04ksoftirqd/725rootRT-5000S0.00.00:00.00watchdog/7

26root10-5000S0.00.00:01.91events/027root10-5000S0.00.00:49.45events/1

28root10-5000S0.00.00:04.01events/229root10-5000S0.00.00:00.87events/3

30root10-5000S0.00.00:01.08events/431root10-5000S0.00.00:00.06events/5

tty:[1]jobs:[0]cwd:[/opt/logs/nginx/access]

需要将top命令展示的线程号转换为16进制,以15100为例,在linux下输入命令:printf0x%x15100,得到15100的十六进制为0x3afc

13:25[[email protected]]$printf0x%x27698

0x6c32

jstack27698>jstack27698.txt

与nid一致即找到

使用top-H-pPID命令查看对应进程是哪个线程占用CPU过高

top-13:31:47up252days,2:51,1user,loadaverage:0.07,0.10,0.09

Tasks:68total,0running,68sleeping,0stopped,0zombie

Cpu(s):0.3%us,0.1%sy,0.0%ni,99.3%id,0.2%wa,0.0%hi,0.0%si,0.0%st

Mem:65995412ktotal,39238540kused,26756872kfree,208128kbuffers

Swap:4192956ktotal,0kused,4192956kfree,36363388kcached

PIDUSERPRNIVIRTRESSHRS%CPU%MEMTIME+COMMAND

27698cafe1805050m287m10mS0.00.40:00.00java

27701cafe1805050m287m10mS0.00.40:06.00java

27702cafe1505050m287m10mS0.00.40:00.11java

27703cafe1505050m287m10mS0.00.40:00.09java

27704cafe1605050m287m10mS0.00.40:00.11java

27705cafe1505050m287m10mS0.00.40:00.07java

查看线程信息

13:31[[email protected]]$ps-mp27698-oTHREAD,tid,time

USER%CPUPRISCNTWCHANUSERSYSTEMTIDTIME

cafe1.4------00:00:57

cafe0.021-futex_--2769800:00:00

cafe0.121----2770100:00:06

cafe0.024-futex_--2770200:00:00

cafe0.024-futex_--2770300:00:00

cafe0.023-futex_--2770400:00:00

cafe0.024-184466--2770500:00:00

cafe0.024-futex_--2770600:00:00

cafe0.024-184466--2770700:00:00

cafe0.024-184466--2770800:00:00

cafe0.024-184466--2770900:00:00

cafe0.017-184466--2771000:00:00

cafe0.020-futex_--2771100:00:00

http://my.oschina.net/jccpp/blog/129784

jmap
打印出某个java进程(使用pid)内存内的,所有‘对象’的情况(如:产生那些对象,及其数量)。

可以输出所有内存中对象的工具,甚至可以将VM 中的heap,以二进制输出成文本。使用方法 jmap -histo pid。如果连用SHELL jmap -histo pid>a.log可以将其保存到文本中去,在一段时间后,使用文本对比工具,可以对比出GC回收了哪些对象。jmap -dump:format=b,file=outfile 3024可以将3024进程的内存heap输出出来到outfile文件里,再配合MAT(内存分析工具(Memory Analysis Tool)或与jhat (Java Heap Analysis Tool)一起使用,能够以图像的形式直观的展示当前内存是否有问题。

64位机上使用需要使用如下方式:

jmap -J-d64 -heap pid

命令格式
       jmap [ option ] pid

       jmap [ option ] executable core

       jmap [ option ] [server-id@]remote-hostname-or-IP

参数说明
1)、options: 

executable Java executable from which the core dump was produced.

(可能是产生core dump的java可执行程序)

core 将被打印信息的core dump文件

remote-hostname-or-IP 远程debug服务的主机名或ip

server-id 唯一id,假如一台主机上多个远程debug服务 

2)、基本参数:

-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的数量和占用内存数也会打印出来. 

-F 强迫.在pid没有相应的时候使用-dump或者-histo参数. 在这个模式下,live子参数无效. 

-h | -help 打印辅助信息 

-J 传递参数给jmap启动的jvm. 

pid 需要被打印配相信息的java进程id

 

使用示例
查询jvm堆的概要信息

复制代码
blue@blue-pc:~/apache-tomcat-7.0.39/conf$ jps -ml
2491 
9142 org.apache.catalina.startup.Bootstrap start
9168 sun.tools.jps.Jps -ml
2967 

root@blue-pc:/home/blue/apache-tomcat-7.0.39/conf# jmap -heap 9142
Attaching to process ID 9142, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 20.10-b01

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

Heap Configuration:
   MinHeapFreeRatio = 40
   MaxHeapFreeRatio = 70
   MaxHeapSize      = 1031798784 (984.0MB)
   NewSize          = 1048576 (1.0MB)
   MaxNewSize       = 4294901760 (4095.9375MB)
   OldSize          = 4194304 (4.0MB)
   NewRatio         = 2
   SurvivorRatio    = 8
   PermSize         = 16777216 (16.0MB)
   MaxPermSize      = 67108864 (64.0MB)

Heap Usage:
PS Young Generation
Eden Space:
   capacity = 16121856 (15.375MB)
   used     = 15074368 (14.37603759765625MB)
   free     = 1047488 (0.99896240234375MB)
   93.50268356199187% used
From Space:
   capacity = 2686976 (2.5625MB)
   used     = 2684584 (2.5602188110351562MB)
   free     = 2392 (0.00228118896484375MB)
   99.91097799161585% used
To Space:
   capacity = 2686976 (2.5625MB)
   used     = 0 (0.0MB)
   free     = 2686976 (2.5625MB)
   0.0% used
PS Old Generation
   capacity = 42991616 (41.0MB)
   used     = 5071824 (4.8368682861328125MB)
   free     = 37919792 (36.16313171386719MB)
   11.797239722275153% used
PS Perm Generation
   capacity = 16777216 (16.0MB)
   used     = 11304456 (10.780769348144531MB)
   free     = 5472760 (5.219230651855469MB)
   67.37980842590332% used
复制代码
 

dump jvm内存信息

复制代码
root@blue-pc:/home/blue/apache-tomcat-7.0.39/conf# jmap -F -dump:format=b,file=tomcat.bin 9142
Attaching to process ID 9142, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 20.10-b01
Dumping heap to tomcat.bin ...
Finding object size using Printezis bits and skipping over...
Heap dump file created
复制代码
 

jhat
用途:是用来分析java堆的命令,可以将堆中的对象以html的形式显示出来,包括对象的数量,大小等等,并支持对象查询语言

生成tomcat.bin dump文件后,使用jhat查看

root@blue-pc:/home/blue/apache-tomcat-7.0.39/conf# jhat tomcat.bin
.....
Started HTTP server on port 7000
Server is ready.
访问 http://localhost:7000,就可以查看详细的内存信息

有时你dump出来的堆很大,在启动时会报堆空间不足的错误,可以使用如下参数:

jhat -J-Xmx512m <heap dump file>

http://www.cnblogs.com/ggjucheng/archive/2013/04/16/3024986.html

http://blog.csdn.net/gtuu0123/article/details/6039474

jvm

相关推荐