In-depth understanding of the Java virtual machine - how to take advantage of high concurrency VisualVM project performance analysis

Depth understanding of the Java Virtual Machine series Full Article update ...

Front in learning knowledge of the JVM, generally requires the use of relevant parameters for analysis, and the analysis generally need to use some analysis tools, because the general use of IDEA, IDEA and VisualVM for good, so we chose to analyze the JVM performance VisualVM , this article will explain how to use VisualVM performance analysis , as well as prior to analysis need to know some principles of GC-optimized , GC optimization purposes , as well as methods how to solve problems when they encounter problems .

1 Why do we need

Development of large-scale Java application process will inevitably encounter memory leaks, performance bottlenecks and other issues, such as connection files, networks, databases unreleased, non-optimized algorithms. With the continuous operation of the application may result in reduced efficiency of the whole system running, it will cause a serious crash. To find a program to hide these problems, often late in the project to develop the use of performance analysis tools to analyze and optimize the performance of your application.

VisualVM is a free performance analysis tools. It is real-time data from a program running in a variety of ways jvmstat, JMX, SA (Serviceability Agent) Attach API and the like, so that the dynamic performance analysis. At the same time, it can automatically select the faster and more lightweight technology to minimize the impact on application performance analysis result, improve the accuracy of performance analysis.

2 How to Install

There are two ways:

  • Did not follow the IDEA plug-in

If you did not follow the IDEA plug-in, we need to find to find the JDK implementation of procedures in accordance with the following directory bin.

Then double-click execution, the interface will appear, as follows;

However, we generally use IDEA, so will the use of plug-ins, is the following in this way.

  • According to IDEA plug-in

First find VisualVM plug-in installed;

After installation, the place running VisualVM will run two more buttons appear;

After this run the program, it can automatically open the VisualVM program.

3 Basic Introduction

The first part is a brief introduction to this tool, look at what we will use the basic functions there.

Without the addition of other plug-ins when it is only a few functions below.

3.1 Overview

As shown, basically outlined above chart our system properties, set the JVM parameters when you run the program to present information, etc. So, this part allows us to view this information.

3.2 monitoring

Monitoring function of this interface is still very role, you can see the cup run, the dynamics of the heap usage, as well as the case of the class of thread .

Therefore, we can use this interface to see good cpu bad situation, more importantly, we can see the use of the heap, which for us is very important to analyze JVM.

3.3 thread

As FIG. Therefore, the situation can be seen that all the threads are running, sleeping, waiting, resident, monitoring and so on.

Note that these are not the key, the key is VisualVM There is also a very important function, you can add plug-ins get more features.

3.4 plug-in add

It is because of the extended functionality with plug-ins, so this tool is so powerful, VisualVM can do the following:

  • Display configuration of the virtual machine process and process environmental information, jps, jinfo.
  • Monitor the application of cpu, GC, heap, the method area as well as information threads (jstat, jstack).
  • heap dump analysis and transfer stored snapshot (jmap, jhat).
  • There are many other features.

Find the available plug-ins>, can be installed - in tools.

The next section on the use of already installed plug-ins Visual GCfor analysis.

4 using the Visual GCanalysis of virtual machine memory area

This section will use some of the basics of the Java virtual machine, so before you can view this section, please see this article:

In this interface is divided into the following sections.

  • space (Metaspace (metadata), Old old era, the Cenozoic (Eden, S0, S1))
  • Graphs (Compile Time (compile time), Class Loader Time (class load time), GC Time (garbage collection time), Eden Space, Survivor 0, Survivor 1, Old Gen, Metaspace)
  • Histogram (Parameters Parameter Setting)

So after know these parameters, how to analyze the virtual machine running in the end it is good or bad , this time, we need to understand some optimization knowledge-based Java virtual machine.

First, we need to understand some principles of GC optimization .

  • Most Java applications do not need to optimize the GC on the server;
  • Most of the problems leading to GC Java application, not because we set the parameter error, but code issues;
  • Before on-line applications, consider the JVM is set to the optimum parameters of the machine (most suited);
  • Reduce the number of created objects;
  • Reduce the use of global variables and large objects;
  • GC optimization is a means of last resort to the last used;
  • In actual use, GC analysis of much more than the case of optimized code GC parameter optimization;

In addition, we need to know our purpose GC optimization .

  • The number of objects will be transferred to the old age is reduced to a minimum;
  • Reduce the execution time of full GC;

Generally, the following points we need to do;

  • Reduce the use of global variables and large objects;
  • Resize most suitable to the new generation;
  • Set old's size is most appropriate;
  • GC select the appropriate collector;

As for how to calculate the right, behind I will explain through an example.

In fact, if you want to know more about the principles of JVM memory allocation and recovery strategies , you can view this article: Principle JVM memory allocation and recovery strategies .

After we execute our general program, the next step is need to check the status of the GC , and then analyze the results to determine the need for optimization .

If you achieve the following general indicators , it does not require a GC.

  • Minor GCLess than execution time 50ms, Minor GCdo not frequently, about 10once every second;
  • Full GCLess than execution time 1s, Full GCthe execution frequency is not frequent, not less than 10once per minute;

Example 1

We look at an example of a state of GC to be optimized, in this example, we give the heap allocation maximum or minimum values are 64M(small heap size).

GC Analysis poor condition
/**
 * VM Args:-Xms64m -Xmx64m -XX:+HeapDumpOnOutOfMemoryError
 * @author 欧阳思海
 */
public class HeapTest {

    static class StaticObject {
    }

    public static void main(String[] args) {
        List<StaticObject> list = new ArrayList<StaticObject>();
        int i = 1;

        //不断的向堆中添加对象
        while (true) {
            list.add(new StaticObject());
            i++;
            System.out.println(i);
            System.out.println(list.size());
        }
    }
}
复制代码

Because of allocated heap memory is too small, resulting in, stack overflow.

Then we look at Visual GCmonitoring the situation.

  • Interface monitoring the situation

We can see from the use of the heap, it has basically been used up.

  • Visual GC to monitor the situation

Seen from the figure, the operation in a short time, Eden was the GC 49, although the time is short, but can explain a problem, new generation space allocated heap memory is too small, resulting in frequent the GC .

Meanwhile, Old years old also conducted 33 GC, although the running time is also within the scope of optimization is not required, but can be seen from Survivor, basically no GC, a description of these objects are large, direct access to the Old years old, resulting in GC frequently .

So, we need optimization is to increase the old and the new generation's heap memory size, while reducing a large object .

Optimization parameters

We will change VM parameters: -Xms512m -Xmx512m -Xmn128m -XX:+HeapDumpOnOutOfMemoryErrorrunning about five minutes to see the results again.

First heap overflow does not occur.

  • Monitor the situation

It increased the heap memory, heap memory so no problems.

  • Visual GC to monitor the situation

After increasing the heap memory, Eden Cenozoic were 66 times GC, using a time 3.381s basically meet the requirements (execution time of less than 50ms, Minor GC do not frequently, for about 10 seconds), while the old were 2 years old GC, using a time 4.127s, here is yet to be optimized, is unlikely to meet the optimization requirements.

  • dump file analysis

Click heap dump this button will generate a dump file, we can analyze some instances of classes and objects.

After the analysis found, StaticObject most objects, no GC , the main issue here, so the next step is to solve this problem.

Can explain a problem by the above analysis, increased the heap memory after, GC case of the new generation and the old age greatly improved, but there is the problem of large objects , it has yet to be optimized.

Modify large object, GC

Modify the program as follows:

/**
 * VM Args:-Xms512m -Xmx512m -Xmn128m -XX:+HeapDumpOnOutOfMemoryError
 *
 * @author 欧阳思海
 */
public class HeapTest {

   /* static class StaticObject {
    }*/

    public static void main(String[] args) {
        int i = 1;

        while (true) {
            i++;
            System.out.println(i);
        }

    }
}

复制代码
  • Monitor the situation

Heap has been running well.

  • Visual GC to monitor the situation

Last compared to the relative old age situation has improved, there is no GC, indicates that large objects do not exist.

Through the analysis with optimization of the above, it is to meet the needs of the GC, you do not need to optimize.

5 summary

By using the above analysis and the basic use and how to use VisualVM VisualVM be optimized Java Virtual Machine I believe you already know, if you want to know more through knowledge of the Java Virtual Machine and optimization article, see the other articles in this series.

1, the original is not easy , old iron, the article needs your thumbs so that more people see, hope to help to you!

2, the article has inappropriate, please correct me, if you like micro-letters to read, you can also concerned about my micro-channel public number : 好好学javapublic number has been 6W fans, replies: 1024 , access to public numbers spree, the public number of long-term release Java high-quality series of articles , we will definitely give a lot of attention to your harvest!

Guess you like

Origin juejin.im/post/5e09a8c5e51d45584b586ed6