preface

Recalled as a rookie, saw many posts, the inside of the Java great god have to say: the JVM tuning is JavaIT advanced essential knowledge, so he bought books and tutorials to see, but found that this topic for tuning reading efficiency is low, read and reread some content repeatedly, what in retrospect is not left in my head.

In order to avoid similar situations, we have specially edited a JVM related article for your reference and study, hoping to help you want to progress.

One: VM memory diagram


JAVA programs run on virtual machines and need memory to run. When a VM executes JAVA programs, the memory it manages is divided into different data areas for easy management.


The vm management memory data area is divided as follows:



Data area classification:


Method Area:

VM Stack :(VM Stack)

(Native Method Stack)

Heap (Heap)

Program Counter :(Program Counter Register)

Direct Memory :(Direct Memory)


Description:


1. Program counter


Line number indicator, bytecode instruction branch, loop, jump, exception handling, thread recovery (CPU switch), each thread needs an independent counter, thread private memory is not affected, this area will not overflow exceptions.


2. Vm stack


The virtual machine stack is the memory model of Java method execution. Each method is executed and a stack frame is created, which is the basic data structure during the method execution. Stack frames are used to store: Local variable table, operand stack, dynamic link, method exit, etc., each method execution corresponds to the process of virtual machine stack frame from the stack to the stack.


Is a data structure that is a local variable table in the virtual machine, corresponding to the program data model above the physical layer.


A local variable table is a program running data model that stores various data types known at compile time. For example:

Boolean, byte, char, short, int, float, long, double, object reference type (object memory address variable, pointer, or handle); StackOverFlowError The thread request stack depth is greater than the allowed depth of the VM. OutOfMemory The memory space is exhausted and cannot be extended.


3. Local method stack


Similar to the vm stack, the VM stack serves Java programs, and the local method stack supports the RUNNING of the VM. The implementation is determined by the VM vendor. StackOverFlowError and OutOfMemory exceptions are also raised.


4. The heap


Is the largest part of virtual machine managed memory, shared by all threads, used to store object instances (objects, arrays), physically discontinuous memory space, due to GC collector, generational collection, so divided into: New generation Eden, From SurVivor space, To SurVivor space, allot buffer(allocated space), may divide multiple threads private buffer, old age.


5. Methods area


A thread-shared area of memory like the heap, used to store data such as virtual machine-loaded class information, constants, static variables, just-in-time compiler compiled code (dynamically loaded OSGI), and so on. It is technically part of the Java virtual machine and is called non-heap to distinguish it.


You can choose not to do garbage collection in this area. The purpose of garbage collection in this area is mainly to recycle the constant pool and unload the class of the type. OutOfMemory is thrown when the memory area is insufficient


Runtime constant pool:


A section of the method area where the Class version, fields, interfaces, methods, etc., and various literals and symbolic references generated at compile time are stored after the compiled Class loads. An OutOfMemory exception is thrown.


6. Direct memory


Direct memory does not belong to the virtual memory area. It is an IO method based on channel and buffer. Local functions can be used to directly allocate out-of-heap memory, store referenced external memory addresses in the heap, and complete operations on directly referenced memory by reference. It avoids the round-trip replication between the heap memory and Native memory and is not controlled by the VM memory, which may throw OUtOfMemory exceptions.


Two: object access internal implementation process


Object access involves changing the address state of an object, moving the memory address, variables, interfaces, implementation classes, methods, parent types, and so on.


1. Handle mode (access)



2. Pointer mode (access)



The advantages and disadvantages:


Handle access: Reference stores a stable address. When the object changes, only the handle instance data pointer will be changed, and the reference itself does not need to be modified


Pointer access mode: Advantages fast speed, save pointer location time overhead


Three: Memory area control parameters and corresponding overflow exceptions


Every time we get an OutOfMemory exception or GC exception or StackOverflowError during development, we’re going to have a bunch of arguments that are going to go up, and we’re going to have a general idea of how we’re going to do it, and what parameters we’re going to adjust for that exception. Or to put it another way, most of the time you don’t know what parameters are in the JVM’s memory allocation areas. You just jump in the parameters and expect them to work, and you don’t know if they’ll work. Let’s take a look at the parameters of memory areas such as the JVM heap, stack, and method area, as well as the types of exceptions that can be thrown by each area.


1. Parameter types


  1. Heap space parameter

  2. Stack space parameter

  3. Method area spatial parameters

  4. Native direct memory parameters

2. Exception types


1. OutOfMemory exceptions

2. StackOverflowError anomalies


3. Auxiliary parameter description


  1. – XX: + HeapDumpOnOutOfMemoryError print to print out a snapshot when heap memory abnormal information

  2. -xx :+HeapDumpPath Snapshot output path

  3. -Xmn Specifies the Eden area size

  4. -xx: adjusts the size of the surviving area by SurvirorRation

  5. – XX: PretenureSizeThreshold set into the old s threshold


4. Parameter description and exceptions in corresponding scenarios


1. Heap memory parameters


-xMS: heap minimum (sum of Cenozoic and old age)

-Xmx: maximum heap size (sum of new generation and old generation)


When minimum = maximum, then heap memory is not expandable.


Example: – Xms80M – Xmx80M


Generally, -xmx and -xms are set to the same size to reduce the number of GC, and OutOfMemoryError is thrown when the heap is out of memory.


2. Stack memory parameters


-Xss


Example: -xss128k


A StackOverflowError is raised in a single thread, whether the stack frame is too large or too small, or if the reference depth exceeds the virtual machine’s allowable depth. Each method pushes an inconsistent frame size onto the stack. An OutOfMemoryError is raised when each thread allocated a stack frame that is too large to be scalable.


3. Method area parameters


-xx :PermSize Specifies the minimum memory size of the method area

-xx :MaxPermSize Specifies the maximum memory size of the method area


An area of memory shared by threads that is used to store metadata, constants, static variables, code compiled by the just-in-time compiler, and so on


Example: – XX: XX: PermSize = 20 m – MaxPermSize = 20 m


Exception type OutOfMemoryError:


Cause: Too many constants, or proxy reflection used too often


4. Native direct memory parameters


-XX:MaxDirectMemorySize


Example: – XX: MaxDirectMemorySize = 10 m


If not, an OutOfMemory exception is raised


Four: garbage collection algorithm


Classic garbage collection algorithms are as follows


1. Mark-sweep


Status before recycling:



Status after recycling:



The advantages and disadvantages:


Algorithm execution is divided into two stages mark and clear, all the recycling algorithm, basically all

Deep optimization is made based on tag recovery algorithm


Disadvantages: Efficiency issues, memory space fragmentation (discontinuous space)


2. Copying Algorithms


Status before recycling:


Eden Memory space 8



Survivor1 Space (From space) 1



Survivor2 Space (To space) 1



Eden memory space and Survivor space 8:1


Status after recycling:



Survivor1 Space (From space) 1



Eden memory space and Survivor space 8:1



The advantages and disadvantages:


The comparison tag clearing algorithm avoids the memory fragmentation problem caused by the reclamation


Disadvantages: at the expense of local memory space, but the waste of space is relatively small, default 8:1 ratio 1 is wasted.


Copying also has some efficiency and space cost


3. Mark-compact algorithm


Status before recycling:



Status after recycling:



The advantages and disadvantages:


It avoids the problem of wasted space and memory fragmentation.


Disadvantages: Copy the efficient cost of finishing.


Five: garbage collector


1. Seven garbage collectors


Serial (GC) -xx :+UseSerialGC

(2) ParNew (parallel GC) -xx :+UseParNewGC

Avenge the insane.

(4) Serial Old (MSC) -xx :+UseSerialGC

(5) CMS (Concurrent GC) -xx :+UseConcMarkSweepGC

ParallelOld (Parallel GC) -xx :+UseParallelOldGC

(7) G1 (JDK1.7update14 can be commercially available)



2. 1~3 For garbage recycling of young generation:


The garbage collection of the young generation is called the Minor GC


3, 4~6 for garbage recycling of the old generation (of course, it can also be used for recycling in the method area) :


Garbage collection for the older generation is called full GC


G1 completed “Generational garbage Collection” independently


Note: parallelism vs. concurrency


Parallel: Multiple garbage collection threads operate simultaneously


Concurrency: The garbage collector thread operates with the user thread


4. Five combinations are commonly used


Serial/Serial Old


ParNew/Serial Old: Compared to above, just more multithreaded garbage collection than the younger generation

ParNew/CMS: a more efficient combination at the moment

The Parallel Avenge /Parallel Old

G1: state-of-the-art collector, but requires JDK1.7update14 or higher


5, Serial/Serial Old


The Serial collector uses a single GC thread to implement the “copy” algorithm (including scanning and copying).

The Serial Old collector uses a single GC thread to implement the “mark-tidy” algorithm

Both Serial and Serial Old suspend all user threads (that is, STW)


Description:


STW (Stop the World) : SafePoint is injected into each method when the code is compiled. When the application is paused, it waits for all user threads to enter SafePoint, then pauses all threads and garbage collection.


Applicable situations:


Machines with less than 2 CPU cores and less than 2G physical memory (simply speaking, single CPU, small space for the new generation and low requirements for STW time)


-xx :UseSerialGC: forcibly use the GC combination

– XX: PrintGCApplicationStoppedTime: check the STW time


6, ParNew/Serial Old:


ParNew is the same as Serial except that it uses multiple GC threads to implement the replication algorithm, but Serial Old in this combination is a single GC thread, so this combination is awkward. Not as fast as Serial/Serial Old on a single CPU (because ParNew threads need to be switched) and not as fast as the following three combinations on multiple cpus (because Serial Old is a single GC thread), so it’s not really used much.


-xx :ParallelGCThreads: Specifies the number of ParNew GC threads. The default value is the same as the number of CPU cores. This parameter may also be used in CMS GC combinations


8. be insane/insane:


Features:


The Avenge avenge avenge avenge avenge avenge avenge avenge avenge avenge avenge avenge avenge avenge avenge avenge avenge Old suspends all user threads (STW)


Description:


Throughput: Insane/INSANE/INSANE CMS is based on the STW insane/INSANE/INSANE. That the higher the CPU utilization, so it is mainly used for processing a lot of CPU computing tasks, and less user interaction tasks)


Parameter Settings:


-xx :+UseParallelOldGC: Use the GC combination

-xx :GCTimeRatio: Sets the maximum allowed GC time at 1/(1+19). The default value is 99, that is, 1/(1+99).

-xx :MaxGCPauseMillis: indicates the maximum GC pause time. The smaller the parameter is, the better

– XX: + UseAdaptiveSizePolicy: Open the parameter, Xmn / – XX: SurvivorRatio / – XX: PretenureSizeThreshold these parameters doesn’t work, virtual opportunity to automatically collect monitoring information, dynamic adjustment of these parameters in order to provide the most appropriate pause time or the maximum throughput adaptive adjustment strategy (GC), -xmx, -xx :+UseParallelOldGC, -xx :GCTimeRatio, -xms: -xmx, -xx :+UseParallelOldGC, -xx :GCTimeRatio


Note:


-xx :GCTimeRatio and -xx :MaxGCPauseMillis

Don’t open – XX: + UseAdaptiveSizePolicy, Xmn / – XX: SurvivorRatio / – XX: PretenureSizeThreshold these parameters can be configured, still in resin server, for example


<jvm-arg>-Xms2048m</jvm-arg> <jvm-arg>-Xmx2048m</jvm-arg> <jvm-arg>-Xmn512m</jvm-arg> <jvm-arg>-Xss1m</jvm-arg> <jvm-arg>-XX:PermSize=256M</jvm-arg> <jvm-arg>-XX:MaxPermSize=256M</jvm-arg> <jvm-arg>-XX:SurvivorRatio=8</jvm-arg> <jvm-arg>-XX:MaxTenuringThreshold=15</jvm-arg> <jvm-arg>-XX:+UseParallelOldGC</jvm-arg> <jvm-arg>-XX:GCTimeRatio=19</jvm-arg> <jvm-arg>-XX:+PrintGCDetails</jvm-arg> <jvm-arg>-XX:+PrintGCTimeStamps</jvm-arg> View Code


Applicable situations:


You don’t want to pay too much attention to GC parameters and you want to let the virtual machine do the tuning itself


8. Tuning methods


8.1 New objects reserved for the new generation


Since the cost of fullGC(old generation) is much higher than that of minorGC (New generation and old generation), it is necessary to allocate a reasonable Cenozoic space for applications and try to allocate objects to Cenozoic to reduce the frequency of fullGC


8.2 Large objects enter the old age


Distribution of the large object directly to the old s, maintain the integrity of the structure of the new generation object, to improve the efficiency of GC, in order to pass the – XX: PretenureSizeThreshold set into the old s threshold


8.3 Stable and oscillating heap size


A stable pair size is good for garbage collection, and the method matches the size of -xms and -xmx


8.4 Throughput Priority


As much as possible to reduce the system to perform garbage collection total time, so use a parallel garbage collector


-xx :+UseParallelGC or use -xx :+UseParallelOldGC


8.5 Lowering the Pause


Use the CMS collector and reduce the number of FULLGCs


9. Methods to obtain GC information


9.1 -verbose:gc or -xx :+PrintGC To obtain GC information

9.2 -xx :+PrintGCDetails For more detailed GC information

9.3-xx :+PrintGCTimeStamps get the frequency and interval of GC

9.4-xx :+PrintHeapAtGC retrieves heap usage

9.5 -xloggc :D:\gc.log Specifies the path for saving log information


10. JVM tuning practice – Tomcat startup acceleration


Add the configuration at the beginning of the tomcat bin/catalina.bat file


Six: monitoring tools


Monitoring tools: used for general problem location and performance tuning.


1, the JPS


Jps is named after THE NAMING rules of Unix systems. Jps is similar to PS in that it can list all running VM processes and display the main classes executed by VMS as well as the unique ids of these processes (LVMID is the same as PID on this vm).


Jps [option] [hostid]

JPS -q outputs only LVMID

JPS -m outputs the methods passed to the main class when the JVM starts

JPS -l prints the full name of the main class, or the path to the Jar if it is Jar

JPS -v prints startup parameters for the JVM


2, jstat


Jstat is used to monitor virtual machine health information such as class loading, memory, garbage collection, JIT compiler, etc. It is the preferred monitoring tool for servers without guIs. Its usage is as follows:


jstat [option vmid [interval [s|ms] [vount] ] ]


Jstat monitors the amount of time between content threads to refresh


Jstat — GC 20445 1 20: Monitors the Java heap, including Eden, two survivor zones, old zones, and permanent zone capacity, used space, and TOTAL GC time


Jstat — gcutil 20445 1 20: Monitors the same as -GC, but the output focuses on the percentage of the total space used


Jstat — class 20445 1 20: Monitor the number of classes loaded, unloaded, total load space and elapsed time of classes, etc


. -gccapcity…… : Monitors the same as -GC, but the output focuses on the maximum and minimum space used by the Java region

. -gccause…….. : Same as the -gcutil output, the extra output is what caused the last GC

. -gcnew………. : Monitor the GC status of the new generation

. -gcnewcapacity.. : Same as -gcnew monitoring information, the output focuses on the maximum and minimum space used

. -gcold………. : Monitors old generation GC

. -gcoldcapacity.. : Same as -gcold monitoring information, the output focuses on the maximum and minimum space used

. -gcpermcapacity.: indicates the maximum and minimum space used by the output permanent tape

. -compiler……. : Outputs information about the methods and time that the JIT compiler has compiled

. -printcompilation: Outputs methods that have been JIT compiled


3, jinfo


JPS -v allows you to view the vm parameters that are explicitly specified when the VM is started, but what if you want to know the default parameters? In addition to querying the corresponding data, jInfo becomes important. Jinfo can be used as follows:


Jinfo [option] pid


4, jmap


Map is used to generate heapdump snapshots. Of course we have a lot of ways to get to the corresponding dump information, as we pass the JVM starts to join launch parameters – XX: HeapDumpOnOutOfMemoryError parameters, can let the JVM memory leak fault generated automatically in the event of a dump file, -xx :HeapDumpOnCtrlBreak (CTRL + Break) -xx :HeapDumpOnCtrlBreak (CTRL + Break) Jmap is not only used to get dump files, but also can be used to query finalize execution queue, Java heap and persistent band details, such as space utilization, garbage collector, etc. Its run format is as follows:


Jmap [option] vmip


Monitoring stack information is used to locate faults and generate stack snapshots


. -dump…… -dump:[live,]format=b,file={fileName}

. -finalizerinfo…… : Displays the objects for which the Finalizer method is waiting in the f-queue (only for Linux)

. -heap…… : Displays heap details, garbage collector information, parameter configuration, and generation details

. -histo…… : Displays statistics about objects in the stack, including classes, number of instances, and total capacity

. -permstat…… : Displays the memory status of the permanent band in ClassLoder format

. -F…… : If the VM does not respond to -dump, you can use this option to forcibly generate a dump snapshot


Example: jmap-dump :format=b,file=yhj.dump 20445


5, jstack


The Jstack is used to take a snapshot of the JVM’s threads at the current moment, also known as a Threaddump file, which is a collection of the stack information currently being executed by each thread of the JVM. The main purpose of creating a thread snapshot is to locate the cause of a long pause in a thread, such as thread deadlock, dead loop, request external time is too long. Jstack tells us which processes are doing what in the background? What resources are waiting for! Its run format is as follows:


Jstack [option] vmid


-f Forces the thread stack to be output when a normally output request does not respond

-l Displays additional information about locks in addition to stack information

-m Displays the stack information of the native method


6, jconsole


In the BIN directory of the JDK, monitor memory,thread, stack, etc


7, jprofile


Similar to JConsole, it monitors information more comprehensively than JConsole, memory, threads, packages, CUP classes, stacks, and so on