欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用VisualVM进行Java应用的性能测量 JavaSAP云平台SAP Cloud PlatformCloudCloudFoundry 

程序员文章站 2022-06-13 17:47:48
...

Recently I am trying to find a handy tool to measure the performance of my Java application and finally I think the VisualVM provided by JDK is the ideal one. This blog is written based on JDK1.7 + Eclipse 4.3.2.

What is VisualVM

It is a tool automatically available after JDK is installed. The executable file could be found on your /bin as displayed below.

 

使用VisualVM进行Java应用的性能测量
            
    
    
        JavaSAP云平台SAP Cloud PlatformCloudCloudFoundry 

 

In order to measure the performance of your application, it is necessary for the application to be recognized by VisualVM first. There is a plugin named “VisualVM launcher for Eclipse” which can help us about it.

Install and configure VisualVM launcher

(1) download the zip from visualvm.java.net/eclip. Unzip the file and put it to the plugin folder of your Eclipse installation folder. In my laptop it looks like below. There should be a site.xml inside the unzipped folder.

 

使用VisualVM进行Java应用的性能测量
            
    
    
        JavaSAP云平台SAP Cloud PlatformCloudCloudFoundry 

 

(2) In Eclipse, choose menu “Help->Install New Software”, click “Local”, add locate the folder to the one you finish in step1.

 

使用VisualVM进行Java应用的性能测量
            
    
    
        JavaSAP云平台SAP Cloud PlatformCloudCloudFoundry 

 

Then the local downloaded plugin is successfully parsed and ready for install.

 

使用VisualVM进行Java应用的性能测量
            
    
    
        JavaSAP云平台SAP Cloud PlatformCloudCloudFoundry 

 

finish the installation.

 

使用VisualVM进行Java应用的性能测量
            
    
    
        JavaSAP云平台SAP Cloud PlatformCloudCloudFoundry 

 

(3) Restart Eclipse, then you can find a new option via the path below. Configure two paths accordingly.

For “JDK Home”, if you configure the JRE path by mistake, later when you try to measure your application, the VisualVM will fail to load with the following error message:

 

使用VisualVM进行Java应用的性能测量
            
    
    
        JavaSAP云平台SAP Cloud PlatformCloudCloudFoundry 

 

Now the plugin is ready to use.

Do performance measurement

(1) Select your Java project, choose context menu “Run as”->”Run configuration”, create a new Application configuration by specifying VisualVM launcher as its launcher, instead of the default Eclipse JDT launcher.

 

使用VisualVM进行Java应用的性能测量
            
    
    
        JavaSAP云平台SAP Cloud PlatformCloudCloudFoundry 

 

(2) For example I have a Java application which sorts an array by QuickSort algorithm written by myself and I would like to get its performance data, then I set a breakpoint on line 57, before my main logic is executed. Then launch the application in debugging mode with the application configuration created in previous step. Afterwards VisualVM will automatically be launched and successfully recognize the execution of my application. Click Profiler tab:

 

使用VisualVM进行Java应用的性能测量
            
    
    
        JavaSAP云平台SAP Cloud PlatformCloudCloudFoundry 

 

Current status: profiling inactive. Click CPU button:

 

使用VisualVM进行Java应用的性能测量
            
    
    
        JavaSAP云平台SAP Cloud PlatformCloudCloudFoundry 

 

Now profiling is activated:

 

使用VisualVM进行Java应用的性能测量
            
    
    
        JavaSAP云平台SAP Cloud PlatformCloudCloudFoundry 

 

(3) Go back to Eclipse and click F8 to finish execution. Once finished, VisualVM will immediately capture this event and notify you. Just click Yes to get performance result.

 

使用VisualVM进行Java应用的性能测量
            
    
    
        JavaSAP云平台SAP Cloud PlatformCloudCloudFoundry 

 

The result is displayed as below:

 

使用VisualVM进行Java应用的性能测量
            
    
    
        JavaSAP云平台SAP Cloud PlatformCloudCloudFoundry 

 

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

使用VisualVM进行Java应用的性能测量
            
    
    
        JavaSAP云平台SAP Cloud PlatformCloudCloudFoundry