Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to trigger a YoungGC or FullGC operation

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to trigger YoungGC or FullGC operation", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to trigger YoungGC or FullGC operation.

(1) on garbage collection

Garbage collection in JAVA needs to accomplish three things:

1. Which memory needs to be reclaimed

2. When will it be recycled

3. How to recycle

Let's start from these three questions to understand the garbage collection mechanism of Java.

(B) which rubbish needs to be recycled

Before garbage collection, the first problem is to determine which garbage needs to be collected. Now Java uses the root search algorithm (GC Roots Tracing) to determine whether an object is alive or not. The idea of this algorithm is to search down from these nodes through a series of objects called "GC Roots". When GC Roots cannot reach this object (or an object is not referenced by any other object) It proves that the object is unavailable, and these objects are determined to be objects that need to be recycled.

As shown in the figure, ObjC is unreachable, and this object is the object that needs to be recycled.

Objects that can be used as GC Roots in the Java language include the following:

1. Objects referenced in the virtual machine stack (the local variable table in the stack frame)

2. Objects referenced by class static properties in the method area

3. Objects referenced by constants in the method area

4. Objects referenced by the local method stack (Native method)

(3) when will it be recycled

On the question of how to recycle, I refer to "in-depth understanding of the Java virtual machine". The unreachable objects in the root search algorithm are not immediately recycled, but are marked once:

If the object does not override the finalize () method, or if the finalize () method has been called, the virtual machine determines that the object does not need to perform finalize (), and the object will not be reclaimed in this tag.

If the object is marked as necessary to execute the finalize () method, it is placed in a queue called F-Queue, which is later garbage collected by the virtual machine.

But this object has a last chance to escape. When in F-Queue, the virtual machine marks the object in F-Queue on a small scale. If it is found that an object is reachable at this time, it will escape the fate of GC.

(4) how to recycle

In the final analysis, the problem of how to collect garbage is the problem of how to collect garbage by garbage collection algorithm. This paper mainly introduces the implementation ideas of three garbage collection algorithms:

4.1Mark-clear algorithm (Mark-Sweep)

This algorithm is divided into two stages: marking and clearing. First, all the objects that need to be recycled are marked, and the marked objects are recycled uniformly after the marking is completed.

As you can see from the picture, the problem with this algorithm is that a large number of discontinuous space debris will be produced after removal.

4.2 replication algorithm (Copying)

The replication algorithm divides the memory into two chunks, one of which is used each time. During garbage collection, the remaining memory in the memory being used is put into the other memory, and then the original memory block is emptied, as shown below:

Replication algorithm is widely used in the new generation of garbage collection, because about 98% of the objects of the new generation will be reclaimed, so the new generation of memory will be divided into one Eden space and two Survivor spaces, the proportion is 8:1:1.

The first time YGC only reclaims the eden area, after recycling, most (about 98%) of the objects will be recycled, and the living objects will enter the Survivor0 through the replication algorithm (replaced by S0 and S1 later). After YGC again, the living object in eden+S0 enters S1. Once again, the living object in YGC,eden+S1 enters S0. Cycle in turn

4.3Mark-organize (Mark-Compact)

The tag-finishing algorithm is divided into three steps: marking, sorting, and clearing. The first step is to mark the recyclable objects, then move the surviving objects to one side, and then directly clean up the garbage outside the boundary.

Tag collation algorithm is widely used in garbage collection in the old days.

(v) when YoungGC or FullGC will be triggered

YoungGC triggers often occur, and YoungGC will be triggered when the Eden area of the new generation is full.

FullGC can be triggered in several situations:

1. Check before the occurrence of Young GC. If "continuous memory space available in the old age" < "average size of objects in the old age after the previous Young GC of the new generation", it indicates that the object size that may rise to the old age after this Young GC may exceed the current available memory space in the old age, and FullGC will be triggered at this time.

2. When there is not enough space to store objects in the old days, a FullGC will be triggered.

3. If the memory in the metaspace area reaches the set threshold-XX:MetaspaceSize=, will also trigger FullGC.

At this point, I believe you have a deeper understanding of "how to trigger YoungGC or FullGC operation". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report