linux获取java进程PID:

如果命令使用过程中报错,可能解决你问题的方案:

==========================================

1、jmap命令基本概述

jmap命令是一个可以输出所有内存中对象的工具,甚至可以将VM 中的heap,以二进制输出成文本。

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

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

jmap -J-d64 -heap pid

2、命令格式
复制代码

jmap [option] <pid>
    (to connect to running process) 连接到正在运行的进程

jmap [option] <executable <core>
    (to connect to a core file)     连接到核心文件

jmap [option] [server_id@]<remote server IP or hostname>
    (to connect to remote debug server) 连接到远程调试服务

复制代码

3.参数说明

  1. options:
pid: 目标进程的PID,进程编号,可以采用ps -ef | grep java 查看java进程的PID;
 executable: 产生core dump的java可执行程序;
 core: 将被打印信息的core dump文件;
 remote-hostname-or-IP: 远程debug服务的主机名或ip;
 server-id: 唯一id,假如一台主机上多个远程debug服务;

2)基本参数:

[就是替换[option]位置的参数]
1> -dump:[live,]format=b,file= 使用hprof二进制形式,输出jvm的heap内容到文件=. live子选项是可选的,假如指定live选项,那么只输出活的对象到文件.
命令:
jmap -dump:live,format=b,file=myjmapfile.txt 19570
结果:
即可在/root目录打开myjmapfile.txt文件。
当然,file=后面也可以指定文件存放的目录,就可以在指定目录查看文件了。
2> -finalizerinfo 打印正等候回收的对象的信息
命令:
jmap -finalizerinfo 3772
结果:
Attaching to process ID 19570, please wait…
 Debugger attached successfully.
 Server compiler detected.
 JVM version is 24.80-b11
 Number of objects pending for finalization: 0 (等候回收的对象为0个)3> -heap 打印heap的概要信息,GC使用的算法,heap(堆)的配置及JVM堆内存的使用情况.
命令:
jmap -heap 19570
结果:
解析:
 复制代码using parallel threads in the new generation. ##新生代采用的是并行线程处理方式
using thread-local object allocation.
Concurrent Mark-Sweep GC ##同步并行垃圾回收
Heap Configuration: ##堆配置情况,也就是JVM参数配置的结果[平常说的tomcat配置JVM参数,就是在配置这些]
MinHeapFreeRatio = 40 ##最小堆使用比例
MaxHeapFreeRatio = 70 ##最大堆可用比例
MaxHeapSize = 2147483648 (2048.0MB) ##最大堆空间大小
NewSize = 268435456 (256.0MB) ##新生代分配大小
MaxNewSize = 268435456 (256.0MB) ##最大可新生代分配大小
OldSize = 5439488 (5.1875MB) ##老年代大小
NewRatio = 2 ##新生代比例
SurvivorRatio = 8 ##新生代与suvivor的比例
PermSize = 134217728 (128.0MB) ##perm区 永久代大小
MaxPermSize = 134217728 (128.0MB) ##最大可分配perm区 也就是永久代大小
Heap Usage: ##堆使用情况【堆内存实际的使用情况】
New Generation (Eden + 1 Survivor Space): ##新生代(伊甸区Eden区 + 幸存区survior(1+2)空间)
capacity = 241631232 (230.4375MB) ##伊甸区容量
used = 77776272 (74.17323303222656MB) ##已经使用大小
free = 163854960 (156.26426696777344MB) ##剩余容量
32.188004570534986% used ##使用比例
Eden Space: ##伊甸区
capacity = 214827008 (204.875MB) ##伊甸区容量
used = 74442288 (70.99369812011719MB) ##伊甸区使用
free = 140384720 (133.8813018798828MB) ##伊甸区当前剩余容量
34.65220164496263% used ##伊甸区使用情况
From Space: ##survior1区
capacity = 26804224 (25.5625MB) ##survior1区容量
used = 3333984 (3.179534912109375MB) ##surviror1区已使用情况
free = 23470240 (22.382965087890625MB) ##surviror1区剩余容量
12.43827838477995% used ##survior1区使用比例
To Space: ##survior2 区
capacity = 26804224 (25.5625MB) ##survior2区容量
used = 0 (0.0MB) ##survior2区已使用情况
free = 26804224 (25.5625MB) ##survior2区剩余容量
0.0% used ## survior2区使用比例
PS Old Generation: ##老年代使用情况
capacity = 1879048192 (1792.0MB) ##老年代容量
used = 30847928 (29.41887664794922MB) ##老年代已使用容量
free = 1848200264 (1762.5811233520508MB) ##老年代剩余容量
1.6416783843721663% used ##老年代使用比例
Perm Generation: ##永久代使用情况
capacity = 134217728 (128.0MB) ##perm区容量
used = 47303016 (45.111671447753906MB) ##perm区已使用容量
free = 86914712 (82.8883285522461MB) ##perm区剩余容量
35.24349331855774% used ##perm区使用比例
复制代码
4> -histo[:live] 打印每个class的实例数目,内存占用,类全名信息. VM的内部类名字开头会加上前缀”*”. 如果live子参数加上后,只统计活的对象数量.
命令:
jmap -histo:live 19570
结果:
 复制代码num #instances(实例) #bytes(字节大小) class name(类名)
1: 65220 9755240     
 2: 65220 8880384     
 3: 11721 8252112     [B
 4: 6300 6784040     
 5: 75224 6218208     [C
 6: 93969 5163280     
 7: 6300 4854440     
 8: 5482 4203152     
 9: 72097 2307104     java.lang.String
 10: 15102 2289912     [I
 11: 4089 2227728     
 12: 28887 1386576      org.apache.velocity.runtime.parser.Token
 13: 6792 706368 java.lang.Class
 14: 7445 638312 [Ljava.util.HashMap$Entry;4380: 1 16 com.sun.proxy.Proxy46
 4383: 1 16 org.apache.ibatis.ognl.SetPropertyAccessor
 4384: 1 16 oracle.jdbc.driver.OracleDriver
 4385: 1 16 com.sun.proxy.Proxy82
 4387: 1 16 java.util.jar.JavaUtilJarAccessImpl
 4388: 1 16 com.sun.proxy.$Proxy171
 4389: 1 16 sun.reflect.GeneratedMethodAccessor136
 4390: 1 16 sun.reflect.GeneratedConstructorAccessor22
 4391: 1 16 org.elasticsearch.action.search.SearchAction
 4392: 1 16 org.springframework.core.annotation.AnnotationAwareOrderComparator
 Total 1756265 162523736复制代码
采用jmap -histo pid>a.log日志将其保存,在一段时间后,使用文本对比工具,可以对比出GC回收了哪些对象。
jmap -dump:format=b,file=outfile 3024可以将3024进程的内存heap输出出来到outfile文件里,再配合MAT(内存分析工具)。
5> -permstat 打印classload和jvm heap长久层的信息. 包含每个classloader的名字,活泼性,地址,父classloader和加载的class数量. 另外,内部String的数量和占用内存数也会打印出来.
命令:
jmap -permstat 19570
结果:
解析:
 复制代码Attaching to process ID 19570, please wait…
 Debugger attached successfully.
 Server compiler detected.
 JVM version is 24.80-b11
 finding class loader instances …done.
 computing per loader stat …done.
 please wait… computing liveness.liveness analysis may be inaccurate …
 class_loader classes bytes parent_loader alive? type 2538 14654264 null live 
 0x000000070af968c8 63 399160 0x0000000707db1788 dead org/apache/catalina/loader/WebappClassLoader@0x000000070367d2a8
 0x000000070cba7b08 1 3064 0x0000000707e709a8 dead sun/reflect/DelegatingClassLoader@0x0000000702a50b98
 0x000000070cba6a38 28 221344 0x0000000707e709a8 dead org/apache/jasperrvlet/JasperLoader@0x0000000705b11878
 0x000000070baed8b8 32 297296 0x0000000707e709a8 dead org/apache/jasperrvlet/JasperLoader@0x0000000705b11878
 0x000000070919a610 1 3056 0x0000000707e709a8 dead sun/reflect/DelegatingClassLoader@0x0000000702a50b98
 0x000000070bdd1e18 1 3064 0x0000000707e709a8 dead sun/reflect/DelegatingClassLoader@0x0000000702a50b98
 0x0000000707f1d480 1 3072 null dead sun/reflect/DelegatingClassLoader@0x0000000702a50b98
 0x000000070cba7f48 1 1912 0x0000000707e709a8 dead sun/reflect/DelegatingClassLoader@0x0000000702a50b98
 0x000000070cba8508 1 3064 0x0000000707e709a8 dead sun/reflect/DelegatingClassLoader@0x0000000702a50b98
 0x000000070cba6c40 1 3064 0x0000000707e709a8 dead sun/reflect/DelegatingClassLoader@0x0000000702a50b98
 0x000000070bd4c6a0 1 3056 null dead sun/reflect/DelegatingClassLoader@0x0000000702a50b98
 0x000000070cba62b0 28 235472 0x0000000707e709a8 dead org/apache/jasperrvlet/JasperLoader@0x0000000705b11878
 0x000000070cba77c8 1 3064 0x0000000707e709a8 dead sun/reflect/DelegatingClassLoader@0x0000000702a50b98
 0x000000070cba7388 1 3064 0x0000000707e709a8 dead sun/reflect/DelegatingClassLoader@0x0000000702a50b98
 0x000000070cba8148 1 3064 0x0000000707e709a8 dead sun/reflect/DelegatingClassLoader@0x0000000702a50b98
 0x000000070afd8b60 1 6704 0x0000000707db1788 dead org/apache/catalina/loader/WebappClassLoader@0x000000070367d2a8
 0x000000070919a410 1 1888 null dead sun/reflect/DelegatingClassLoader@0x0000000702a50b98
 0x000000070bdd05b0 1 1912 null dead sun/reflect/DelegatingClassLoader@0x0000000702a50b98
 0x000000070bc848b8 1 3088 0x0000000707db1788 dead sun/reflect/DelegatingClassLoader@0x0000000702a50b98
 0x000000070cba64e8 1 1888 0x0000000707e709a8 dead sun/reflect/DelegatingClassLoader@0x0000000702a50b98
 0x0000000707f1d2c0 1 3064 0x0000000707db1788 dead sun/reflect/DelegatingClassLoader@0x0000000702a50b98
 0x000000070be82e38 1 1912 null dead sun/reflect/DelegatingClassLoader@0x0000000702a50b98
 0x000000070cba7908 1 3208 null dead sun/reflect/DelegatingClassLoader@0x0000000702a50b98
 …total = 273 12995 87547304 N/A alive=1, dead=272 N/A
复制代码
6> -F 强迫.在pid没有相应的时候使用-dump或者-histo参数. 在这个模式下,live子参数无效.
7> -h | -help 打印辅助信息
8> -J 传递参数给jmap启动的jvm.