JVM tuning of Java architecture

  1. JVM Tuning Basics 1.1 Java heap structure

  Java heap may be on discontinuous physical memory space, as long as can be logically contiguous. Java heap allocation is a variety of objects and save memory space, shared between threads. Java heap into Eden region, Survivor region, and the tenured region Permanent region, as shown below.

  Java heap allocation principles are as follows:

Java heap profile as shown below, a new instance of the class most of Eden (Eden The reason why the word is represented by the initial start-up means) allocation region. Eden area is full, or when required GC, still live objects are copied to the Survivor areas, a total of two survivor district, when a district full of survivor still alive object will be copied to another survivor zone. When the survivor zone is full, is copied from the first zone over a survivor, and at this time live objects are copied to the Tenured Generation (old generation) Perm Generation for storing static files, the size of the permanent generation may be performed by MaxPermSize settings.

Perm Generation JVM resident memory is used to store JDK carry their own CLASS, metadata Interface and the like. This area is loaded into the data will not be recovered out of the garbage collector GC, closed JVM, this frees the memory area controlled.

Lifecycle of Java objects in the heap from Young to Tenured, the period from birth to Eden Tenured death. When EDEN zone is not enough, will trigger minor gc, GC will EDEN area for garbage collection, objects are no longer in use for destruction, while if the object was also found by another object, the object is moved to a survivor area, behind so By analogy, when Tenured zone occurs GC, called major gc, java because the life cycle of most objects is very short, so the GC generally occurs in the Eden area, the frequency of occurrence of minor gc much higher than major gc. If the last whole heap space is full, it will burst abnormal JVM space overflow: java.lang.OutOfMemoryError: java heap space.

1.2 JVM GC algorithm enumeration

Mark-sweep algorithm that is marked recovery algorithm, will need to be recovered object tag, and then uniform recycling. This algorithm is suitable object on behalf of Perm, Perm behalf of that storage space is relatively large, and not much need to be recovered.

Copying copy algorithm Algorithm i.e., direct copy. Eden target area for a copy of the survivor zone, because most of the objects Eden district will need to GC, and small-capacity Eden area.

Mark-compact algorithm sorting algorithm that is marked, remove the marks of different algorithms, tags to organize algorithm avoids memory fragmentation problems, it will all live objects move toward one end of memory, "finishing" is completed, one end is the survival of the object end is recovery of memory, and then directly recycled to clear the memory. 

1.3 GC collector

Serial Collector

   The most basic, the oldest collector. This is a single-threaded collector, the GC process, the application is stopped, i.e., stop-the-world.

Parnew collector

   Implementation code and serial about the same, except that this is a multi-threaded collector.

Paralled scavenge collector

   Paralled scavenger collector is a new generation of collector, using the copy algorithm, is a multi-threaded parallel collector. parallel scavenge collector focused on improving throughput, throughput is defined as: 

 Throughput = the user program execution time / (user program execution time + time garbage collection)

Serial old collector

   Serial old old's version of the Serial collector.

Parallel old collector

   Parallel Scavenge collector's version of the old. Use markers - sorting algorithms.

CMS (concurrent mark sweep) collector: concurrent mark sweep collector, the goal in the shortest time to pause collector. Using mark - sweep algorithm. 

  2.VisualVM1 actual status monitoring JVM

2.1 Common tuning parameter list

  Is a major need to know the total amount of storage space, the proportion of each area is provided, a different set of algorithms for the recovery and recycling of the different regions. By default, some options are not open, so the need to manually configure and select the appropriate parameters according to their own applications.       

   2.2 to view and analyze GC logs

    For applications, the eclipse can increase VM properties by engineering properties, GC will save the output as a log file.              

    GC output options:

-XX: + PrintGC GC log output -XX: + PrintGCDetails output detailed log of GC

-XX: + PrintGCTimeStamps GC output timestamp (in the form of a reference time)

-XX: + PrintGCDateStamps GC output timestamp (in the form of date, such as 2013-05-04T21: 53: 59.234 + 0800)

-XX: + GC before and during PrintHeapAtGC print out the information heap

-Xloggc: ../ output path logs / gc.log log file

Log output in the form of:

Calculated prepared a simple recursive procedure Fibbonaccy number sequence, and finally adds a System.gc () Full force a GC, GC logs obtained as follows, using an amount of 0 Eden area recovered. When the server type of application up and running, will inevitably be repeated GC, it can be relatively easy to locate performance problems by GC logs, such as full gc excessive number of times.

test environment:

  JVM: Java HotSpot(TM) Client VM (25.60-b23, mixed mode, sharing)   Java: version 1.8.0_60, vendor Oracle Corporation

Step1:

Monitoring interface can visually see the memory usage of each region JVM. In addition to monitoring the virtual machine, you can also monitor memory usage applications. Eclipse try to optimize startup, you can clearly see when the eclipse starts, S0 S1 object to copy, and the slightly increased amount of the old region.

You can also monitor the current CPU and memory load, the number of classes loaded and threads:

Step2:

There .ini files in the installation directory eclipse, the eclipse profile parameters namely, the need to go to fill out the configuration. After starting the eclipse, see GC log and found the whole boot process took place 27 GC Party, which has three times Full GC, start the whole process, I Qiazhiyisuan about 6 to 7 seconds, very slow

Parameter -XX: NewSize = n the Eden District reset after 128m, start eclipse was found again, this time it only happened 11 times GC, a general decline in more than start speed significantly faster than the original.

Step3:

Then using -Xverify: none ban bytecode verification procedure when loading a class, the number of the GC 10, now starting speed substantially more than three seconds or so, faster than the original doubled.

However, this too coarse tuning, program requires more precise positioning time on the server, it is necessary to debug more parameters.

  First wrote here.    

  

Reproduced in: https: //juejin.im/post/5d0725cc5188255fc638480e

Guess you like

Origin blog.csdn.net/weixin_34198583/article/details/93181931