当前位置:网站首页>JVM basic theory.

JVM basic theory.

2022-06-09 05:43:00 Die hard audio and video

Preface

What we often say JVM tuning , What exactly is it ? You should remember a noun stop-the-world, Whatever you choose GC Algorithm , Will happen stop-the-world,stop-the-world signify JVM Because to execute GC And it stops the execution of the application . When stop-the-world occurs , except GC Outside the required thread , All threads are waiting , until GC Task to complete . What we often say JVM Tuning mostly refers to reducing stop-the-world Time

 Insert picture description here

Not recommended System.gc()

stay JAVA in , Unable to display the allocation and deletion of memory , Most programmers will set related objects to null, Or use System.gc() Method to display delete memory . Set to null There's no harm , But the call System.gc() But it is worth considering .

• By default , adopt System.gc() perhaps Runtime.getRuntime().gc() Call to , Will explicitly trigger Full GC, At the same time, the old generation and the new generation are recycled , Trying to free memory occupied by discarded objects .•JVM The implementer can use system.gc() Call to determine JVM Of GC Behavior . And in general , Garbage collection should be automatic , No manual triggering required , Otherwise, it would be too much trouble .• However System.gc() The call comes with a disclaimer , Calls to the garbage collector cannot be guaranteed ( There is no guarantee that it will be triggered immediately GC).

The young and the old

gc() What are the guidelines for recycling ? That is, what kind of objects can be recycled ?

An object that is not pointed to by any reachable variable . Here "reachable" means something that can be found ( No reachable variable points to you , Do you have any reason to live ? Even if you live, who can find you ?)

HotSpot VM Consider that most objects have the following properties

• Most objects quickly become inaccessible .• Only a very small number of objects with long creation time have references

So in HotSpot VM in , It is physically divided into two - The younger generation (young generation) And the older generation (old generation).

The younger generation (Young generation): Most newly created objects are assigned here , Because most objects quickly become inaccessible , So many objects are created in the younger generation , And then disappear . The process by which an object disappears from this area , We call it minor GC.

Old age (Old generation): No objects that are inaccessible and have survived from the younger generation are copied here , It usually takes up more space than the younger generation . Because it takes up a lot of space , So it happened gc The frequency of is much lower than that of the younger generation . The process by which objects disappear from the old generation , We call it full GC.

 Insert picture description here

The permanent generation in the above figure ( permanent generation ) Also known as the method area (method area). Used to save class constants and string constants . therefore , This area is not used to permanently store objects that have survived the aging generation . It could happen in this area, too GC. And what happens in this area GC Events can also be counted as major GC

Card Table

What if the old objects need to reference the young objects ? There is one. ”card table“, He is a 512 byte The size of the block . All references from old objects to new objects will be recorded in this table . When implemented for the new generation GC When , Just query card table To determine whether it can be collected , Instead of looking at the entire older generation . This card table By a write barrier To manage .write barrier to GC It brings great performance improvement , Although there may be some overhead , but GC The overall time is significantly reduced . Insert picture description here

The composition of the younger generation

Where the object was first created . The young generation is divided into 3 Space .

• An Eden space (Eden )• Two survivor spaces (Survivor )

All in all 3 Space , Two of them are survivor spaces . The execution sequence of each space is as follows :

1. Most newly created objects are located in Eden In the space . 2. stay Eden Once in space GC after , Move the surviving objects to Survivor One of the spaces . 3. stay Eden Space for one GC after , Objects are stacked into Survivor In the space , Other surviving objects already exist . 4. Once the survivor space is full , Surviving objects will be moved to another survivor space . then , Full Survivor The space will become completely data free . 5. Objects that survive these repeated steps are moved to the old age .

As you can see by examining these steps ,Survivor One of the spaces must remain empty . If two Survivor There is data in the space , Or the usage of both spaces is 0, It will be regarded as a sign of system problems .

Data is passed through Minor GC The process of piling up to the old age is shown in the figure below : Insert picture description here

It should be noted that HotSpot Virtual machines use two techniques to speed up memory allocation . They are respectively ”bump-the-pointer“ and “TLABs(Thread-Local Allocation Buffers)”.

Bump-the-pointer Technology tracks the last object created in the garden of Eden space . This object will be placed at the top of the garden of Eden space . If you need to create objects later , Just check if there is enough space left in the garden of Eden . If there is enough space , Objects will be created in the garden of Eden space , And it's placed on the top . Since then , Every time you create a new object , Just check the last created object . This will greatly speed up memory allocation . however , If we are multithreaded , Things will be very different . If you want to store objects in Eden space in a thread safe manner in multiple threads , The inevitable need for locking , This will greatly affect the performance .TLABs yes HotSpot Virtual machine solutions to this problem . This scheme allocates an exclusive space for each thread in Eden space , So each thread only accesses their own TLAB Space , And again bump-the-pointer Technology can be used to allocate memory without locking .

summary : After the object has just been created , It is preserved in the garden of Eden . Those who survive for a long time will be transferred to the elder generation space through the survivor space .

Old age GC

Old age space GC The event basically occurs when the space is full , The execution process is due to GC Different types , therefore , Understand the different GC Types will be easier to understand .

•Serial GC•Parallel GC•Parallel Old GC (Parallel Compacting GC)•Concurrent Mark & Sweep GC (or “CMS”)•Garbage First (G1) GC

1.Serial GC (-XX:+UseSerialGC) In the younger generation GC Use the types we explained in the previous paragraph . Old age GC Used a kind called “ mark-sweep-compact ” The algorithm of .

• The first step of the algorithm is to mark the surviving objects in the old generation .( Standard machine )• then , Check the heap memory space from the beginning , And only the objects that still survive ( clear ).• The last step , Start from scratch , Fill the heap memory space sequentially , And the memory space is divided into two parts : A stored object , The other one is empty ( Compress ).

Serial GC Suitable for small memory 、CPU In the case of a small number of auditors .

2.Parallel GC (-XX:+UseParallelGC) Insert picture description here From the above , It can be seen that serial GC and parallel GC The difference between ,serial GC Use only one thread to execute GC, and parallel GC Use multiple threads , therefore parallel GC More efficient . such GC It is useful in the case of sufficient memory and multi-core , So we also call it ”throughput GC“.

3.Parallel Old GC(-XX:+UseParallelOldGC) Parallel Old GC stay JDK5 After that . And parallel GC comparison , The only difference is for the older generation GC Algorithm .Parallel Old GC There are three steps : Mark - Summary - Compress (mark – summary – compaction). Summary (summary) Steps and cleaning (sweep) The difference is that , It distributes the surviving objects to GC Different areas pre processed , The algorithm is a little bit more complicated than cleaning .

1.CMS GC (-XX:+UseConcMarkSweepGC) Insert picture description here

CMS GC It is much more complex than the various algorithms explained above . The first step is to initialize the tag (initial mark) Relatively simple . This step is just to find the surviving objects closest to the class loader . therefore , The pause was very brief . After the parallel tag ( concurrent mark ) step , All objects referenced by the surviving objects will be checked to see if they have been tracked and verified . The difference in this step is , In the process of marking , Other threads are still executing . Re marking (remark) step , The objects that are added or deleted in the parallel marking step and refer to the surviving objects will be checked again . Last , In parallel switching ( concurrent sweep ) step , Transfer to the garbage collection process for disposal . Garbage collection will be carried out during the execution of other threads . Once you take this GC type , from GC The resulting pause time will be extremely short .CMS GC Also known as low latency GC. It is often used in applications where response times are very demanding . Of course , such GC Type has stop-the-world The advantage of short time , It also has the following disadvantages :

• It will be better than others GC Type takes up more memory and CPU• Compression steps are not supported by default

Using this GC Type before you need to carefully consider . If the compression task has to be executed due to too many memory fragments , that stop-the-world More time than any other GC All types are long , You need to consider the frequency and execution time of compression tasks .

5 G1 GC

 Insert picture description here

If you want to understand G1, First of all, you should forget what you have learned about the new generation and the old generation . As you can see above , Each object is assigned to a different grid , And then GC perform . When an area is full , Object is assigned to another region , And implement GC. There are no longer three steps to move from the new generation to the old age ,G1 The biggest benefit is performance .

View the current jdk Default gc The collector

java -XX:+PrintCommandLineFlags -version
原网站

版权声明
本文为[Die hard audio and video]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203021427409210.html