查看java版本
java -version
java 自带命令
jps、jinfo、jstat、jmap、jstack、javac、javap、javah......
java自带工具
jconsole、jvisualvm
javah 生成jni
将UserController.java文件生成Java头文件
进入src/main/java路径下
javah -classpath . -jni com.digua.UserController
使用jps查看本地启动的java程序,以及查看启动的配置命令
[root@/]#jps
653899 jar
664440 Jps
#查看帮助
[root@/]#jps -h
illegal argument: -h
usage: jps [-help]
jps [-q] [-mlvV] [<hostid>]
Definitions:
<hostid>: <hostname>[:<port>]
[root@/]# jps -mlvV
653899 javaApplication.jar --server.port=8081 -Xms256m -Xmx256m -Xss256k -XX:+UseParallelOldGC
664616 sun.tools.jps.Jps -mlvV -Denv.class.path=.:/usr/java/jdk/lib/dt.jar:/usr/java/jdk/lib/tools.jar -Dapplication.home=/usr/java/jdk -Xms8m
使用jinfo查看使用的各项配置
[root@/]# jinfo 653899
....太长了省略啦,,,,,,虚拟机详细信息
[root@lanbaba /]# jinfo -h
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
[root@/]# jinfo -flags 653899
Attaching to process ID 653899, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.291-b10
Non-default VM flags: -XX:CICompilerCount=2 -XX:InitialHeapSize=268435456 -XX:MaxHeapSize=268435456 -XX:MaxNewSize=89128960 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=89128960 -XX:OldSize=179306496 -XX:ThreadStackSize=256 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:+UseParallelOldGC
Command line: -Xms256m -Xmx256m -Xss256k -XX:+UseParallelOldGC
使用jstat查看垃圾回收情况
#查看GC回收情况
[root@/]# jstat -gc 653899
S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT
5632.0 5632.0 1056.0 0.0 75776.0 44086.2 175104.0 73553.5 81752.0 78912.3 10584.0 9993.1 70 0.782 3 0.712 1.494
#查看个垃圾回收的分配空间大小
[root@/]# jstat -gccapacity 653899
NGCMN NGCMX NGC S0C S1C EC OGCMN OGCMX OGC OC MCMN MCMX MC CCSMN CCSMX CCSC YGC FGC
87040.0 87040.0 87040.0 5632.0 5632.0 75776.0 175104.0 175104.0 175104.0 175104.0 0.0 1120256.0 81752.0 0.0 1048576.0 10584.0 70 3
#查看各回收代的空间,和垃圾回收时间次数
[root@/]# jstat -gcutil 653899
[root@/]# jstat -help
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.
使用jmap查看堆栈信息以及dump信息
[root@/]# jmap -heap 653899
Attaching to process ID 653899, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.291-b10
using thread-local object allocation.
Parallel GC with 1 thread(s)
Heap Configuration:
MinHeapFreeRatio = 0
MaxHeapFreeRatio = 100
MaxHeapSize = 268435456 (256.0MB)
NewSize = 89128960 (85.0MB)
MaxNewSize = 89128960 (85.0MB)
OldSize = 179306496 (171.0MB)
NewRatio = 2
SurvivorRatio = 8
MetaspaceSize = 21807104 (20.796875MB)
CompressedClassSpaceSize = 1073741824 (1024.0MB)
MaxMetaspaceSize = 17592186044415 MB
G1HeapRegionSize = 0 (0.0MB)
Heap Usage:
PS Young Generation
Eden Space:
capacity = 77594624 (74.0MB)
used = 63667456 (60.718017578125MB)
free = 13927168 (13.281982421875MB)
82.05137510557432% used
From Space:
capacity = 5767168 (5.5MB)
used = 1081344 (1.03125MB)
free = 4685824 (4.46875MB)
18.75% used
To Space:
capacity = 5767168 (5.5MB)
used = 0 (0.0MB)
free = 5767168 (5.5MB)
0.0% used
PS Old Generation
capacity = 179306496 (171.0MB)
used = 75318784 (71.82958984375MB)
free = 103987712 (99.17041015625MB)
42.00560809576023% used
34204 interned Strings occupying 3833760 bytes.
#生产hprof文件,可以用ftp传出再使用JProfile打开
[root@ testproject]# jmap -dump:format=b,file=dddd.hprof 653899
Dumping heap to /home/testproject/dddd.hprof ...
Heap dump file created
使用jstack查看死锁情况
[root@ testproject]# jstack -h
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@ testproject]# jstack -l 653899
2021-11-17 15:19:17
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.291-b10 mixed mode):
"Attach Listener" #38 daemon prio=9 os_prio=0 tid=0x00007f3008052000 nid=0xa2675 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"DestroyJavaVM" #37 prio=5 os_prio=0 tid=0x00007f303400a800 nid=0x9fa4c waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"http-nio-8081-Acceptor-0" #35 daemon prio=5 os_prio=0 tid=0x00007f3035814800 nid=0x9fab8 runnable [0x00007f300671b000]
java.lang.Thread.State: RUNNABLE
at sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method)
at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:424)
at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:252)
- locked <0x00000000fc3e4f60> (a java.lang.Object)
at org.apache.tomcat.util.net.NioEndpoint.serverSocketAccept(NioEndpoint.java:448)
at org.apache.tomcat.util.net.NioEndpoint.serverSocketAccept(NioEndpoint.java:70)
at org.apache.tomcat.util.net.Acceptor.run(Acceptor.java:95)
at java.lang.Thread.run(Thread.java:748)
Locked ownable synchronizers:
- None
查看逻辑cup以及物理cpu数量
lscpu
查看cpu使用情况
top -p PID
然后再根据实际项目的情况进行调优,例如选择合适的垃圾回收器或者调整合适比例的年轻代老年代大小等等。
常见垃圾回收器
串行回收器:Serial(新生代)、Serial Old(老年代)
并行回收器:ParNew(新生代)、Parallel Scavenge(新生代)、Parallel Old(老年代)
并发回收器:CMS(老年代)、G1(整合型)
垃圾回收命令 | 垃圾回收器 |
---|---|
-XX:+UseSerialGC | Serial(新生代)、Serial Old(老年代) |
-XX:+UseConcMarkSweepGC | ParNew(新生代)、CMS(老年代) |
-XX:+UseParallelGC | Parallel Scavenge(新生代)、Serial Old(老年代) |
-XX:+UseParallelOldGC | Parallel Scavenge(新生代)、Parallel Old(老年代) |
-XX:+UseG1GC | G1(整合型) |
最常用的 -X命令及-XX命令 | 解析 |
---|---|
-Xms | 初始堆大小 物理内存的1/64 |
-Xmx | 最大堆大小 物理内存的1/4 |
-Xmn | 年轻代大小 |
-Xss | 每个线程的堆栈大小 JDK5.0以后每个线程堆栈大小为1M,以前每个线程堆栈大小为256K |
-XX:+DisableExplicitGC | 关闭System.gc() |
-XX:+PrintGC | 打印GC信息 |
-XX:+PrintGCDetails | 打印GC详细信息 |
-XX:+HeapDumpOnOutOfMemoryError | 当首次遭遇OOM时导出此时堆中相关信息 |
-XX:+HeapDumpPath=/home/abc.hprof | dump文件指定位置 |
-XX:ErrorFile=./hs_err_pid.log | 保存错误信息 |