1. Open jVisualvm. exe

Go to the JDK /bin directory and double-click jVisualvm.exe

2. Find your project

3. Check JVM memory (check memory leaks)

A heap Dump is a record of the heap. Clicking it will generate a Dump record of the current time

Memory leak code

Simulation code

import java.util.ArrayList; /** * @description: * @author: jyk * @time: 2021/7/8 17:16 */ public class overflow { static class OOMObject{ private String con; public OOMObject(String con){ this.con = con; } } public static void main(String[] args) { ArrayList<OOMObject> list = new ArrayList<>(); int a = 0; While (true){list.add(new OOMObject(" this is the first: "+ a++)); }}}Copy the code
Configuration idea JVM size and configuration generated snapshot HeapDumpOnOutOfMemoryError this is to show how to generate a snapshot to run the main method in the configuration of these - Xms10m - Xmx10m convenient point -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=D:\Copy the code

Generate a snapshot

After the local snapshot is generated, open snapshot select the generated snapshot format (.hprof) to introduce the snapshotCopy the code

Find the cause of the memory overflow. This is caused by the amount of data stored in the ArrayList and the JVM filling upCopy the code

If you look inside the subscript and you see that there's a bunch of classes stored, go to this method and see where it was called.Copy the code

Check thread deadlocks

Example code:

/** * @description: thread deadlock * @author: jyk * @time: 2021/7/12 16:43 */ public class DeadLock { private Object lock1 = new Object(); private StringBuffer lock2 = new StringBuffer(); Public void method1() throws InterruptedException {// Synchronized with Object (lock1){ System.out.println(thread.currentThread ().getName() + "Obtain lock1, request obtain lock2...." ); Thread.sleep(1000); Synchronized (lock2){system.out.println (" get lock2...."); ); }}} public void method2() throws InterruptedException {// Synchronized (lock2){ System.out.println(thread.currentThread ().getName() + "Obtain lock2, request obtain lock1...." ); Thread.sleep(1000); // synchronized (lock1){system.out.println (" get lock1....") ); } } } public static void main(String[] args) { DeadLock deadLock = new DeadLock(); New Thread(()-> {try {dead.method1 (); } catch (InterruptedException e) { e.printStackTrace(); } }).start(); new Thread(()-> { try { deadLock.method2(); } catch (InterruptedException e) { e.printStackTrace(); } }).start(); }}Copy the code
In the lower right corner is the state corresponding to the colorCopy the code

Clicking on Thread Dump generates a Deadlock following java.lang.thread. State, with an impasse within the same set waiting for a resource. Waiting on condition, Waiting on monitor entry, Object. Wait () or TIMED_WAITING, Suspended, I was Blocked from work or ParkedCopy the code

Found * deadlock.

View the thread waiting to lock, waiting for locked objects and locked objects

Reference links:

Using JVisualVM to troubleshoot a memory overflow (OOM) procedure: blog.csdn.net/xcc_2269861…

Use Jvisualvm to monitor JVM memory, CPU, threads: blog.csdn.net/weixin_3433…

Using jvisualvm. Exe tool to check the Java project of memory (heap overflow) – manufacturing of memory: www.cnblogs.com/chongyou/p/…