JDK命令行工具使用详解

23 篇文章 0 订阅

这篇文章是基于sun提供的命令行工具使用及操作详解。随着时代的进步,市面上已经有许多支持工作的工具,例如阿里的Arthas(阿尔萨斯)就深受喜爱,实现了JVM自带的几乎所有诊断功能,这篇文章是介绍sun提供的一系列开源分析诊断工具。
前面有写过各种JDK命令行工具的简介,详情可查看《JDK目录结构以及自带的工具解析》

一、JPS

主要用于查看进程ID(PID)。

C:\Program Files\Java\jdk1.8.0_152\bin>jps -help
usage: jps [-help]
       jps [-q] [-mlvV] [<hostid>]

Definitions:
    <hostid>:      <hostname>[:<port>]

(1)jps -l
  输出应用程序main.class的完整package名或者应用程序jar文件完整路径名
(2)jps -v
  输出传递给JVM的参数。在诊断JVM相关问题的时候,这个参数可以查看JVM相关参数的设置
(3)jps -q
  仅显示pid,不显示其它任何相关信息
(4)jps -m
  输出传递给main方法的参数

注意:
  Linux环境中jps命令不能查看到进程号?但是用ps -ef | grep javatop | grep java却可以查看到已启动的Java进程?
简单了解jps的机制:
  java程序启动后,会在目录/tmp/hsperfdata_{userName}/下生成几个文件,文件名就是java进程的pid,因此jps列出进程id就是把这个目录下的文件名列一下而已,至于系统参数,则是读取文件中的内容。
  那么当磁盘空间满了后,无法创建这些文件,或者说用户对这些文件没有读取权限,是不是就会照成jps命令失效呢。

二、jmap

Java内存映射工具,主要用于打印指定java进程的共享对象内存映射或堆内存细节
  堆Dump是反映堆使用情况的内存镜像,其中主要包括系统信息、虚拟机属性、完整的线程Dump、所有类和对象的状态等。一般在内存不足,GC异常等情况下,我们会去怀疑内存泄漏,这个时候就会去打印堆Dump。

C:\Program Files\Java\jdk1.8.0_152\bin>jmap -help
Usage:
    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)

where <option> is one of:
    <none>               to print same info as Solaris pmap
    -heap                to print java heap summary
    -histo[:live]        to print histogram of java object heap; if the "live"
                         suboption is specified, only count live objects
    -clstats             to print class loader statistics
    -finalizerinfo       to print information on objects awaiting finalization
    -dump:<dump-options> to dump java heap in hprof binary format
                         dump-options:
                           live         dump only live objects; if not specified,
                                        all objects in the heap are dumped.
                           format=b     binary format
                           file=<file>  dump heap to <file>
                         Example: jmap -dump:live,format=b,file=heap.bin <pid>
    -F                   force. Use with -dump:<dump-options> <pid> or -histo
                         to force a heap dump or histogram when <pid> does not
                         respond. The "live" suboption is not supported
                         in this mode.
    -h | -help           to print this help message
    -J<flag>             to pass <flag> directly to the runtime system

(1)jmap pid
  查看进程堆内存使用情况,包括使用的GC算法、堆配置参数和各代中堆内存使用情况。
  打印的信息分别为:共享对象的起始地址、映射大小、共享对象路径的全程。
(2)jmap -heap pid
  查看堆使用情况
(3)jmap -histo[:live] pid
  查看堆中对象数量和大小的直方图
  打印的信息分别是:序列号、对象的数量、这些对象的内存占用大小、这些对象所属的类的全限定名
  如果是内部类,类名的开头会加上*,如果加上live子参数的话,如jmap -histo:live pid,这个命名会触发一次FUll GC,只统计存活对象
(4)jmap -dump:format=b,file=heapdump pid
  将内存使用的详细情况输出到文件,然后使用jhat命令查看该文件:jhat -port 4000 文件名 ,在浏览器中访问http:localhost:4000/

总结:
  该命令适用的场景是程序内存不足或者GC频繁,这时候很可能是内存泄漏。通过用以上命令查看堆使用情况、大量对象被持续引用等情况。

三、jstack(Java Stack Trace)

Java堆栈跟踪工具,主要用于生成指定进程当前时刻的线程快照,线程快照是当前java虚拟机每一条线程正在执行的方法堆栈的集合,生成线程快照的主要目的是用于定位线程出现长时间停顿的原因,如线程间死锁、死循环、请求外部资源导致长时间等待。
  jstack可以定位到线程堆栈,根据堆栈信息可以定位到具体代码,所以它在JVM性能调优中使用得非常多。

C:\Program Files\Java\jdk1.8.0_152\bin>jstack
Usage:
    jstack [-l] <pid>
        (to connect to running process)
    jstack -F [-m] [-l] <pid>
        (to connect to a hung process)
    jstack [-m] [-l] <executable> <core>
        (to connect to a core file)
    jstack [-m] [-l] [server_id@]<remote server IP or hostname>
        (to connect to a remote debug server)

Options:
    -F  to force a thread dump. Use when jstack <pid> does not respond (process is hung)
    -m  to print both java and native frames (mixed mode)
    -l  long listing. Prints additional information about locks
    -h or -help to print this help message

四、jstat

虚拟机统计信息监视工具,主要是对java应用程序的资源和性能进行实时的命令行监控,包括了对heap size和垃圾回收状况的监控。

C:\Program Files\Java\jdk1.8.0_152\bin>jstat
invalid argument count
Usage: jstat -help|-options
       jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]

Definitions:
  <option>      An option reported by the -options option
  <vmid>        Virtual Machine Identifier. A vmid takes the following form:
                     <lvmid>[@<hostname>[:<port>]]
                Where <lvmid> is the local vm identifier for the target
                Java virtual machine, typically a process id; <hostname> is
                the name of the host running the target Java virtual machine;
                and <port> is the port number for the rmiregistry on the
                target host. See the jvmstat documentation for a more complete
                description of the Virtual Machine Identifier.
  <lines>       Number of samples between header lines.
  <interval>    Sampling interval. The following forms are allowed:
                    <n>["ms"|"s"]
                Where <n> is an integer and the suffix specifies the units as
                milliseconds("ms") or seconds("s"). The default units are "ms".
  <count>       Number of samples to take before terminating.
  -J<flag>      Pass <flag> directly to the runtime system.

(1)jstat工具选项如下:

C:\Program Files\Java\jdk1.8.0_152\bin>jstat -options
-class
-compiler
-gc
-gccapacity
-gccause
-gcmetacapacity
-gcnew
-gcnewcapacity
-gcold
-gcoldcapacity
-gcutil
-printcompilation

在这里插入图片描述

(2)查看垃圾回收信息:jstat -gc PID
显示内容如下:
S0C:年轻代第一个survivor的容量(字节)
S1C:年轻代第二个survivor的容量(字节)
S0U:年轻代第一个survivor已使用的容量(字节)
S1U:年轻代第二个survivor已使用的容量(字节)
EC:年轻代中Eden的空间(字节)
EU:年代代中Eden已使用的空间(字节)
OC:老年代的容量(字节)
OU:老年代中已使用的空间(字节)
PC:永久代的容量
PU:永久代已使用的容量
YGC:从应用程序启动到采样时年轻代中GC的次数
YGCT:从应用程序启动到采样时年轻代中GC所使用的时间(单位:S)
FGC:从应用程序启动到采样时老年代中GC(FULL GC)的次数
FGCT:从应用程序启动到采样时老年代中GC所使用的时间(单位:S)

五、jinfo

输出并修改运行时的java进程的运行参数。

C:\Program Files\Java\jdk1.8.0_152\bin>jinfo
Usage:
    jinfo [option] <pid>
        (to connect to running process)
    jinfo [option] <executable <core>
        (to connect to a core file)
    jinfo [option] [server_id@]<remote server IP or hostname>
        (to connect to remote debug server)

where <option> is one of:
    -flag <name>         to print the value of the named VM flag
    -flag [+|-]<name>    to enable or disable the named VM flag
    -flag <name>=<value> to set the named VM flag to the given value
    -flags               to print VM flags
    -sysprops            to print Java system properties
    <no option>          to print both of the above
    -h | -help           to print this help message

六、jconsole

jconsole是一个用java写的GUI程序,用来监控VM,并可监控远程的VM
使用方法:jconsole + PID
在这里插入图片描述

持续补充。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值