常用jvm命令
jstat
查看java进程的gc状况
/ # jstat -gc 11
S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT
26112.0 26112.0 0.0 23597.8 209920.0 10723.2 524288.0 160828.1 94592.0 90638.6 10880.0 10134.0 14083 364.899 585 161.727 526.626
/ # jstat -gcutil 11
S0 S1 E O M CCS YGC YGCT FGC FGCT GCT
83.26 0.00 20.78 33.24 95.82 93.14 14084 364.926 585 161.727 526.652
注:结果中涉及到jvm堆内存的相关知识,这里不做详细说明,感兴趣的可以搜索jvm堆内存相关的文章学习研究一下
jstack
查看jvm中栈的信息(线程)
直接查看前50行栈的信息
/ # jstack 11 | head -50
2019-09-09 06:50:10
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.171-b11 mixed mode):
"ActiveMQ InactivityMonitor Worker" #11371 daemon prio=5 os_prio=0 tid=0x00007ff4d0006800 nid=0x2d88 waiting on condition [0x00007ff4b4fd0000]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000000d338aec0> (a java.util.concurrent.SynchronousQueue$TransferStack)
at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
at java.util.concurrent.SynchronousQueue$TransferStack.awaitFulfill(SynchronousQueue.java:460)
at java.util.concurrent.SynchronousQueue$TransferStack.transfer(SynchronousQueue.java:362)
at java.util.concurrent.SynchronousQueue.poll(SynchronousQueue.java:941)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1073)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
"Attach Listener" #11349 daemon prio=9 os_prio=0 tid=0x00007ff51800d000 nid=0x2cb0 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"http-nio-8402-exec-12" #3103 daemon prio=5 os_prio=0 tid=0x00007ff5147d7000 nid=0xc29 waiting on condition [0x00007ff4b50d1000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000000d31e44f8> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:103)
at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:31)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
"http-nio-8402-exec-11" #3102 daemon prio=5 os_prio=0 tid=0x00007ff5143a4800 nid=0xc28 waiting on condition [0x00007ff4b51d2000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000000d31e44f8> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:103)
at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:31)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
将所有的栈信息备份到指定文件中
/ # jstack 11 >> jstack20190909
按照状态汇总并展示堆栈信息
/ # jstack 11 | grep "java.lang.Thread.State" | sort -nr | uniq -c
11 java.lang.Thread.State: WAITING (parking)
2 java.lang.Thread.State: WAITING (on object monitor)
3 java.lang.Thread.State: TIMED_WAITING (sleeping)
4 java.lang.Thread.State: TIMED_WAITING (parking)
3 java.lang.Thread.State: TIMED_WAITING (on object monitor)
11 java.lang.Thread.State: RUNNABLE
/ #
jmap
查看堆内存中存活的对象实例信息
/ # jmap -histo:live 11 | head -9
num #instances #bytes class name
----------------------------------------------
1: 901174 47357792 [C
2: 898439 21562536 java.lang.String
3: 58585 5155480 java.lang.reflect.Method
4: 7803 4511848 [B
5: 117765 3768480 java.util.concurrent.ConcurrentHashMap$Node
6: 136149 3267576 java.util.Date
-histo:live
只显示存活的对象, 其中11表示进程id,head -9
显示前9个
使用jmap备份堆内存信息
/ # jmap -dump:live,format=b,file=jmap20190909 11
Dumping heap to /jmap20190909 ...
Heap dump file created
-dump
选项可以将当前的heap信息备份下来,live
仅备份存活的对象,format=b
备份成二进制文件,file=[filedir]
备份的文件名称,11
java进程id
使用jhat分析jmap备份的内存数据,并启动一个web服务进行查看
/ # jhat -port 8899 jmap20190909
Reading from jmap20190909...
Dump file created Mon Sep 09 14:31:18 CST 2019
Snapshot read, resolving...
Resolving 3111305 objects...
Chasing references, expect 622 dots..............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Eliminating duplicate references..............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Snapshot resolved.
Started HTTP server on port 8899
Server is ready.
jmap备份后的信息,可以使用jhat进行分析,并启动一个web服务器,方便查看详细的heap信息
-port
指定启动web服务的端口,默认端口号7000,jmap20190909
为上一步jmap备份的文件名
常用的jvm启动参数
java -jar -Xms256m -Xmx512m -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:NewRatio=2 -XX:SurvivorRatio=8 -Xloggc:/app-gc.log app.jar
-Xms256m
指定最小的堆内存
-Xmx512m
指定最大的堆内存: 动态扩展最大到512m
-XX:+PrintGCDateStamps
打印gc日志,并输出时间戳
-XX:NewRatio=2
指定老年代与年轻代的内存分配比例为 2:1, 即老年代占2/3, 年轻代占1/3
-XX:SurvivorRatio=8
指定年轻代中eden区内存与survivor区内存的比例为 8:1:1
-Xloggc:/app-gc.log
指定gc日志输出的文件
gc日志内容
Java HotSpot(TM) 64-Bit Server VM (25.171-b11) for linux-amd64 JRE (1.8.0_171-b11), built on Mar 28 2018 17:07:08 by "java_re" with gcc 4.3.0 20080428 (Red Hat 4.3.0-8)
Memory: 4k page, physical 8008884k(1129856k free), swap 0k(0k free)
CommandLine flags: -XX:InitialHeapSize=536870912 -XX:MaxHeapSize=805306368 -XX:NewRatio=2 -XX:+PrintGC -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:SurvivorRatio=8 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC
2019-08-30T04:07:01.186+0000: 0.709: [GC (Allocation Failure) [PSYoungGen: 139776K->3815K(157184K)] 139776K->3823K(506880K), 0.0158590 secs] [Times: user=0.03 sys=0.00, real=0.02 secs]
2019-08-30T04:07:01.716+0000: 1.239: [GC (Allocation Failure) [PSYoungGen: 143591K->4274K(244736K)] 143599K->4354K(594432K), 0.0062708 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
2019-08-30T04:07:02.356+0000: 1.880: [GC (Allocation Failure) [PSYoungGen: 231602K->5464K(244736K)] 231682K->5552K(594432K), 0.0057272 secs] [Times: user=0.01 sys=0.01, real=0.01 secs]
2019-08-30T04:07:02.704+0000: 2.228: [GC (Allocation Failure) [PSYoungGen: 232792K->6139K(244736K)] 232880K->6235K(594432K), 0.0057959 secs] [Times: user=0.02 sys=0.00, real=0.00 secs]
2019-08-30T04:07:03.009+0000: 2.533: [GC (Allocation Failure) [PSYoungGen: 233467K->8151K(235520K)] 233563K->8255K(585216K), 0.0096851 secs] [Times: user=0.03 sys=0.00, real=0.01 secs]
2019-08-30T04:07:03.187+0000: 2.711: [GC (Metadata GC Threshold) [PSYoungGen: 103694K->6879K(236032K)] 103798K->6991K(585728K), 0.0078126 secs] [Times: user=0.02 sys=0.01, real=0.01 secs]
2019-08-30T04:07:03.195+0000: 2.719: [Full GC (Metadata GC Threshold) [PSYoungGen: 6879K->0K(236032K)] [ParOldGen: 112K->6743K(349696K)] 6991K->6743K(585728K), [Metaspace: 20923K->20923K(1069056K)], 0.0290297 secs] [Times: user=0.06 sys=0.01, real=0.03 secs]
2019-08-30T04:07:03.557+0000: 3.081: [GC (Allocation Failure) [PSYoungGen: 227328K->2371K(236544K)] 234071K->9122K(586240K), 0.0031362 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
2019-08-30T04:07:03.859+0000: 3.383: [GC (Allocation Failure) [PSYoungGen: 229699K->2796K(252928K)] 236450K->9547K(602624K), 0.0091977 secs] [Times: user=0.03 sys=0.01, real=0.01 secs]
2019-08-30T04:07:04.485+0000: 4.008: [GC (Allocation Failure) [PSYoungGen: 246508K->6477K(250368K)] 253259K->13236K(600064K), 0.0081053 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
2019-08-30T04:07:04.882+0000: 4.406: [GC (Allocation Failure) [PSYoungGen: 250189K->5376K(252928K)] 256948K->14055K(602624K), 0.0108411 secs] [Times: user=0.04 sys=0.01, real=0.01 secs]
2019-08-30T04:07:05.401+0000: 4.925: [GC (Allocation Failure) [PSYoungGen: 248576K->2195K(252416K)] 257255K->14982K(602112K), 0.0113168 secs] [Times: user=0.03 sys=0.00, real=0.02 secs]
2019-08-30T04:07:06.627+0000: 6.151: [GC (Allocation Failure) [PSYoungGen: 245395K->6632K(251904K)] 258182K->20196K(601600K), 0.0089046 secs] [Times: user=0.03 sys=0.00, real=0.01 secs]
2019-08-30T04:07:06.955+0000: 6.478: [GC (Allocation Failure) [PSYoungGen: 249320K->3013K(252416K)] 262884K->19340K(602112K), 0.0156316 secs] [Times: user=0.05 sys=0.00, real=0.01 secs]
2019-08-30T04:07:07.220+0000: 6.743: [GC (Allocation Failure) [PSYoungGen: 245701K->1897K(252416K)] 262028K->19481K(602112K), 0.0045420 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
2019-08-30T04:07:07.346+0000: 6.870: [GC (Metadata GC Threshold) [PSYoungGen: 105178K->1275K(252416K)] 122761K->19666K(602112K), 0.0042970 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]