JVM memory areas and garbage collection


1, JAVA memory areas and memory overflow

1.1 Overview

Java, JVM provides memory management, Java virtual machine in the Java program execution process will divide into different data areas as:

JVM memory area .png

1.2, the program counter

The program counter is the current thread bytecode executed by the line number indicator, bytecode instructions according to the following function is to obtain the value of the counter to be executed. When the method is performed java, the address of the virtual machine is recorded bytecode instructions being executed, if Native method, this counter is empty. OutOfMemoryError task does not exist.

1.3, the virtual machine stack

Each ordinary Java method (Method Native removed) at the same time create a stack frame is going to be executed, a local variable table for storing information, stack operation, dynamic link, export method, each method is invoked corresponds to the process until the stack JVM stack frame stack and the stack. Wherein the local variable table to complete the required memory space partitioned between the compiler.

Now there are two virtual machine stack exception associated with it:

  • StackOverflowError

Request thread stack depth is greater than the maximum depth allowed by the virtual machine.

  • OutOfMemoryError

Virtual machines can not apply to the stack when sufficient memory expansion.

1.4, native method stacks

Native methods for virtual machines, the same stack and other local methods. There will be StackOverflowError and OutOfMemoryError.

1.5, heap

Creating the heap after the virtual machine starts, used to store an object instance. The main work area when the heap of garbage collector, is divided into the old and the new generation's, the new generation of space can be subdivided into Eden, From Survivor space, To Survivor space. When the java program starts, and is available -Xms control -Xmx heap size. OutOfMemoryError will be thrown if no heap allocation and heap memory to complete the instance can not be extended.

1.6, the method area

Metadata region primary storage class methods, class information such as the virtual machine is loaded, a constant, static variables, the real-time compiler code to achieve permanent behalf before JDK1.8, using the element space after JDK1.8, and metaSpace It is using system memory. If you can not apply for memory, it will throw OutOfMemoryError.

2, garbage collection

2.1, how to determine the object have died?

2.1.1 Reference counting

Add a reference in the object counter, when a place reference, the counter value 1, when referring to the failure, the counter value -1. Counter to the object 0 is death, but there is a problem: circular reference objects, two objects refer to each other each other, causing them to reference counter is not zero, so unable to notify GC recovery.

2.1.2, GC Roots search

Java is used in GC ROOT search, the idea is through a series of objects called "GC Roots" as a starting point, to start the search down from these nodes, the search path traversed called chain of references, when GC Roots of a when a target is unreachable, then it proves that this object is not available.

GC Roots include the following:

  • The local variable stack frame referenced object table
  • Method static property class object referenced by region
  • The method of constant reference object region
  • Native method stacks Native method referenced objects

2.2, garbage collection algorithm

2.2.1, mark - sweep algorithm

Mark - sweep algorithm is divided into two stages:

  • Tags: First all need to be recycled objects.
  • Clear: a unified object is marked recovery.

Mark - Clear .png

This algorithm has even a drawback:

  1. low efficiency
  2. It will produce discrete memory fragmentation
2.2.2, replication algorithm

High replication efficiency of the algorithm, which will be equal to the size of the available memory capacity is divided in accordance with two, uses only one of them, when a run, it will also copy live objects to one another above and then removed used memory space.

Replication algorithm .png

This algorithm is suitable for recycling the new generation, the new generation into the Eden space, space From Survivor, To Survivor space, memory allocation ratio generally is 8: 1: 1, when recovered, and Eden will also survive From Survivor the object is copied to the to Survivor in a one-time, clean up after and Eden From Servivor space, when to Survivor space is not enough to rely on the old era.

2.2.3, Mark - Collation Algorithm

In the old era, the survival rate of the object is relatively high, so the flag - sort algorithms have been proposed to, first mark the object to be recovered, and then all the surviving objects are moved to the end, then clean out the direct target of death:

Mark - finishing .jpg

2.2.4, generational collection algorithm

Generational recovery is based on the idea of ​​the survival period of the object, the memory is divided into different pieces, using appropriate collection algorithm based on the characteristics of each memory block, such as the use of the new generation of replication algorithm, using old's mark - Collation Algorithm.

2.3, the garbage collector

The following figure shows seven different generational collectors, if there is the connection between the two collectors, with the representation may be used.

Garbage collector .jpg

You can view information about the garbage collector with the following command:

java -XX: PrintCommandLineFlags -version
复制代码

My test server results:

-XX:InitialHeapSize=524503488 -XX:MaxHeapSize=8392055808 -XX: PrintCommandLineFlags -XX: UseCompressedClassPointers -XX: UseCompressedOops -XX: UseParallelGC 
java version "1.8.0_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)
复制代码

It can be seen using: ParallelGC.

JVM parameters corresponding relationship:

JVM parameters corresponding relationship .png

The following outlines the garbage collector 7:

2.3.1, Serial collector

The new generation of single-threaded collector, simple and efficient.

2.3.2, ParNew collector

Multi-threaded version of the Serial collectors, in addition to the use of multiple threads during garbage collection, and the rest are the same as Serial collectors.

2.3.3, Parallel Scavenge collector

Parallel Scavenge collector in parallel using the copy algorithm of the new generation of collectors, focusing on the throughput of the system for background operation tasks without too much user interaction.

2.3.4, Serial Old collectors

Serial Old is single-threaded years old garbage collector, using a mark - sort algorithm. Simple and efficient.

2.3.5, Parallel Old collectors

Parallel Old collectors are Parallel Scanenge old's version, multi-threaded garbage collection, but also the mark - Collation Algorithm.

2.3.6, CMS collector

CMS service-oriented response time, is based on the mark - sweep algorithm. With a concurrent collection, low pause features.

2.3.7, G1 collector

Garbage First, based on the label - sorting algorithm, which will be the entire Java stack (including the old and the new generation's) is divided into separate areas multiple fixed size, and tracking these areas the extent of garbage piled up in the background to maintain a priority list, every according to sub-collection time allowed, the largest regional priorities garbage collection.

3, CPU occupancy high troubleshooter

3.1, linux View Process

top
复制代码

linux process view information .png

3.2, see the process takes most cpu thread

ps -mp 23967 -o THREAD,tid,time

复制代码

View the process takes up cpu threading .png

3.3, turn hex thread ID

printf "%x\n" 23968

复制代码

Turn hex thread ID .png

3.4, see the thread information

jstack  23967  |grep -A  10  5da0

复制代码

View thread information .png

jstack 23967  |grep 5da0 -A 30

复制代码

View thread information -2.png

3.5 object information, view the process of

jmap -histo:live 23967 | more

复制代码

View the process of object information .png

3.6, GC situation View the process

jstat -gcutil 23967 1000 100

复制代码

GC situation View the process .png

reference

Jmap heap memory use and MAT and other tools to view JVM runtime

tencent.jpg

Guess you like

Origin juejin.im/post/5e020dc5f265da33d56d2df2