Java Performance tuning: Using VISUALVM for performance analysis

Source: Internet
Author: User
Tags visualvm jconsole netbeans

JVISUALVM Introduction

VisualVM is NetBeans's profile subproject, which has been self-contained in JDK6.0 Update 7, is capable of monitoring threads, memory conditions, viewing the CPU time of the method and in-memory objects, the object that has been GC, and the reverse view of the allocated stack ( For example, 100 string objects are assigned by each of the objects). Under Jdk_home/bin (default is C:\Program files\java\jdk1.6.0_13\bin) directory, there is a Jvisualvm.exe file, double-click Open, from the UI, this software is based on NetBeans developed.

VisualVM provides a visual interface for viewing detailed information about Java-based applications running on Java virtual machines (Java machine, JVM). VisualVM organizes the data about the JVM software retrieved by the Java Development Kit (JDK) tool and provides that information in a way that enables you to quickly view data about multiple Java applications. You can view data about applications that are running on a local application or on a remote host. In addition, you can capture data about a JVM software instance and save that data to a local system for later viewing or sharing with other users.

Double-click Start Jvisualvm.exe, boot up and Jconsole same as can choose local and remote, if need to monitor remote also need to configure relevant parameters.

The main interface is as follows;

VISUALVM can be installed according to the needs of different plug-ins, each plug-in has different concerns, and some of the main monitoring GC, some of the main monitoring memory, some monitoring threads and so on.

How to install:

1. Select "Tools" > "Plugins" from the main menu.
2. In the Available Plugins tab, select the Install check box for the plugin. Click Install.
3, gradually complete the plug-in installation program.

Here I take Eclipse (PID 22296) as an example, double-click to expand directly, the main interface shows the system and JVM two chunks of content, click on the lower right JVM parameters and System properties can refer to the detailed parameter information.

Because VISUALVM plug-in too many, I here mainly introduce three I mainly use a few: monitoring, threading, Visual GC

The monitored home page is actually a graph of CPU, memory, class, thread

There's no big difference between threading and jconsole features.

Visual GC is a feature that is often used to clearly see the changes in memory in the younger generation, the old age, and the GC frequency, GC time, and so on.

The above functions in fact jconsole almost, VISUALVM more comprehensive more intuitive some, in addition VISUALVM a lot of other functions, can analyze dump memory snapshot,

Dump out of the thread snapshot and analysis and so on, there are many other plugins you can explore

Case studies prepare to simulate a memory leak sample 1, define static variable HashMap2, create an object in a segmented loop, and add the HashMap code as follows:
ImportJava.util.HashMap;ImportJava.util.Map; Public classcyclicdependencies {//declaring cached Objects    Private Static FinalMap map =NewHashMap ();  Public Static voidMain (String args[]) {Try{Thread.Sleep (10000);//to open VISUALVM time}Catch(interruptedexception e) {e.printstacktrace (); }        //looping add objects to the cache         for(inti=0; i<1000000;i++) {testmemory T=Newtestmemory (); Map.put ("Key" +i,t); } System.out.println ("First"); //provide time for dump out of heap        Try{Thread.Sleep (10000); } Catch(interruptedexception e) {e.printstacktrace (); }         for(inti=0; i<1000000;i++) {testmemory T=Newtestmemory (); Map.put ("Key" +i,t); } System.out.println ("Second"); Try{Thread.Sleep (10000); } Catch(interruptedexception e) {e.printstacktrace (); }         for(inti=0; i<3000000;i++) {testmemory T=Newtestmemory (); Map.put ("Key" +i,t); } System.out.println ("Third"); Try{Thread.Sleep (10000); } Catch(interruptedexception e) {e.printstacktrace (); }         for(inti=0; i<4000000;i++) {testmemory T=Newtestmemory (); Map.put ("Key" +i,t); } System.out.println ("Forth"); Try{thread.sleep (integer.max_value); } Catch(interruptedexception e) {e.printstacktrace (); } System.out.println ("QQQQ"); }}
3. Configure JVM parameters as follows:
         -Xms512m         -Xmx512m         -XX:-UseGCOverheadLimit         -XX:MaxPermSize=50m
4, running the program and clocked VISUALVM monitoring using JVISUALVM to analyze memory leaks 1, view the Visual GC label, the content is as follows, this is the output of the first of this is the output of the Forth: 2 images compared to the Now: Laosheng generation has been in the GC, when the program continues to run can be found that the Laosheng GC continues: increased to 7 times, but the memory of Laosheng generation has not decreased. Indicates that there is an object that cannot be reclaimed, possibly a memory leak. How to analyze is that object leaking? Open the sample label: Click on the following example: According to the program output heap dump, when the output second, dump once, when the output forth dump once. Go to the last dump stack tag and click on the class: Click on the upper right corner: "Compare to another heap storage". Select the dump content comparison for the first export: the comparison results are as follows: You can see that the Testmemory object instance has been increasing and many times in two intervals, indicating that the method referenced by the object may have a memory leak. How do I see the object reference relationship? Right-Select the class Testmemory, select Show in Instance view, as shown below: The total number of instances created on the left, the structure of the instance on the right, and the reference description below, which can be seen in the class Cyclicdependencies and referenced by HashMap. This can determine the location of the leak, and then according to the actual situation to analyze and solve. JVISUALVM remotely monitor Tomcat 1 and modify the remote Tomcat's catalina.sh profile, where it adds:
    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 "
This configuration does not go through the permission check.      Just open the JMX port.      2, open JVISUALVM, right-click Remote, select Add remote host: 3, enter the name of the host, direct write IP, as follows: Right-click the new host, select Add JMX connection, enter the port configured in Tomcat. 4. Double-click to open. Complete!

Resources

53817827

Https://www.cnblogs.com/ityouknow/p/6437037.html

Java Performance Tuning: Using VISUALVM for performance analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.