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;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;/management/ObjectName$Property;>;II)V--More--(0%)
TMLManagerServlet$7%)rvlet$4--More--(0%)ersers/XSDSimpleTypeTraverser;--More--(0%)



jmap -dump:live,format=b,file=heap.txt 24837
jmap -dump:format=b,file=jmapformat.txt 24837

12:20 [[email protected]]$ jhat heap.txt
Reading from heap.txt...
Dump file created Tue Jan 12 12:20:18 CST 2016
Snapshot read, resolving...
Resolving 244567 objects...
Chasing references, expect 48 dots................................................
Eliminating duplicate references................................................
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.

1、top
2、shift+H 在终端1,按下“H”键或者“shift+h”,top视图会切换到线程视图,其中PID是线程号
nginx 15 0 269m 29m 924 S 0.7 0.0 133:43.06 nginx
29077 nginx 15 0 269m 28m 872 S 0.3 0.0 134:12.98 nginx 1 root 15 0 10368 684 572 S 0.0 0.0 0:01.25 init
2 root RT -5 0 0 0 S 0.0 0.0 0:10.92 migration/0 3 root 34 19 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/0
4 root RT -5 0 0 0 S 0.0 0.0 0:00.00 watchdog/0 5 root RT -5 0 0 0 S 0.0 0.0 0:12.51 migration/1
6 root 34 19 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/1 7 root RT -5 0 0 0 S 0.0 0.0 0:00.00 watchdog/1
8 root RT -5 0 0 0 S 0.0 0.0 0:03.06 migration/2 9 root 34 19 0 0 0 S 0.0 0.0 0:00.67 ksoftirqd/2
10 root RT -5 0 0 0 S 0.0 0.0 0:00.00 watchdog/2 11 root RT -5 0 0 0 S 0.0 0.0 0:02.12 migration/3
12 root 34 19 0 0 0 S 0.0 0.0 0:01.00 ksoftirqd/3 13 root RT -5 0 0 0 S 0.0 0.0 0:00.00 watchdog/3
14 root RT -5 0 0 0 S 0.0 0.0 0:01.83 migration/4 15 root 34 19 0 0 0 S 0.0 0.0 0:00.06 ksoftirqd/4
16 root RT -5 0 0 0 S 0.0 0.0 0:00.00 watchdog/4 17 root RT -5 0 0 0 S 0.0 0.0 0:00.73 migration/5
18 root 34 19 0 0 0 S 0.0 0.0 0:00.04 ksoftirqd/5 19 root RT -5 0 0 0 S 0.0 0.0 0:00.00 watchdog/5
20 root RT -5 0 0 0 S 0.0 0.0 0:01.28 migration/6 21 root 34 19 0 0 0 S 0.0 0.0 0:00.03 ksoftirqd/6
22 root RT -5 0 0 0 S 0.0 0.0 0:00.00 watchdog/6 23 root RT -5 0 0 0 S 0.0 0.0 0:00.52 migration/7
24 root 34 19 0 0 0 S 0.0 0.0 0:00.04 ksoftirqd/7 25 root RT -5 0 0 0 S 0.0 0.0 0:00.00 watchdog/7
26 root 10 -5 0 0 0 S 0.0 0.0 0:01.91 events/0 27 root 10 -5 0 0 0 S 0.0 0.0 0:49.45 events/1
28 root 10 -5 0 0 0 S 0.0 0.0 0:04.01 events/2 29 root 10 -5 0 0 0 S 0.0 0.0 0:00.87 events/3
30 root 10 -5 0 0 0 S 0.0 0.0 0:01.08 events/4 31 root 10 -5 0 0 0 S 0.0 0.0 0:00.06 events/5
tty:[1] jobs:[0] cwd:[/opt/logs/nginx/access]

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

13:25 [[email protected]]$ printf 0x%x 27698
0x6c32

jstack 27698 > jstack27698.txt

与nid一致即找到


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

top - 13:31:47 up 252 days, 2:51, 1 user, load average: 0.07, 0.10, 0.09
Tasks: 68 total, 0 running, 68 sleeping, 0 stopped, 0 zombie
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: 65995412k total, 39238540k used, 26756872k free, 208128k buffers
Swap: 4192956k total, 0k used, 4192956k free, 36363388k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
27698 cafe 18 0 5050m 287m 10m S 0.0 0.4 0:00.00 java
27701 cafe 18 0 5050m 287m 10m S 0.0 0.4 0:06.00 java
27702 cafe 15 0 5050m 287m 10m S 0.0 0.4 0:00.11 java
27703 cafe 15 0 5050m 287m 10m S 0.0 0.4 0:00.09 java
27704 cafe 16 0 5050m 287m 10m S 0.0 0.4 0:00.11 java
27705 cafe 15 0 5050m 287m 10m S 0.0 0.4 0:00.07 java

查看线程信息
13:31 [[email protected]]$ ps -mp 27698 -o THREAD,tid,time
USER %CPU PRI SCNT WCHAN USER SYSTEM TID TIME
cafe 1.4 - - - - - - 00:00:57
cafe 0.0 21 - futex_ - - 27698 00:00:00
cafe 0.1 21 - - - - 27701 00:00:06
cafe 0.0 24 - futex_ - - 27702 00:00:00
cafe 0.0 24 - futex_ - - 27703 00:00:00
cafe 0.0 23 - futex_ - - 27704 00:00:00
cafe 0.0 24 - 184466 - - 27705 00:00:00
cafe 0.0 24 - futex_ - - 27706 00:00:00
cafe 0.0 24 - 184466 - - 27707 00:00:00
cafe 0.0 24 - 184466 - - 27708 00:00:00
cafe 0.0 24 - 184466 - - 27709 00:00:00
cafe 0.0 17 - 184466 - - 27710 00:00:00
cafe 0.0 20 - futex_ - - 27711 00: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= 使用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


http://www.cnblogs.com/ggjucheng/archive/2013/04/16/3024986.html
http://blog.csdn.net/gtuu0123/article/details/6039474

你可能感兴趣的:(jvm)