1. JAVA memory area and memory overflow
1.1 overview,
The JVM in Java provides a memory management mechanism. During the execution of Java programs, the Java VIRTUAL machine will divide the internal data into different data areas, as shown in the following figure:
1.2. Program counter
A program counter is a line number indicator of the bytecode to be executed by the current thread. It is used to obtain the next bytecode instruction to be executed based on the value of the counter. When a Java method is executed, it records the address of the vm bytecode instruction being executed. If a Native method is executed, the value of this counter is null. No task OutOfMemoryError exists.
1.3. Virtual Machine Stack
Every ordinary Java method (except Native method) will create stack frames at the same time when it is executed, which is used to store information such as local variation table, operation stack, dynamic link, method exit, etc. The process of each method being called until completion corresponds to the stack frame loading and unloading in THE JVM stack. The memory space needed by the local variable table is allocated between compilers.
There are two types of exceptions associated with the virtual machine stack:
- StackOverflowError
The stack depth of thread requests is greater than the maximum depth allowed by the virtual machine.
- OutOfMemoryError
Failed to allocate sufficient memory for vm stack expansion.
1.4. Local method stack
Used for virtual machines to execute Native methods, otherwise the same as the Native method stack. There are also StackOverflowErrors and OutofMemoryErrors.
1.5, heap
After the VM starts, a heap is created to store object instances. The main working area of garbage collector during heap is mainly divided into new generation and old generation. The new generation can be subdivided into Eden space, From Survivor space and To Survivor space. The size of the heap can be controlled by -xmx and -xms when a Java program is started. OutOfMemoryError is thrown if there is no memory in the heap to complete the instance allocation and the heap cannot be extended.
1.6. Method area
The method area mainly stores metadata of classes, such as class information loaded by the virtual machine, constants, static variables, and code compiled by the just-in-time compiler. Before JDK1.8, it was implemented as a permanent generation. After JDK1.8, it used meta-space, which used system memory. OutOfMemoryError is thrown if memory cannot be allocated.
2. Recycling
2.1. How to determine the death of the object?
2.1.1 reference counting method
Adds a reference counter to the object with a value of 1 when there is a reference and -1 when the reference fails. Objects with a counter of zero are dead, but there is a problem: objects refer to each other in a circular way, so that their reference counter is not zero and the GC cannot be told to reclaim.
2.1.2 GC Roots search
In Java, GC ROOT search is adopted. The idea is to start from a series of objects named “GC Roots” and start to search downward from these nodes. The path taken in the search is called reference chain.
GC Roots include:
- The object referenced in the local variable table in a stack frame
- The object referenced by the class static property in the method area
- The object referenced by the constant in the method area
- Native method stack Objects referenced by Native methods
2.2 garbage collection algorithm
2.2.1. Mark-clear algorithm
The mark-clear algorithm is divided into two stages:
- Mark: First mark all objects that need to be reclaimed.
- Clear: Uniformly reclaim marked objects.
This algorithm has a couple of drawbacks:
- The efficiency is not high
- Discontinuous memory fragmentation is generated
2.2.2 Replication algorithm
The replication algorithm is very efficient. It divides the available memory into two pieces of equal size according to the capacity, and only uses one piece at a time. When one piece is used up, the surviving objects are copied to the other piece, and the used memory space is cleared.
This algorithm is very suitable for recycling the new generation, which is divided into Eden space, From Survivor space and To Survivor space. Generally, the allocated memory ratio is 8:1: 1. When recycling, the surviving objects in Eden and From Survivor are copied To To Survivor at a time, and then the Eden and From Servivor space is cleared. When the To Survivor space is insufficient, the old age is required.
2.2.3 Mark-collation algorithm
In the old days, objects had a higher survival rate, so the mark-de-clutter algorithm was proposed, first marking objects to be reclaimed, then moving all surviving objects to one end, and then directly cleaning up dead objects:
2.2.4 Generational collection algorithm
The idea of generational recycling is to divide different memory into several blocks according to the life cycle of the object, and adopt appropriate collection algorithms according to the characteristics of each memory, such as the replication algorithm in the new generation and the mark-collation algorithm in the old age.
2.3. Garbage collector
The following figure shows seven different generational collectors that can be used together if there is a line between them.
To view garbage collector information, run the following command:
java -XX: PrintCommandLineFlags -version
Copy the code
My test server results:
-XX:InitialHeapSize=524503488 -XX:MaxHeapSize=8392055808 -XX: PrintCommandLineFlags -XX: UseCompressedClassPointers -XX: UseCompressedOops -XX: UseParallelGC
java version "1.8.0 comes with _152"
Java(TM) SE Runtime Environment (build 1.8.0_152-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.152-b16, mixed mode)
Copy the code
As you can see, ParallelGC is used.
JVM parameter mapping:
The following is a brief introduction to the garbage collector in 7:
2.3.1 Serial collector
New generation single thread collector, simple and efficient.
2.3.2 ParNew collector
The multithreaded version of the Serial collector is identical to the Serial collector except that it uses multithreading for garbage collection.
2.3.3. Parallel Avenge
The Parallel Scavenge collector is a new generation of collector that uses a replication algorithm to focus on system throughput and is suitable for tasks that require less user interaction in the background.
2.3.4 Serial Old collector
Serial Old is a single-threaded old-time garbage collector that uses a mark-collation algorithm. It has the characteristics of simple and efficient.
2.3.5 Parallel Old collector
Parallel Old collector is an older version of Parallel Scanenge, multi-threaded garbage collection, also using mark-collation algorithm.
2.3.6 CMS collector
CMS focuses on the response time of the service and is based on the mark-sweep algorithm. With concurrent collection, low pause characteristics.
2.3.7 G1 collector
Garbage First, based on the mark-collation algorithm, divides the entire Java heap (including the new generation and the old generation) into multiple independent regions of fixed size, tracks the degree of Garbage accumulation in these regions, maintains a priority list in the background, and prioritises the regions with the most Garbage to be collected according to the allowed collection time each time.
3. Check whether the CPU usage is too high
3.1 viewing Process Information on Linux
top
Copy the code
3.2. View the threads that occupy the most CPU
ps -mp 23967 -o THREAD,tid,time
Copy the code
3.3. Change the thread ID to hexadecimal
printf "%x\n" 23968
Copy the code
3.4. View thread information
jstack 23967 |grep -A 10 5da0
Copy the code
jstack 23967 |grep 5da0 -A 30
Copy the code
3.5. View the process object information
jmap -histo:live 23967 | more
Copy the code
3.6. Check the GC status of the process
jstat -gcutil 23967 1000 100
Copy the code
reference
Take a look at JVM runtime heap memory using tools such as JMap and MAT