Java自带命令行工具攻略
Java自带了多个命令行工具,可以对Java程序进行分析、调试和监控。其中,jmap、jhat和jinfo是非常重要的工具。本攻略将详细讲解它们的使用,包括实例代码和示例结果。
jmap
jmap是Java Memory Map的缩写,是一个用于打印Java进程中内存使用情况的命令行工具。下面是jmap的常用选项:
Usage:
jmap [option] <pid>
(to connect to running process)
jmap [option] <executable <core>
(to connect to a core file)
jmap [option] [-h|-help]
(for help on options)
其中,常用的选项有:
-heap
:打印Java堆(Java heap)的使用情况-histo
:打印Java堆(Java heap)上对象的分布情况-dump
:导出Java堆(Java heap)的快照,以二进制格式保存
下面是一个jmap的使用示例:
# 打印Java堆的使用情况
$ jmap -heap 12345
Attaching to process ID 12345, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.92-b14
using thread-local object allocation.
Garbage-First (G1) GC with 16 thread(s)
Heap Configuration:
MinHeapFreeRatio = 40
MaxHeapFreeRatio = 70
MaxHeapSize = 2147483648 (2048.0MB)
NewSize = 1363144 (1.2999954223632812MB)
MaxNewSize = 17592186044415 MB
OldSize = 5452592 (5.1999969482421875MB)
NewRatio = 2
SurvivorRatio = 8
MetaspaceSize = 16896000 (16.125MB)
CompressedClassSpaceSize = 1073741824 (1024.0MB)
MaxMetaspaceSize = 17592186044415 MB
G1HeapRegionSize = 1048576 (1.0MB)
Heap Usage:
G1 Heap:
regions = 2048
capacity = 2147483648 (2048.0MB)
used = 1350830528 (1289.0901412963867MB)
free = 796653120 (758.9098587036133MB)
62.91093444824219% used
...
上面的示例中,-heap
选项打印了Java堆的使用情况。12345
是进程PID。
jhat
jhat是Java Heap Analysis Tool的缩写,是一个用于分析Java堆(Java heap)中对象分布情况的命令行工具。jhat使用Web界面展示堆中对象信息,并支持搜索、过滤等操作。下面是jhat的常用选项:
Usage:
jhat [-version] [-h | -help] [-J<flag>] [<file>]
(to read heap dump from a file)
jhat [-version] [-h | -help] [-J<flag>] <vmid>
(to connect to a running VM)
where <file> is an HPROF binary heap dump file.
<vmid> is the ID of a process to attach to.
See jps(1) for information on determining the VMID.
其中,需要传入一个HPROF格式的堆转储文件,或者连接到一个正在运行的Java进程。如果传入一个文件,则可以不指定vmid;如果连接到一个Java进程,则必须要指定vmid。
下面是一个jhat的使用示例:
# 生成Java堆快照并以HPROF格式保存
$ jmap -dump:file=/tmp/dump.hprof 12345
# 启动jhat并加载堆快照
$ jhat /tmp/dump.hprof
Reading from /tmp/dump.hprof...
Dump file created Thu Sep 30 19:42:03 CST 2021
Snapshot read, resolving...
Resolving 78801 objects...
Chasing references, expect 3 dots.................
Eliminating duplicate references..............
Snapshot resolved.
# jhat Web界面启动成功,请在浏览器中打开 http://localhost:7000 查看
上面的示例中,-dump
选项导出了Java堆的快照,并以HPROF格式保存到/tmp/dump.hprof
文件中。jhat /tmp/dump.hprof
启动了jhat,并加载了该堆快照。最后,浏览器中打开http://localhost:7000
即可查看堆中对象信息。
jinfo
jinfo是Java Configuration Info的缩写,是一个用于打印Java进程配置信息的命令行工具。下面是jinfo的常用选项:
Usage:
jinfo [option] <pid>
(to connect to running process)
jinfo [option] <executable>
(to examine a remote executable)
jinfo [option] [server_id@]<remote server IP or hostname>
(to connect to an instrumented JVM on a remote host)
where <pid> is an LWP id of a thread in the target Java process.
<executable> is the name of the remote java launcher executable.
<server_id> is a unique prefix for integer server-side identifications.
其中,常用的选项有:
-flags
:打印Java虚拟机(JVM)的运行时参数-sysprops
:打印Java虚拟机(JVM)的系统属性
下面是一个jinfo的使用示例:
# 打印Java虚拟机(JVM)的运行时参数
$ jinfo -flags 12345
Attaching to process ID 12345, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.92-b14
Non-default VM flags: -XX:G1HeapRegionSize=1048576 -XX:HeapDumpPath=/tmp -XX:InitialHeapSize=2147483648 -XX:MaxHeapSize=2147483648 -XX:+PrintCommandLineFlags -XX:ReservedCodeCacheSize=262144000 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseG1GC
# 打印Java虚拟机(JVM)的系统属性
$ jinfo -sysprops 12345
Attaching to process ID 12345, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.92-b14
java.runtime.name = Java(TM) SE Runtime Environment
sun.boot.library.path = ...
...
上面的示例中,-flags
选项打印了JVM的运行时参数,-sysprops
选项打印了JVM的系统属性。12345
是进程PID。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:java自带命令行工具jmap、jhat与jinfo的使用实例代码详解 - Python技术站