【基于JDK命令行工具的监控】

1. JVM 的参数类型

1.1 标准参数

-help
-server -client

java client模式_JVM的Client模式与Server模式

-version -showversion
-cp -classpath

1.2 x参数

非标准化参数:在各个JVM的版本中可能会变。

-Xint:解释执行

在这里插入图片描述

-Xcomp:第一次使用就编译成本地代码

在这里插入图片描述

-Xmixed:混合模式,JVM自己来决定是否编译成本地代码

在这里插入图片描述

1.3 xx参数

  • 非标准转化参数
  • 相对不稳定
  • 主要用于JVM调优和Debug

Boolean类型

格式: -XX:[±] < name>表示启用或禁用name属性
比如: -XX:+ UseConcMarkSweepGC
         -XX:+ UseG1GC

非Boolean类型

格式: -XX:< name>= < value>表示name属性的值是value
比如: -XX:MaxGCPauseMillis = 500
         -XX:GCTineRatio = 19

-Xmx -Xms

不是X参数,而是XX参数

-Xms等价于:-XX:InitialHeapSize
-Xmx等价于:-XX:MaxHeapSize

2. 运行时JVM参数查看

-XX:+PrintFlagsInitial

查看初始值

-XX:+PrintFlagsFinal

查看最终值
【基于JDK命令行工具的监控】_第1张图片

=表示默认值
=:表示被用户活着JVM修改后的值

-XX:+UnlockExperimentalVMOptions

解锁实验参数

-XX:+UnlockDiagnosticVMOPtions

解锁诊断参数

-XX:+PrintCommandLineFlags

打印命令行参数

java -XX:+PrintFlagsFinal -version > flag_info.txt

将最终参数输出到文件

jps

【基于JDK命令行工具的监控】_第2张图片

jinfo

jinfo -flag MaxHeapSize 28798[进程号]
jinfo -flags 28798[进程号]

在这里插入图片描述

查看最大内存

jinfo -flag MaxHeapSize 14008

在这里插入图片描述
查看垃圾回收器

jinfo -flag UseConcMarkSweepGC 14008

在这里插入图片描述

jinfo -flag UseG1GC 14008

在这里插入图片描述

jinfo -flag UseParallelGC 14008

在这里插入图片描述

3. jstat查看虚拟机统计信息

监控 Java 虚拟机 (JVM) 统计信息。 此命令是实验性的,不受支持

命令格式:

jstat -help
options: -class(类加载), -compiler(编译信息),-gc,-printcompilation

3.1 类装载

jstat -class 14008 1000 10

1000:单位:ms,每隔多久
10:一共输出10次

【基于JDK命令行工具的监控】_第3张图片

3.2 垃圾收集

-gc 、-gcutil、-gccause、-gcnew、-gcold
jstat -gc 14008 1000 10

【基于JDK命令行工具的监控】_第4张图片

S0C、S1C、S0U、S1U:S0和S1的总量与使用量
EC、EU:Eden区总量与使用量
OC、OU:Old区总量与使用量
MC、MU:Metaspace区总量与使用量
CCSC、CCSU:压缩类空间总量与使用量
YGC、YGCT:YoungGC的次数与时间
FGC、FGCT:FullGC的次数与时间
GCT:总的GC时间

-gccause:此选项显示与 -gcutil 选项相同的垃圾收集统计信息摘要,但包括上次垃圾收集事件的原因和(如果适用)当前垃圾收集事件。

-gcutil:垃圾收集统计信息摘要。

JVM的内存结构

【基于JDK命令行工具的监控】_第5张图片

3.3 JIT编译

-compiler、-printcompilation

-comilper:Java HotSpot VM 即时编译器统计信息。

在这里插入图片描述

-printcompilation:Java HotSpot VM 编译器方法统计。

4. jmap + MAT实战内存溢出

堆(Heap):通过new或者其它方式创建的实例对象(包括数组)。如果这些对象不再被引用会被垃圾回收。堆的优势就是在运行时动态分配。

代码示例

/**
 * -Xmx32M -Xms32M
 *
 *  -Xmx    --> -XX:MaxHeapSize.
 *  -Xms    --> -XX:InitialHeapSize
 * */
public String heap(){


	List<User> userList = new ArrayList<>();

	AtomicInteger atomicInteger = new AtomicInteger(0);

	while (true){

		User user = new User(atomicInteger.incrementAndGet(),"大牛" + atomicInteger.get());

		userList.add(user);
		
		if(atomicInteger.get() % 100000 ==0){
			System.out.println(atomicInteger.get());
		}
	}
}

JVM参数设置
【基于JDK命令行工具的监控】_第6张图片
运行结果

100000
200000
300000
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at java.util.Arrays.copyOf(Arrays.java:3210)
	at java.util.Arrays.copyOf(Arrays.java:3181)
	at java.util.ArrayList.grow(ArrayList.java:265)
	at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:239)
	at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:231)
	at java.util.ArrayList.add(ArrayList.java:462)
	at com.example.demo.controller.MemoryController.heap(MemoryController.java:31)
	at com.example.demo.controller.MemoryController.main(MemoryController.java:59)

代码示例

/**
 * -XX:MetaspaceSize=32M -XX:MaxMetaspaceSize=32M
 * */
public String nonHeap(){

	List<Class<?>> classes = new ArrayList<>();

	AtomicInteger atomicInteger = new AtomicInteger(0);

	while (true){
		classes.addAll(Metaspace.generate());
	}
}
package com.example.demo.entity;

import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

import java.util.ArrayList;
import java.util.List;

public class Metaspace extends ClassLoader{
	public static List<Class<?>> generate(){
		// 类持有
		List<Class<?>> classes = new ArrayList<Class<?>>();
		// 循环1000w次生成1000w个不同的类。
		for (int i = 0; i < 10000000; ++i) {
			ClassWriter cw = new ClassWriter(0);
			// 定义一个类名称为Class{i},它的访问域为public,父类为java.lang.Object,不实现任何接口
			cw.visit(Opcodes.V1_1, Opcodes.ACC_PUBLIC, "Class" + i, null,
					"java/lang/Object", null);
			// 定义构造函数方法
			MethodVisitor mw = cw.visitMethod(Opcodes.ACC_PUBLIC, "",
					"()V", null, null);
			// 第一个指令为加载this
			mw.visitVarInsn(Opcodes.ALOAD, 0);
			// 第二个指令为调用父类Object的构造函数
			mw.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object",
					"", "()V");
			// 第三条指令为return
			mw.visitInsn(Opcodes.RETURN);
			mw.visitMaxs(1, 1);
			mw.visitEnd();

			Metaspace test = new Metaspace();
			byte[] code = cw.toByteArray();
			// 定义类
			Class<?> exampleClass = test.defineClass("Class" + i, code, 0, code.length);
			classes.add(exampleClass);
		}
		return classes;
	}
	public static void main(String[] args) {

	}
}

<dependency>
	<groupId>asmgroupId>
	<artifactId>asmartifactId>
	<version>3.3.1version>
dependency>

JVM参数设置
【基于JDK命令行工具的监控】_第7张图片
运行结果

Exception in thread "main" java.lang.OutOfMemoryError: Metaspace
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:642)
	at com.example.demo.entity.Metaspace.generate(Metaspace.java:38)
	at com.example.demo.controller.MemoryController.nonHeap(MemoryController.java:50)
	at com.example.demo.controller.MemoryController.main(MemoryController.java:59)

如何导出内存映像文件

内存逸出自动导出

-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=./

使用jump命令手动导出

jmap -help
option: -help, -clstats,-dump:<dump-options>,-F

jamp -dump:<dump-options> <pid>

jmap -dump:format=b,file=D:\temp\heap.20220361631.hprof 13408

MAT分析内存溢出

MAT下载

5. jstack实战死循环与死锁

jps -l(显示应用程序主类的完整包名或应用程序JAR文件的完整路径名。)
jstack 14008 > 14008.txt
2022-03-27 13:25:21
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.201-b09 mixed mode):

"JMX server connection timeout 39" #39 daemon prio=5 os_prio=0 tid=0x0000000019ae5000 nid=0x10cc in Object.wait() [0x000000001bc4f000]
   java.lang.Thread.State: TIMED_WAITING (on object monitor)
	at java.lang.Object.wait(Native Method)
	- waiting on <0x00000000ffc90178> (a [I)
	at com.sun.jmx.remote.internal.ServerCommunicatorAdmin$Timeout.run(ServerCommunicatorAdmin.java:168)
	- locked <0x00000000ffc90178> (a [I)
	at java.lang.Thread.run(Thread.java:748)

"DestroyJavaVM" #35 prio=5 os_prio=0 tid=0x0000000019ae2000 nid=0x1ed4 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"http-nio-8080-Acceptor" #33 daemon prio=5 os_prio=0 tid=0x0000000019ae1000 nid=0x46e0 runnable [0x000000001c24e000]
   java.lang.Thread.State: RUNNABLE
	at sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method)
	at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:422)
	at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:250)
	- locked <0x00000000fe778cc0> (a java.lang.Object)
	at org.apache.tomcat.util.net.NioEndpoint.serverSocketAccept(NioEndpoint.java:467)
	at org.apache.tomcat.util.net.NioEndpoint.serverSocketAccept(NioEndpoint.java:73)
	at org.apache.tomcat.util.net.Acceptor.run(Acceptor.java:95)
	at java.lang.Thread.run(Thread.java:748)

"http-nio-8080-ClientPoller" #32 daemon prio=5 os_prio=0 tid=0x0000000019adf000 nid=0x28cc runnable [0x000000001c14f000]
   java.lang.Thread.State: RUNNABLE
	at sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method)
	at sun.nio.ch.WindowsSelectorImpl$SubSelector.poll(WindowsSelectorImpl.java:296)
	at sun.nio.ch.WindowsSelectorImpl$SubSelector.access$400(WindowsSelectorImpl.java:278)
	at sun.nio.ch.WindowsSelectorImpl.doSelect(WindowsSelectorImpl.java:159)
	at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
	- locked <0x00000000fe77f6c0> (a sun.nio.ch.Util$3)
	- locked <0x00000000fe77f6b0> (a java.util.Collections$UnmodifiableSet)
	- locked <0x00000000fe77f098> (a sun.nio.ch.WindowsSelectorImpl)
	at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
	at org.apache.tomcat.util.net.NioEndpoint$Poller.run(NioEndpoint.java:712)
	at java.lang.Thread.run(Thread.java:748)

"http-nio-8080-exec-10" #31 daemon prio=5 os_prio=0 tid=0x0000000019ae0800 nid=0x5574 waiting on condition [0x000000001c04e000]
   java.lang.Thread.State: WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000000fe7847f8> (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:107)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:33)
	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-8080-exec-9" #30 daemon prio=5 os_prio=0 tid=0x0000000019ae3800 nid=0x1710 waiting on condition [0x000000001bf4f000]
   java.lang.Thread.State: WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000000fe7847f8> (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:107)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:33)
	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-8080-exec-8" #29 daemon prio=5 os_prio=0 tid=0x0000000019ae2800 nid=0x5148 waiting on condition [0x000000001be4f000]
   java.lang.Thread.State: WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000000fe7847f8> (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:107)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:33)
	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-8080-exec-7" #28 daemon prio=5 os_prio=0 tid=0x0000000019ae4000 nid=0x53ec waiting on condition [0x000000001bd4e000]
   java.lang.Thread.State: WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000000fe7847f8> (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:107)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:33)
	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-8080-exec-6" #27 daemon prio=5 os_prio=0 tid=0x0000000016852800 nid=0x524c waiting on condition [0x000000001bb4e000]
   java.lang.Thread.State: WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000000fe7847f8> (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:107)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:33)
	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-8080-exec-5" #25 daemon prio=5 os_prio=0 tid=0x0000000016854800 nid=0x1fac waiting on condition [0x000000001ba4e000]
   java.lang.Thread.State: WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000000fe7847f8> (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:107)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:33)
	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-8080-exec-4" #24 daemon prio=5 os_prio=0 tid=0x0000000016859000 nid=0x16a8 waiting on condition [0x000000001b94e000]
   java.lang.Thread.State: WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000000fe7847f8> (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:107)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:33)
	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-8080-exec-3" #23 daemon prio=5 os_prio=0 tid=0x0000000016858800 nid=0x4120 waiting on condition [0x000000001b84f000]
   java.lang.Thread.State: WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000000fe7847f8> (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:107)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:33)
	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-8080-exec-2" #22 daemon prio=5 os_prio=0 tid=0x0000000016857000 nid=0x229c waiting on condition [0x000000001b74e000]
   java.lang.Thread.State: WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000000fe7847f8> (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:107)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:33)
	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-8080-exec-1" #21 daemon prio=5 os_prio=0 tid=0x0000000016856000 nid=0x2e48 waiting on condition [0x000000001b64e000]
   java.lang.Thread.State: WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000000fe7847f8> (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:107)
	at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:33)
	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-8080-BlockPoller" #20 daemon prio=5 os_prio=0 tid=0x0000000016857800 nid=0x3210 runnable [0x000000001b44e000]
   java.lang.Thread.State: RUNNABLE
	at sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method)
	at sun.nio.ch.WindowsSelectorImpl$SubSelector.poll(WindowsSelectorImpl.java:296)
	at sun.nio.ch.WindowsSelectorImpl$SubSelector.access$400(WindowsSelectorImpl.java:278)
	at sun.nio.ch.WindowsSelectorImpl.doSelect(WindowsSelectorImpl.java:159)
	at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
	- locked <0x00000000fe773a38> (a sun.nio.ch.Util$3)
	- locked <0x00000000fe773a28> (a java.util.Collections$UnmodifiableSet)
	- locked <0x00000000fe7738b8> (a sun.nio.ch.WindowsSelectorImpl)
	at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
	at org.apache.tomcat.util.net.NioBlockingSelector$BlockPoller.run(NioBlockingSelector.java:313)

"RMI Scheduler(0)" #19 daemon prio=5 os_prio=0 tid=0x0000000016854000 nid=0x36b0 waiting on condition [0x000000001b34e000]
   java.lang.Thread.State: TIMED_WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000000fe6dd0b0> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
	at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
	at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)
	at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
	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 java.lang.Thread.run(Thread.java:748)

"RMI TCP Connection(idle)" #18 daemon prio=5 os_prio=0 tid=0x0000000016855800 nid=0x2778 waiting on condition [0x000000001b24e000]
   java.lang.Thread.State: TIMED_WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000000fe6dd208> (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)

"RMI TCP Accept-0" #17 daemon prio=5 os_prio=0 tid=0x0000000019f89000 nid=0x35e0 runnable [0x000000001af4f000]
   java.lang.Thread.State: RUNNABLE
	at java.net.DualStackPlainSocketImpl.accept0(Native Method)
	at java.net.DualStackPlainSocketImpl.socketAccept(DualStackPlainSocketImpl.java:131)
	at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:409)
	at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:199)
	- locked <0x00000000fe6e06f8> (a java.net.SocksSocketImpl)
	at java.net.ServerSocket.implAccept(ServerSocket.java:545)
	at java.net.ServerSocket.accept(ServerSocket.java:513)
	at sun.management.jmxremote.LocalRMIServerSocketFactory$1.accept(LocalRMIServerSocketFactory.java:52)
	at sun.rmi.transport.tcp.TCPTransport$AcceptLoop.executeAcceptLoop(TCPTransport.java:405)
	at sun.rmi.transport.tcp.TCPTransport$AcceptLoop.run(TCPTransport.java:377)
	at java.lang.Thread.run(Thread.java:748)

"container-0" #16 prio=5 os_prio=0 tid=0x000000001721b000 nid=0x5b78 waiting on condition [0x000000001ac4f000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
	at java.lang.Thread.sleep(Native Method)
	at org.apache.catalina.core.StandardServer.await(StandardServer.java:570)
	at org.springframework.boot.web.embedded.tomcat.TomcatWebServer$1.run(TomcatWebServer.java:181)

"Catalina-utility-2" #15 prio=1 os_prio=-2 tid=0x000000001721a000 nid=0x4ff8 waiting on condition [0x000000001a87e000]
   java.lang.Thread.State: WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000000fe607840> (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.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1088)
	at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
	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)

"Catalina-utility-1" #14 prio=1 os_prio=-2 tid=0x0000000019bf8800 nid=0x3ac4 waiting on condition [0x00000000196ff000]
   java.lang.Thread.State: TIMED_WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000000fe607840> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
	at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
	at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)
	at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
	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)

"Service Thread" #10 daemon prio=9 os_prio=0 tid=0x0000000014f27800 nid=0x3710 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C1 CompilerThread3" #9 daemon prio=9 os_prio=2 tid=0x0000000014ea8800 nid=0x2930 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread2" #8 daemon prio=9 os_prio=2 tid=0x0000000014e9c800 nid=0x2248 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread1" #7 daemon prio=9 os_prio=2 tid=0x0000000014e93000 nid=0x4610 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" #6 daemon prio=9 os_prio=2 tid=0x0000000014e90800 nid=0x530 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x0000000014e3a800 nid=0x322c waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x0000000014e3a000 nid=0x11ec runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x0000000014e21000 nid=0x3034 in Object.wait() [0x00000000153ff000]
   java.lang.Thread.State: WAITING (on object monitor)
	at java.lang.Object.wait(Native Method)
	- waiting on <0x00000000fe222cb0> (a java.lang.ref.ReferenceQueue$Lock)
	at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
	- locked <0x00000000fe222cb0> (a java.lang.ref.ReferenceQueue$Lock)
	at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
	at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216)

"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x0000000014e20800 nid=0x2da0 in Object.wait() [0x00000000152ff000]
   java.lang.Thread.State: WAITING (on object monitor)
	at java.lang.Object.wait(Native Method)
	- waiting on <0x00000000fe1628f8> (a java.lang.ref.Reference$Lock)
	at java.lang.Object.wait(Object.java:502)
	at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
	- locked <0x00000000fe1628f8> (a java.lang.ref.Reference$Lock)
	at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)

"VM Thread" os_prio=2 tid=0x0000000013028800 nid=0x4790 runnable 

"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000000002839800 nid=0x33ec runnable 

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x000000000283b800 nid=0x2468 runnable 

"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x000000000283d000 nid=0x898 runnable 

"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x000000000283e800 nid=0x329c runnable 

"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x0000000002840800 nid=0x2db8 runnable 

"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x0000000002842000 nid=0x114c runnable 

"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x0000000002845000 nid=0x352c runnable 

"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x0000000002846000 nid=0x33f4 runnable 

"VM Periodic Task Thread" os_prio=2 tid=0x0000000014f49800 nid=0x2934 waiting on condition 

JNI global references: 1074


prio:线程优先级
os_prio:系统线程优先级
http-nio-8080-exec-:工作线程
C1 CompilerThread:JIT后台编译;将Java代码编译成native代码

实战死循环导致CPU飙高

package com.whaleson.solar.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class LoopController {

    @GetMapping("/loop")
    public String loop(){
        String data = "{\"data\":[{\"partnerid\":}]";

        List<Long> partneridsFromJson = getPartneridsFromJson(data);
        return "666";
    }
    private static List<Long> getPartneridsFromJson(String data){
        //{\"data\":[{\"partnerid\":982,\"count\":\"10000\",\"cityid\":\"11\"},{\"partnerid\":983,\"count\":\"10000\",\"cityid\":\"11\"},{\"partnerid\":984,\"count\":\"10000\",\"cityid\":\"11\"}]}
        //上面是正常的数据
        List<Long> list = new ArrayList<Long>(2);
        if(data == null || data.length() <= 0){
            return list;
        }
        int datapos = data.indexOf("data");
        if(datapos < 0){
            return list;
        }
        int leftBracket = data.indexOf("[",datapos);
        int rightBracket= data.indexOf("]",datapos);
        if(leftBracket < 0 || rightBracket < 0){
            return list;
        }
        String partners = data.substring(leftBracket+1,rightBracket);
        if(partners == null || partners.length() <= 0){
            return list;
        }
        while(partners!=null && partners.length() > 0){
            int idpos = partners.indexOf("partnerid");
            if(idpos < 0){
                break;
            }
            int colonpos = partners.indexOf(":",idpos);
            int commapos = partners.indexOf(",",idpos);
            if(colonpos < 0 || commapos < 0){
                //partners = partners.substring(idpos+"partnerid".length());//1
                continue;
            }
            String pid = partners.substring(colonpos+1,commapos);
            if(pid == null || pid.length() <= 0){
                //partners = partners.substring(idpos+"partnerid".length());//2
                continue;
            }
            try{
                list.add(Long.parseLong(pid));
            }catch(Exception e){
                //do nothing
            }
            partners = partners.substring(commapos);
        }
        return list;
    }
}

top -p <pid> -H:(-H:按照线程查看)

【Linux】Ubuntu查看CPU个数及其核心数

load average数据是每隔5秒钟检查一次活跃的进程数,然后根据这个数值算出来的。如果这个数除以CPU 的数目,结果高于5的时候就表明系统在超负荷运转了。
【基于JDK命令行工具的监控】_第8张图片

你可能感兴趣的:(JDK命令行工具)