• JVisualVM profile
  • Case analysis
  • Prepare simulated memory leak samples
  • Analyze memory leaks using JVisualVM
  • JVisualVM remotely monitors Tomcat

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

Case analysis

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 to add objects to the cachefor(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:

  1. JAVA_OPTS=”$JAVA_OPTS
  2. – Djava. Rmi. Server hostname = 192.168.122.128
  3. -Dcom.sun.management.jmxremote.port=18999
  4. -Dcom.sun.management.jmxremote.ssl=false
  5. – Dcom. Sun. Management. Jmxremote. Authenticate = false “the configuration don’t walk permission to check first. Just open the JMX port. 2, Open JVisualVM, right-click remote, and select Add Remote Host:

3. Enter the host name and write the host IP address as follows:

Right-click the newly created host, select Add JMX Connection, and enter the port configured in Tomcat.

4. Double-click to open it. Finished!