准备一个测试类
package demo;
import java.util.HashMap;
import java.util.Map;
/**
* @author shawn
*/
public class TestDump {
/**
* 声明缓存对象
*/
private static final Map<String, Object> TEST_MAP = new HashMap<>();
public static void main(String[] args) {
try {
Thread.sleep(10000);
//循环添加对象到缓存
for (int i = 0; i < 4000000; i++) {
Person t = new Person();
TEST_MAP.put("key" + i, t);
}
Thread.sleep(Integer.MAX_VALUE);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Person {
public String name = "shawn";
public String address = "深圳";
}
执行jmap命令
jmap -dump:format=b,file=demo.dump 15820
image.png
使用jvisualvm进行分析
文件->装入 dump文件
image.png
可以看到person类占用内存
image.png
person类被hashmap装入了
点击查看实例
image.png
点击大小排序
image.png
可以看到引用的类的类型,就可以定位到相应代码位置分析
jstack pid命令
>jstack 20868
2021-03-11 15:58:56
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.201-b09 mixed mode):
"Service Thread" #13 daemon prio=9 os_prio=0 tid=0x000000001e7aa800 nid=0x5e64 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread3" #12 daemon prio=9 os_prio=2 tid=0x000000001e753000 nid=0x4ec0 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread2" #11 daemon prio=9 os_prio=2 tid=0x000000001e750000 nid=0x5810 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1" #10 daemon prio=9 os_prio=2 tid=0x000000001e749000 nid=0x5d54 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0" #9 daemon prio=9 os_prio=2 tid=0x000000001e746000 nid=0x34a0 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"JDWP Command Reader" #8 daemon prio=10 os_prio=0 tid=0x000000001e6c6000 nid=0x21c0 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"JDWP Event Helper Thread" #7 daemon prio=10 os_prio=0 tid=0x000000001e6c2800 nid=0x1328 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"JDWP Transport Listener: dt_socket" #6 daemon prio=10 os_prio=0 tid=0x000000001e6b7800 nid=0x423c runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000000001e658000 nid=0x4e04 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001e657000 nid=0x55d8 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000000001c85e000 nid=0x5b9c in Object.wait() [0x000000001ec1f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000006c979bc60> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
- locked <0x00000006c979bc60> (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=0x000000001e640800 nid=0x2d28 in Object.wait() [0x000000001eb1f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000006c979bca0> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x00000006c979bca0> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"main" #1 prio=5 os_prio=0 tid=0x0000000002e43800 nid=0x51f8 waiting on condition [0x0000000002c6f000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at demo.TestDump.main(TestDump.java:24)
"VM Thread" os_prio=2 tid=0x000000001c859800 nid=0x1ba4 runnable
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000000002e59000 nid=0x5118 runnable
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x0000000002e5a800 nid=0xa54 runnable
"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x0000000002e5c000 nid=0x5ca4 runnable
"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x0000000002e5d800 nid=0x4200 runnable
"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x0000000002e61000 nid=0x3a50 runnable
"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x0000000002e62000 nid=0x4f90 runnable
"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x0000000002e65000 nid=0x4850 runnable
"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x0000000002e66800 nid=0x5ae8 runnable
"VM Periodic Task Thread" os_prio=2 tid=0x000000001e7ae800 nid=0x5124 waiting on condition
JNI global references: 1476
重点关注线程状态
runnable:运行中状态,在虚拟机内部执行,可能已经获取到了锁,可以观察是否有locked字样。
blocked:被阻塞并等待锁的释放。
wating:处于等待状态,等待特定的操作被唤醒,一般停留在park(), wait(), sleep(),join() 等语句里。
time_wating:有时限的等待另一个线程的特定操作。
terminated:线程已经退出。
"main" #1 prio=5 os_prio=0 tid=0x0000000002e43800 nid=0x51f8 waiting on condition [0x0000000002c6f000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at demo.TestDump.main(TestDump.java:24)
名称为‘main’的线程当前处于等待状态,休眠时间结束后将自动唤醒