Author: toward the light rain have dust source: www.cnblogs.com/xifengxiaom…
JVisualVM profile
VisualVM is a Netbeans profile subproject that comes with JDK6.0 Update 7. It can monitor threads, memory status, view methods’ CPU times and in-memory objects, GC objects, Look backwards at the allocation stack (for example, by which 100 strings are allocated). In the JDK_HOME/bin directory (default is C: Program Files\Java\jdk1.6.0_13\bin), there is a jVisualvm. exe file.
VisualVM provides a visual interface for viewing detailed information about Java technology-based applications running on the Java Virtual Machine (JVM). VisualVM organizes the DATA related to JVM software retrieved by Java Development Kit (JDK) tools and provides this information in a way that allows you to quickly view data about multiple Java applications.
You can view data about local applications or applications running on remote hosts. In addition, you can capture data about JVM software instances and save that data to your local system for later viewing or sharing with other users.
Double-click to start JVisualvm. exe. After starting up, you can choose local and remote as well as JConsole. If you need to monitor remote, you also need to configure related parameters.
The main interface is as follows:
VisualVM can install different plug-ins as needed, each with a different focus, some mainly monitoring GC, some mainly monitoring memory, some monitoring threads, etc.
How to install:
1. Choose Tools > Plug-ins from the main menu.
2. On the Available Plug-ins TAB page, select the Install check box of the plug-in. Click Install.
Step by step, complete the plug-in installation program.
Here I take Eclipse(PID 22296) as an example. Double-click it and expand it directly. The main interface displays two large pieces of content: system and JVM.
Because there are so many plug-ins for VisualVM, I will introduce three that I mainly use: monitoring, threading and Visual GC
The home page of the monitor is essentially a chart of CPU, memory, classes, and threads
Threads and JConsole functionality are not much different
Visual GC is a frequently used feature that allows you to clearly see the memory changes of the young generation and the old generation, as well as the GC frequency, GC time, etc.
Jconsole has almost all of the above features, VisualVM is more comprehensive and intuitive, and VisualVM has a lot of other features, such as analyzing memory snapshots of dumps,
Dump out of the thread snapshot and analysis, as well as many other plug-ins you can explore
Prepare simulated memory leak samples
Define the static variable HashMap
Create objects in a piecewise loop and add them to the HashMap
The code is as follows:
import java.util.HashMap; import java.util.Map; Public class CyclicDependencies {private static final Map = new HashMap(); public static void main(String args[]){ try { Thread.sleep(10000); // Give visualVM time to open} catch (InterruptedException e) {e.printStackTrace(); } // loop for(int I =0; i<1000000; i++){ TestMemory t = new TestMemory(); map.put("key"+i,t); } System.out.println("first"); Thread.sleep(10000); thread.sleep (10000); } catch (InterruptedException e) { e.printStackTrace(); } for(int i=0; i<1000000; i++){ TestMemory t = new TestMemory(); map.put("key"+i,t); } System.out.println("second"); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } for(int i=0; i<3000000; i++){ TestMemory t = new TestMemory(); map.put("key"+i,t); } System.out.println("third"); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } for(int i=0; i<4000000; i++){ TestMemory t = new TestMemory(); map.put("key"+i,t); } System.out.println("forth"); try { Thread.sleep(Integer.MAX_VALUE); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("qqqq"); }}Copy the code
3. Set the JVM parameters as follows:
-Xms512m
-Xmx512m
-XX:-UseGCOverheadLimit
-XX:MaxPermSize=50m
Copy the code
4. Run the program and punch visualVM monitoring
Analyze memory leaks using JVisualVM
1. Look at the Visual GC TAB, which reads as follows. This is a screenshot of output FIRST
Here is a screenshot of output Forth:
Through the comparison of the two graphs, it is found that:
The old generation is still gc, and when the program continues to run, it can be found that the old generation is still GC:
Increased to 7 times, but the old generation memory did not decrease. There are objects that cannot be reclaimed. The possible cause is a memory leak.
How to analyze which object is leaking? Open the sampler TAB: click as shown below:
Heap dump is performed on the program output, once on output second and once on output Forth.
To go to the last heap tag dumped, click class:
Click on the upper right: “Compare to another heap.” > dump > dump > dump > dump > dump
The comparison results are as follows:
You can see that the TestMemory object instances have been increasing and multiplying between the two intervals, indicating that the methods referenced by the object may be leaking memory.
How do I view object reference relationships?
Right-click the TestMemory class and select “Show in Instance View”, as shown below:
CyclicDependencies is a reference to the CyclicDependencies class and is referenced by the HashMap.
In this way, the location of leakage can be determined, and then analyzed and solved according to the actual situation.
JVisualVM remotely monitors Tomcat
1. Add the following information to the catalina.sh configuration file of remote Tomcat:
- JAVA_OPTS = "$JAVA_OPTS - Djava. Rmi. Server hostname = 192.168.122.128 - Dcom. Sun. Management jmxremote. Port = 18999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"Copy the code
This configuration does not require permission verification. Just open the JMX port.
Open JVisualVM, right-click Remote and select Add Remote Host:
Enter the host name and write the IP address as follows:
Right-click the newly created host, select Add JMX Connection, and enter the port configured in Tomcat.
Double-click to open it. Finished!
Recent hot articles recommended:
1.1,000+ Java Interview Questions and Answers (2021)
2. I finally got the IntelliJ IDEA activation code thanks to the open source project. How sweet!
3. Ali Mock is officially open source, killing all Mock tools on the market!
4.Spring Cloud 2020.0.0 is officially released, a new and disruptive version!
5. “Java Development Manual (Songshan version)” the latest release, quick download!
Feel good, don’t forget to click on + forward oh!