directory

1. Introduction

2. A VM group is created

2.1. Data isolation area

2.1.1. Program counter

2.1.2. JVM Virtual machine stack

2.1.3. Local method stack

2.2. Data sharing area

2.2.1 method area

2.2.2 Java heap

3. Garbage collector and algorithm ideas

3.1 Mark – removal algorithm

3.2 Replication Algorithm

3.3 Mark-collation algorithm

3.4 Generational recycling

4. Use ideas

4.1 the new generation

4.2 the old s

5. Java GC collector and configuration

Introduction to Concurrency

Throughput first garbage collector

Response-time first concurrent collector

Configuration summary



1. Introduction

A brief introduction to the JVM components and how it works, as well as the gc implementation

  • The JVM includes the program counter, virtual machine stack, local method stack, method area, and Java heap

  • The program counter, virtual machine stack, and local method stack are the thread data isolation areas

  • Method areas and Java heap data are shared

  • Thread running uses the services of the isolated region for support, while thread sharing region method areas and heap for specific threads

  • Program GC algorithm and ideas

  • 1. Mark removal method, is to mark and then view the mark clearance

  • Replication algorithm, one Eden, one survivor, copies backward to the second domain of the octant, and later to the old age

    • So the younger generation takes up 3/1, a little bit less, because it’s faster, and the older generation takes up 3/2

      • The younger generation of Eden takes up 10/8 and the two survivors each take up 10/1 so when you use the assignment algorithm, the only survivor on the far right is the one who doesn’t use it very often, which is the 10% that the younger generation keeps the 10% that they don’t use very often, and the other 90% is used, so, When one Eden and one survivor find the object still has references or marks, they copy it later, and the survivor runs out of space to copy it directly to the belt
  • 3. Mark sorting algorithm, mainly for the elderly substitution, mark the long-term survival to one end

  • 4. Generation recycling, dividing the young generation and the old generation, using different algorithms for different generations

 

 

2. A VM group is created

2.1. Data isolation area

2.1.1. Program counter

  • Small memory space, thread private. The bytecode interpreter works by changing the value of this counter to select the next bytecode instruction that needs to be executed. Branch, loop, jump, exception handling, thread recovery and other basic functions need to be completed by the counter

2.1.2. JVM Virtual machine stack

  • The thread is private and has the same life cycle as the thread. Describes the memory model of Java method execution: each method creates a Stack Frame for storage as it executesLocal variable scale,The operand stack,Dynamic link,Methods the exportAnd other information. Each method from the call to the end of execution, corresponding to the process of a stack frame from the virtual machine stack to the stack.
  • Local variable table: stores basic types known at compile time (Boolean, byte, CHAR, short, int, float, long, double), object references (reference types), and returnAddress types (which refer to the address of a bytecode instruction)
  • StackOverflowError: Stack depth of thread request is greater than the depth allowed by the virtual machine.
  • OutOfMemoryError: If the vm stack can be dynamically expanded and sufficient memory cannot be allocated during the expansion.

2.1.3. Local method stack

  • It differs from the Java virtual machine stack in that the Java virtual machine stack performs Java methods (that is, bytecode) services for the virtual machine, whereas the Native method stack serves Native methods used by the virtual machine. StackOverflowError and OutOfMemoryError exceptions also occur.

 

2.2. Data sharing area

2.2.1 method area

  • For most applications, this area is the largest chunk of memory managed by the JVM. Thread sharing, mainly to store object instances and arrays. Multiple Thread Local Allocation buffers (TLabs) are allocated internally. It can be physically discontinuous, but logically continuous.

2.2.2 Java heap

  • It is a shared memory area that stores data such as class information, constants, static variables, and code compiled by the just-in-time compiler that has been loaded by the VIRTUAL machine.

 

3. Garbage collector and algorithm ideas

3.1 Mark – removal algorithm

  • Mark clear directly can.

 

3.2 Replication Algorithm

  • Divide the space into two pieces and GC only one at a time. When a block of memory is used up, the surviving objects are copied onto another block.
  • Solve the shortcomings of the former method, but it will cause low space utilization. Because most new generation objects don’t survive the first GC. So there’s no need to divide the space 1 to 1. You can split a larger Eden space and two smaller Survivor Spaces, using the Eden space and one Survivor at a time. When recycling is done, the surviving objects in Eden and Survivor are copied to another Survivor at a time, and the Eden and Survivor space is cleaned up. The size ratio is generally 8:1:1, wasting 10% of Survivor space each time. But the question here is what if more than 10% survive? An allocation guarantee strategy is used here: the extra objects go straight into the old age.

 

3.3 Mark-collation algorithm

  • Different from the replication algorithm for the new generation, this algorithm is created according to the characteristics of the old age. Basically, the live object is moved to one end of memory.

 

3.4 Generational recycling

  • According to the living object divided into several memory areas, generally divided into new generation and old age. Then according to the characteristics of each age to develop the corresponding recovery algorithm.

 

4. Use ideas

4.1 the new generation

Since a large number of objects die and only a few survive each garbage collection, the replication algorithm is a reasonable choice.

 

4.2 the old s

In the old age, the object has a higher survival rate and no additional space allocation guarantees it. So you have to use either a mark-sweep or a mark-collation algorithm for recycling.

 

 

5. Java GC collector and configuration

The JVM GC has three main business needs: serial collector, concurrent collector, and parallel handmachine

Concurrent parallel can see my article: Java multi-thread yushen.blog.csdn.net/article/det…

Serial collector is only suitable for small data, we mainly introduce the introduction of concurrent and parallel collector JKD1.5 before the use of serial collector, if the use of other collectors need to start the configuration of the parameters added to the response, jdK1.5 after the system will automatically judge

Introduction to Concurrency

Parallel: Multiple garbage collectors execute in parallel, but the user thread remains in a wait state

Concurrency: when the user thread and garbage collector execute in the same way, not necessarily in parallel, and most likely cross

 

Throughput first garbage collector

Parallel collector mainly to achieve a certain throughput bit purpose, suitable for artificial intelligence, background processing, this kind of high-speed running class, using two types of collector:

1. Parallel collector (-xx :UseParallelGC) : Secondary collections are executed using multiple threads, and primary collections are executed using a single thread

2. The parallel generation collector (-xx :UseParallelOldGC) uses a single thread for both the primary and primary collections. The main collection is triggered when the old area is filled

Common configurations:

java -Xmx3800m -Xms3800m -Xmn2g -Xss128k -XX:UseParallelGC -XX:ParallelGCThreans=20

  • -Xmx Maximum heap size
  • -xMS initializes the heap size. This can be set to the same as -xmx to avoid the JVM reallocating memory after each garbage collector collection.
  • -Xmn young generation is set to 2g, which is usually 3/1 of the total heap. The official recommendation is 8/3 (young + old + persistent generation)
  • -xss set the heap size of each thread to 1m after jdk1.5, before each thread heap size is 256K, more threads need to adjust the corresponding size, reduce this value can appear more threads, but generally the system will set the maximum number of threads, can not be infinite generation, experience about 3000 to 5000.
  • -xx :UseParallelGC Parallel collector. This configuration only applies to the young generation. The young generation uses parallel collection, and the old generation uses serial collection.
  • -xx :ParallelGCThreaes=20 The number of threads for the parallel collector. This configuration should be consistent with the number of processors.

java -Xmx3500m -Xms3500m -Xmn2g -Xss128k -XX:UseParallelGC -XX:UseParallelGCThreaes=20 -XX:+UseParallelOldGC

  • -xx :UseParallelOldGC configuration old age parallel collector JDk6.0 supports old age parallel collection

java -Xmx3500 -Xms3500 -Xmn2g -Xss128k -XX:UseParallelGC -XX:MaxGCPauseMillis=100

  • -xx :MaxGCPauseMillis=100 Sets the young generation reclamation period, when the JVM automatically expands the young generation

-xx :UseAdaptiveSizePolicy =100 Sets the system to automatically select the proportion of survivors to achieve the minimum response and collection frequency specified by the system target

 

Response-time first concurrent collector

Concurrency mainly addresses response time and prevents pause time

CMS concurrent tag cleanup collector

The CMS (-xx :UseConcMarkSweepGC) collector was used in the old age to collect unreachable old objects in the main collection and execute concurrently with the application. In the old age, there is always enough space to ensure that there will not be young generation replication failures

java -Xmx3550 -Xms3550 -Xmn2g -Xss128k -XX:ParallelGCThreaes=20 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC

  1. -xx :+UseConcMarkSweepGC: Sets the old-age concurrent collector
  2. -xx :+UseParNewGC: Sets the younger generation of parallel collectors, which can be used in CMS collectors with JKD5.0 and above. JVMS are more system automatic configuration, no manual configuration is required.

java -Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX:+UseConcMarkSweepGC -XX:CMSFULLGCsBeforCompaction=5 -XX:+UseCMSCompactAtFullCollection

  • – XX: CMSFULLGCsBeforCompaction = 5: after running for a long time because of the concurrent collector generates fragments, lower the efficiency of, how many times this value is set to run space
  • – XX: + UseCMSCompactAtFullCollection: open to the compression of old generation. Performance may be affected, but fragmentation can be eliminated.

 

Configuration summary

  • -xx :+CMSIncrementalMode: set it to incremental mode. Suitable for a single CPU

  • -xx :ParallelGCThreads=n Sets the collector for the number of concurrent threads in the young generation

  • -xx :MaxGCPauseMillis=n: Sets the maximum pause time for parallel collection

  • -xx :GCTimeRatio=n: Sets the percentage of garbage collection time in the running time of the program. Formula for 1 / (1 + n)

  • -XX:+PrintGC

  • -XX:+PrintGCDetails

  • -XX:+PrintGCTimeStamps

  • -Xloggc:filename

  • -xx :+UseSerialGC: Sets the serial collector

  • -xx :+UseParallelGC: Sets the parallel collector

    -xx :+UseParalledlOldGC: sets the parallel generation collector

  • -xx :+UseConcMarkSweepGC: Sets the concurrent collector

  • -Xms: indicates the initial heap size

  • -Xmx: indicates the maximum heap size

  • -xx :NewSize=n: Sets the size of the young generation

  • -xx :NewRatio=n: Sets the ratio of the young generation to the old generation. For example, is 3, indicating that the ratio of the young generation to the old generation is 1:3, and the young generation accounts for 1/4 of the sum of the young generation and the old generation

  • -xx :MaxPermSize=n: Set the persistent generation size

 

 

 

 

 

 

 

ok

 

 

 

 

 

 

 

 

 

 

 

 

Continuously updated