Java常用的性能调优的命令有哪些?

Java常用的性能调优的命令有哪些?

编码文章call10242025-06-20 14:30:483A+A-

jps命令

JPS(Java Process Status Tool),Java版本的PS命令,主要是用来查看Java进程相关信息的,如果你需要再服务器上查找Java相关进程可以使用jps命令来查找。如果直接使用Linux的ps命令则还需要配合grep命令来查找Java进程信息。

jps [options] [hostid]

其中options是操作参数

  • -l:表示显示进程ID以及主类名的路径
  • -q:表示显示进程ID
  • -m:显示进程ID以及在启动的时候传入到主函数中的参数信息
  • -v:显示进程ID以及在启动的时候传递的JVM的启动参数。

hostid 表示的对应的服务器ID可以是本机,如果是本机的话可以省略,也可以是相互连通的其他服务器的IP。

当然这些命令参数也可以组合使用如下所示。

[root@localhost ~]# jps -l
27681 swly-api-0.0.1-8081.jar
4789 code-admin.jar
4263 code-admin.jar
397 com.qinhailin.common.config.MainConfig
28317 sun.tools.jps.Jps

jinfo命令

jinfo命令是用来查看JVM参数的命令,也可以用来动态的去修改一些JVM的参数。其用法如下所示

jinfo [option] <pid>

option参数如下

  • no options 可以输出系统的所有系统参数以及使用到的属性
  • -flag:可以打印出指定的参数信息
  • -flag:[+|-] 可以通过这种方式打开或者是关闭某些参数的使用。
  • -flag= :可以使用这种方式来设置参数
  • -flags 打印所有的虚拟机参数以及应用参数
  • -sysprops :打印系统配置参数

查看所有的系统参数

[root@localhost ~]# jinfo 28712

查看所有虚拟机参数

[root@localhost ~]# jinfo -flags 28712
Attaching to process ID 28712, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.291-b10
Non-default VM flags: -XX:CICompilerCount=3 -XX:InitialHeapSize=130023424 -XX:MaxHeapSize=2051014656 -XX:MaxNewSize=683671552 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=42991616 -XX:OldSize=87031808 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:+UseParallelGC 
Command line:  
[root@localhost ~]# 

当然也可以通过如下的这种方式来查看选定的参数

[root@localhost ~]# jinfo -flag PrintGC 28869
-XX:-PrintGC
[root@localhost ~]# 

jstat命令

jstat命令是在JVM调优的时候,使用较多的命令,它主要是用来查看JVM的运行状态信息,包括内存、垃圾回收等信息。使用命令如下,其中VMID是虚拟机的进程ID,interval 表示打印间隔时间,count表示打印次数。

jstat [option] VMID [interval] [count]

option参数说明,也可以通过jstat -help 命令查看帮助文档

  • -class 这个参数主要是对于类加载的信息的统计,包括已经加载、未加载、加载大小加载时间等信息的统计
  • -compiler HotSpt JIT编译器行为进行统计,包时间,大小以及属性方法等信息
  • -gc 垃圾回收堆的行为,包括统计s1/s2的垃圾收集情况,新生代老年代垃圾回收情况等等。
  • -gccapacity 各个垃圾回收代容量(young,old,perm)和他们相应的空间统计
  • -gcutil 垃圾回收统计概述,包括s1、s2、新生代、老年代信息等。
  • -gccause 垃圾收集统计概述(同-gcutil),附加最近两次垃圾回收事件的原因
  • -gcnew 新生代行为统计
  • -gcnewcapacity 新生代与其相应的内存空间的统计
  • -gcold 年老代和永生代行为统计
  • -gcoldcapacity 年老代行为统计
  • -printcompilation HotSpot编译方法统计

命令使用情况如下所示

[root@localhost ~]# jstat -gcutil 28869 1000 3
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
  0.00   0.00  47.48  23.89  95.28  93.17     12    0.147     3    0.279    0.426
  0.00   0.00  47.48  23.89  95.28  93.17     12    0.147     3    0.279    0.426
  0.00   0.00  47.48  23.89  95.28  93.17     12    0.147     3    0.279    0.426
[root@localhost ~]# 

关于这些参数可以参考JVM内存模型来理解。

jstack命令

jstack命令是用来查看JVM线程的快照的命令,因为线程在堆栈中的信息是动态的所以只能保存在某一时间的快照来对其进行分析。使用jstack就可以实现这个功能,并且可以分析出线程卡顿,执行不流畅的原因,例如出现了死锁、死循环等操作信息。当然jstack还可以用来查看应用程序崩溃之后所产生的信息文件。

命令如下

jstack [option]

使用参数如下所示

[root@localhost ~]# 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
[root@localhost ~]# 

例如可以使用如下的命令来查看堆栈信息

[root@localhost ~]# jstack -l 28869

jmap命令

jmap命令可以生成JVM的运行的dump文件,用来查看堆内存对象信息、查看类加载信息以及finalizer的队列信息等等。

命令如下所示

[root@localhost ~]# jmap
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
[root@localhost ~]# 

jhat命令

jhat命令用来分析通过jmap命令生成的dump文件,而且jhat命令内置了应用服务器,通过网页也可以查看到dump中的信息,一般情况下jhat就是用来分析离线情况下的dump文件。

使用命令如下

[root@localhost ~]# jhat
ERROR: No arguments supplied
Usage:  jhat [-stack <bool>] [-refs <bool>] [-port <port>] [-baseline <file>] [-debug <int>] [-version] [-h|-help] <file>

	-J<flag>          Pass <flag> directly to the runtime system. For
			  example, -J-mx512m to use a maximum heap size of 512MB
	-stack false:     Turn off tracking object allocation call stack.
	-refs false:      Turn off tracking of references to objects
	-port <port>:     Set the port for the HTTP server.  Defaults to 7000
	-exclude <file>:  Specify a file that lists data members that should
			  be excluded from the reachableFrom query.
	-baseline <file>: Specify a baseline object dump.  Objects in
			  both heap dumps with the same ID and same class will
			  be marked as not being "new".
	-debug <int>:     Set debug level.
			    0:  No debug output
			    1:  Debug hprof file parsing
			    2:  Debug hprof file parsing, no server
	-version          Report version number
	-h|-help          Print this help and exit
	<file>            The file to read

For a dump file that contains multiple heap dumps,
you may specify which dump in the file
by appending "#<number>" to the file name, i.e. "foo.hprof#3".

All boolean options default to "true"
[root@localhost ~]# 

总结

上面我们介绍了在JVM性能调优的时候常用的一些Java命令。希望对大家能有所帮助。

点击这里复制本文地址 以上内容由文彬编程网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

文彬编程网 © All Rights Reserved.  蜀ICP备2024111239号-4