The JVM tuning

Before writing a lot of KNOWLEDGE of the JVM, the implementation of the actual work, tuning is their integration of the concrete output, the front is the basis, tuning is the application, we will analyze the optimization together

A tuning scenario

1 High concurrent services

The amount of memory allocated by the Jvm is insufficient due to a surge in traffic on a system. In this case, you can solve the problem by deploying several nodes. But in so doing, also can produce some questions 1) each node concurrent access to all of the disk file may cause IO exception 2) unable to efficiently use the connection pool, memory problem solved, but the connection pool may cause waste spare 3) local cache wasteful, multiple nodes after local cache also became more, they are the same, This can be solved with distributed cachingCopy the code

Out-of-heap memory overflow error

A system that does not have frequent GC but throws OOM is found to be out of memory in the meta space, so it is important to pay attention not only to the memory in the heap but also to the memory in other partsCopy the code

3 The invoked service crashes, causing an exception

The interface provided by project A and project B, but project B crashed, resulting in the failure of project A to call, so that each call of project A will generate exceptions, and then the virtual machine process will crash due to space occupation, the solution is to deal with the queue mode of producer consumers.Copy the code

4 Exceptions caused by improper data structures

In the project, a large amount of data is written into the HashMap. Since the object is initially created in the Edgn area, and the object in the Edgn area should also be extinguished, but because the HashMap is too large, it will enter the S area, and the S area will increase the age of the object through the replication algorithm, which requires that the object in the Edgn area be not too large as possible. This solution involves modifying the program.Copy the code

Two through the VisualVm tool for tuning

VisualVm is a very famous JVM tuning tool, his characteristics is one is all, he can load many kinds of JVM tuning tools through plug-ins, such as JPS, JMAP, etc., the following is his application interface

This screen shows class loading, Cpu usage, heap memory usage, thread status.

When we go to the TAB page, we can see the generational collection of heap memory, including how many generation GC, old GC, compile time, class load time, by looking at the frequency of GC, we can adjust accordingly.

Besides these two, VisualVm also has some other functions, including thread monitoring, running snapshot generation of heap, etc., so VisualVm is a good tool to help with tuning.

Three conclusion

From the JVM memory structure, to the JVM garbage collection algorithm, to the garbage collector, basically laid the foundation of the JVM tuning needed knowledge, finally see tuning tool in nouns, found that it is easy to understand, code technology from Hello world, always forward, one step at a time can be slow, but it will be very solid, I am Yang Lele, and technology is a long and difficult road, looking forward to growing together with you.