10. JVM常用命令

1 性能优化常用命令

  1. top/jps -l:获取要监控的进程ID:Pid
  2. jinfo Pid:输出进程的基本信息。
  3. jstat -gcutil Pid 5s:每5秒输出一次GC情况。
  4. jstack -l Pid > /data/jstack.txt:将指定进行的线程情况进行输出到指定文件中。
  5. jmap -histo Pid > /data/histo.txt:将堆中对象统计信息输出到指定文件中。
  6. jmap -heap Pid > /data/jmap_heap.txt:输出堆内存信息到指定文件中。
  7. jmap -J-d64 -dump:format=b,file=/data/heap_dump.bin Pid:输出JVM的堆内容到指定文件中。

2 官网学习资料

2.1 官网地址

JVM参数官网地址

网页引导过程:官网 -> Reference -> Java SE Tools Reference for UNIX

2.2 jps(JVM Process Status Tool)

2.2.1 官网地址

jps说明

2.2.2 描述

虚拟机进程状况工具,用于输出虚拟机进程状态。

jps是用于查看有权访问的hotspot虚拟机的进程. 当未指定hostid时,默认查看本机jvm进程,否者查看指定的hostid机器上的jvm进程,此时hostid所指机器必须开启jstatd服务。 jps可以列出jvm进程lvmid,主类类名,main函数参数, jvm参数,jar名称等信息。

The jps command lists the instrumented Java HotSpot VMs on the target system. The command is limited to reporting information on JVMs for which it has the access permissions.

2.2.3 命令格式

jps [ options ] [ hostid ]

2.2.3.1 参数(options)
  • -m

显示传递为main方法的参数。

Displays the arguments passed to the main method. The output may be null for embedded JVMs.

  • -l

输出类的全路径名称。

Displays the full package name for the application’s main class or the full path name to the application’s JAR file.

  • -v

输出启动时的JVM参数。

Displays the arguments passed to the JVM.

2.2.4 示例

jsp -l:打印进程ID与对应的类全路径名称。

2.3 jinfo(Configuration Info for Java)

2.3.1 官网地址

jinfo说明

2.3.2 描述

Java配置信息工具,用于实时查看和调整虚拟机各项参数。

The jinfo command prints Java configuration information for a specified Java process or core file or a remote debug server. The configuration information includes Java system properties and Java Virtual Machine (JVM) command-line flags. If the specified process is running on a 64-bit JVM, then you might need to specify the -J-d64 option, for example: jinfo -J-d64 -sysprops pid.

2.3.3 命令格式

jinfo [ option ] pid

2.3.3.1 参数(options)
  • pid

进程ID。

The process ID for which the configuration information is to be printed. The process must be a Java process. To get a list of Java processes running on a machine, use the jps(1) command.

没有参数,打印所有的信息。

Prints both command-line flags and system property name-value pairs.

  • -flag name

打印指定命令行参数的名称和值。

Prints the name and value of the specified command-line flag.

  • -flag [+|-]name

启用或禁用布尔类型参数。

enables or disables the specified Boolean command-line flag.

  • -flag name=value

设置命令行参数值。

Sets the specified command-line flag to the specified value.

  • -flags

打印JVM启动参数。

Prints command-line flags passed to the JVM.

  • -sysprops

以键值对形式打印所有属性。

Prints Java system properties as name-value pairs.

2.3.4 示例

jinfo 666:打印进程ID为666的线程基本信息。
jinfo 666 > /data/666_jinfo.txt:将进程ID为666的线程基本信息输出到/data/666_jinfo.txt文件中。

2.4 jhat(JVM Heap Analysis Tool)

2.4.1 官网地址

jhat说明

2.4.2 描述

虚拟机堆转储快照分析工具

可以使用jhat来分析jmap生成的dump文件,但实际过程中,基本上没有人会使用jhat进行dump文件的分析,一般都会选择第三方更强大的分析工具而不是使用简单的自带的原生工具。

2.5 jmap(Memory Map for Java)

2.5.1 官网地址

jmap说明

2.5.2 描述

Java内存映射工具,用于生成堆转储快照(一般称为heapdump或dump文件)。

Prints shared object memory maps or heap memory details for a process, core file, or remote debug server. This command is experimental and unsupported.

2.5.3 命令格式

jmap [ options ] pid

2.5.3.1 参数(options)

不使用参数时,打印共享对象映射。打印内容有每个共享对象的起始地址、映射大小以及共享对象文件的路径全称。

When no option is used, the jmap command prints shared object mappings. For each shared object loaded in the target JVM, the start address, size of the mapping, and the full path of the shared object file are printed.

  • -dump:[live,] format=b, file=filename

使用hprof二进制形式,输出jvm的heap内容到文件。

Dumps the Java heap in hprof binary format to filename. The live suboption is optional, but when specified, only the active objects in the heap are dumped. To browse the heap dump, you can use the jhat(1) command to read the generated file.

  • -heap

显示Java堆详细信息,如使用哪种回收器、参数配置、分代状况等。

Prints a heap summary of the garbage collection used, the head configuration, and generation-wise heap usage. In addition, the number and size of interned Strings are printed.

  • -histo[:live]

显示堆中对象统计信息,包括类、实例数量、合计容量。

Prints a histogram of the heap. For each Java class, the number of objects, memory size in bytes, and the fully qualified class names are printed. The JVM internal class names are printed with an asterisk (*) prefix. If the live suboption is specified, then only active objects are counted.

  • -F

当正常输出的请求不被响应时,强制输出线程堆栈信息。

Force a stack dump when jstack [-l] pid does not respond.

2.5.4 示例

jmap -histo 666 > /data/666_histo.txt:将进程ID为666的堆中对象统计信息输出到/data/666_histo.txt文件中。
jmap -heap 666 > /data/666_jmap_heap.txt:将进程ID为666的堆内存信息到/data/666_jmap_heap.txt文件中。
jmap -J-d64 -dump:format=b,file=/data/666_heap_dump.bin 666:将进程ID为666的JVM的堆内容到/data/666_heap_dump.bin文件中。

2.6 jstack(Stack Trace for Java)

2.6.1 官网地址

jstack说明

2.6.2 描述

Java堆栈跟踪工具,生成虚拟机当前时刻的线程快照。。

Prints Java thread stack traces for a Java process, core file, or remote debug server.

2.6.3 命令格式

jstack [ options ] pid

2.6.3.1 参数(options)
  • -F

当正常输出的请求不被响应时,强制输出线程堆栈信息。

Force a stack dump when jstack [-l] pid does not respond.

  • -l

除堆栈外,显示关于锁的附加信息。

Long listing. Prints additional information about locks such as a list of owned java.util.concurrent ownable synchronizers.

2.6.4 示例

jstack -l 666:输出进程ID为666的线程情况。
jstack -l 666 > /data/666_jstack.txt:将进程ID为666的线程情况输出到/data/666_jstack.txt文件中。

2.7 jstat(JVM Statistics Monitoring Tool)

2.7.1 官网地址

jstat说明

2.7.2 描述

虚拟机统计信息监视工具,监控JVM运行时各个统计信息。

The jstat command displays performance statistics for an instrumented Java HotSpot VM. The target JVM is identified by its virtual machine identifier, or vmid option.

2.7.3 命令格式

jstat [ generalOption | outputOptions vmid [ interval[s|ms] [ count ] ]

2.7.3.1 常规选项(General Options)

输入了常规选项命令,就不能再输入其它的选项命令。

If you specify one of the general options, then you cannot specify any other option or parameter.

  • -help

显示帮助信息

Displays a help message.

  • -options

输出 Output Options 选项列表。

Displays a list of static options. See Output Options.

2.7.3.2 输出选项(Output Options)
  • -class

监视类装载,卸载数量,总空间以及类装载所消耗的时间。

Displays statistics about the behavior of the class loader.

  • -compiler

输出JIT编译器编译过的方法、耗时等信息。

Displays statistics about the behavior of the Java HotSpot VM Just-in-Time compiler.

  • -gc

监视Java堆状况,包括Eden区、两个survivor区,老年代,永久代等的容量、已用空间,GC时间合计等信息。

Displays statistics about the behavior of the garbage collected heap.

  • -gccapacity

监视内容与-gc基本相同,但输出主要关注Java堆各个区域使用到的最大、最小空间。

Displays statistics about the capacities of the generations and their corresponding spaces.

  • -gcutil

监视内容与-gc基本相同,但输出主要关注已使用空间占总空间的百分比。

Displays a summary about garbage collection statistics.

  • -gccause

与-gcutil功能一样,但是会额外输出导致上一次GC产生的原因。

Displays a summary about garbage collection statistics (same as -gcutil), with the cause of the last and current (when applicable) garbage collection events.

  • -gcnew

监视新生代GC状况。

Displays statistics of the behavior of the new generation.

  • -gcnewcapacity

监视内容与-gcnew基本相同,输出主要关注使用到的最大,最小空间。

Displays statistics about the sizes of the new generations and its corresponding spaces.

  • -gcold

监视老年代GC状况。

Displays statistics about the behavior of the old generation and metaspace statistics.

  • -gcoldcapacity

监视内容与-gcold基本相同,输出主要关注使用的最大,最小空间。

Displays statistics about the sizes of the old generation.

  • -gcmetacapacity

输出元空间GC情况。

Displays statistics about the sizes of the metaspace.

  • -printcompilation

输出已经被JIT编译的方法。

Displays Java HotSpot VM compilation method statistics.

2.7.4 示例
  • jstat -gcutil 666: 输出进程ID为666的GC情况,输出一次。
  • jstat -gcutil 666 5000: 输出进程ID为666的GC情况,每5秒输出一次。
  • jstat -gcutil 666 5000 20: 输出进程ID为666的GC情况,每5秒输出一次,一共只输出20次。
  • jstat -gcutil -t 666: 输出进程ID为666的GC情况,输出一次, 第一例增加时间戳显示,时间戳为JVM启动开始计算。
  • jstat -gcutil -t -h10 666: 输出进程ID为666的GC情况,输出一次, 第一例增加时间戳显示,时间戳为JVM启动开始计算,每输出10条记录后输出一次标题。
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值