当前位置:网站首页>7 kinds of JVM garbage collector, this time all understand

7 kinds of JVM garbage collector, this time all understand

2021-01-01 12:06:23 Program ape Owen

Preface

We explained before jvm Of Make up the structure And Garbage collection algorithm Wait for knowledge , Today we are going to talk about jvm The most important Heap memory How to use Garbage collector Garbage collection , And how to use command To configure and use these garbage collectors .

Detailed explanation of heap memory

You should have understood the picture above . You can understand it as a house It was divided into several groups room , The function of each room is different , Some are for babies , Some of them live with their parents , Some of them live with grandparents

  • Heap memory is divided into Two pieces of , One piece The younger generation , The other is Old age .

  • The young generation is divided into Eden and survivor. The default size ratio of the two spaces is 8:2,

  • The survival zone is further divided into s0 and s1. The two spaces are as like as two peas. , It's twins , They are 1:1 The proportion of

Heap memory garbage collection process

First step

New generation The object of the Eden District , When Eden District Full of Will trigger Minor GC.

The second step

First step GC Who survived , Will be moved to survivor In the district S0 District ,S0 When the zone is full, it will trigger Minor GC,S0 The surviving objects will be moved to the S1 District ,S0 Zone free .

S1 When it's full GC, Those who survived moved again to S0 District ,S1 Zone free , This is repeated over and over again GC, Every time GC once , The age of the object is One year up , After reaching a certain value (15), Will enter Old age .

The third step

Once in a while Minor GC after ( Prerequisite ), Old times may come Major GC, It depends on the garbage collector .

Full GC The trigger condition

  • Manual call System.gc, It will be implemented continuously Full GC

  • There is not enough space in the old age / Full of

  • Insufficient method space / Full of

Be careful

We need to remember a word :stop-the-world. It will be in any kind of GC In the algorithm .stop-the-world signify JVM Because of the need to execute GC and stop it Application execution .

When stop-the-world occurs , except GC Out of thread required , be-all Threads All in wait for state , until GC Task to complete .GC Optimization is often reduced stop-the-world Happen .

What areas of objects are recycled

It should be noted that ,JVM GC Just recycle Heap memory and In the method area The object of . and Stack memory The data of , After being out of scope, it will be JVM Automatically release , So it's not in JVM GC Within the management scope of .

Common parameter configuration of heap memory

Parameters describe
-Xms Initial size of heap memory , Company m、g
-Xmx Maximum allowable heap memory size , Generally not larger than physical memory 80%
-XX:PermSize Initial size of non heap memory , Initialization of general application settings 200m, Maximum 1024m That's enough
-XX:MaxPermSize Maximum allowable size of non heap memory
-XX:NewSize(-Xns) The initial memory size of the younger generation
-XX:MaxNewSize(-Xmn) The maximum allowed memory size of the younger generation
-XX:SurvivorRatio=8 Young generation Eden District and Survivor Capacity ratio value of the zone , The default is 8, namely 8:1
-Xss Stack memory size
-XX:NewRatio= Old age / The new generation Set the size ratio between the old age and the new generation
-XX:+PrintGC jvm After starting , Just meet GC The log will be printed
-XX:+PrintGCDetails see GC Details , Including the situation of each district
-XX:MaxDirectMemorySize stay NIO Can be accessed directly in Direct memory , This is to set its size , Do not set the default is the maximum heap space value -Xmx
-XX:+DisableExplicitGC close System.gc()
-XX:MaxTenuringThreshold Garbage can enter the age of old age
-Xnoclassgc No garbage collection
-XX:TLABWasteTargetPercent TLAB Occupy eden % of area , The default is 1%
-XX:+CollectGen0First FullGC Whether it's first or not YGC, Default false

TLAB Memory

TLAB The full name is Thread Local Allocation Buffer namely Threads allocate the cache locally , It's a thread specific memory allocation area by name , It's designed to speed up object allocation .

Each thread generates one TLAB, A work area that is exclusive to this thread ,java Virtual machines use this method TLAB Area to avoid multithreading conflicts , The efficiency of object allocation is improved .

TLAB Space is usually not too large , When large objects cannot TLAB Distribution , Will be allocated directly to the heap .

Parameters describe
-Xx:+UseTLAB Use TLAB
-XX:+TLABSize Set up TLAB size
-XX:TLABRefillWasteFraction Set up maintenance access TLAB The single object size of the space , It's a proportional value , The default is 64, That is, if the object is larger than the 1/64, Is created in the heap
-XX:+PrintTLAB see TLAB Information
-Xx:ResizeTLAB Self adjustment TLABRefillWasteFraction threshold .

Garbage collector Overview

版权声明
本文为[Program ape Owen]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/01/20210101120300364z.html

随机推荐