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 understand the garbage collection mechanism in common knowledge points of Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article will explain in detail how to understand the garbage collection mechanism in common knowledge points of Java. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

There are a large number of Java object instances in the Java heap, and before the garbage collector reclaims memory, the first thing to do is to determine which objects are "alive" and which can be recycled.

The Stop-The-World mechanism in Java, referred to as STW, means that when the garbage collection algorithm is executed, all other threads of the Java application are suspended (except the garbage collection helper). In Java, a global pause, global pause, all Java code stops, native code can execute, but cannot interact with JVM; these phenomena are mostly caused by gc.

one。 An algorithm for judging whether an object is alive or not

1. Reference counting algorithm (basically deprecated)

The reference counting algorithm is the basic algorithm to judge whether an object is alive or not: add a reference counter to each object, and when it is not referenced in a place, the counter value increases by 1; when the reference expires, the counter value is subtracted by 1. But this approach has a fatal flaw that when two objects refer to each other, neither of them can be recycled.

two。 Root search algorithm (currently in use)

In the mainstream commercial languages (Java, C # … The root search algorithm is used to determine whether the object is alive. For a program, the root object is always accessible. From these root objects, any object that can be touched is considered to be a "living" object. Untouchable objects are considered rubbish and need to be recycled.

The collection of root objects for the Java virtual machine varies from implementation to implementation, but always includes the following aspects:

The object referenced in the stack (the local variable table in the stack frame).

The variable referenced by the static property of the class in the method area.

A variable referenced by a constant in the method area.

The reference object of the local method JNI.

The two basic ways to distinguish between active objects and garbage are reference counting and root search. Reference counting distinguishes active objects from garbage by saving a count for each object in the heap. The root search algorithm actually tracks the reference graph starting from the root node.

In the mainstream implementation of mainstream commercial programming languages (such as our Java), reachability analysis algorithms are used to determine whether the object is alive or not.

two。 Garbage collection algorithm

1. Mark-clear algorithm

It can be divided into two stages: first, the objects that need to be recycled at the mark, and all the marked objects will be recycled uniformly after the marking is completed.

It has two shortcomings: one is the efficiency problem, the marking and removal process is inefficient; the other is the space problem, after the mark removal will produce a large number of discontinuous memory fragments (similar to our computer disk debris). Too much space debris makes it impossible to find enough contiguous memory when large objects need to be allocated and has to trigger another garbage collection action in advance.

two。 Replication algorithm

In order to solve the problem of efficiency, there is a "replication" algorithm, which divides the available memory into two equal blocks according to capacity, and only needs to use one of them at a time. When one piece of memory is used up, copy the surviving objects to another, and then clean up the memory space that has just been used up at once. This solves the problem of memory fragmentation, but at the cost of reducing the content to half of the original.

3. Marking-finishing algorithm

The replication algorithm will copy frequently when the object survival rate is high, and the efficiency will be reduced. Therefore, there is a mark-collation algorithm, and the marking process is the same as the mark-removal algorithm, but in the subsequent steps, instead of cleaning up the objects directly, all the surviving objects are moved to one side, and then the memory outside the end boundary is cleared directly.

4. Generation collection method

At present, the GC of commercial virtual machines adopts generational collection algorithm.

In order to increase the efficiency of garbage collection, JVM divides the heap into different parts, which are generally divided into three parts, namely, the new generation, the old generation and the permanent generation (in the new version, the permanent generation has been discarded and the concept of metaspace has been introduced. The permanent generation uses JVM memory and the metaspace uses physical memory directly):

New generation (corresponding to minor GC): all objects from the new new will first appear in the new generation. When the new generation is full of memory, it will initiate a garbage collection event, which occurs in the new generation of garbage collection called Minor collections. This kind of collection is usually faster, because most of the objects of the new generation need to be recycled, and those that cannot be recycled temporarily will be moved to the old age.

Old age (corresponding to Full GC): the old age is used to store objects that have lived for a long time. Generally speaking, we will limit the survival time of the new generation of objects, and when this time has not been collected, it will be moved to the old age.

Permanent generation: used to store static files, such as Java classes, methods, etc. Persistent generation has no significant impact on garbage collection, but some applications may dynamically generate or call some class, such as Hibernate, at this time, you need to set a large persistent generation space to store these new classes.

The GC that is enforced by actively calling System.gc () in the program is Full GC.

Approximate process:

Memory partitions:

Younger generation (Young Generation) (Eden,Survivor-s0,Survivor-s1, the size ratio defaults to 8:1:1)

Older generation (Old Generation)

Permanent generation (Permanent Generation). Contains the applied class / method information, as well as the class and method information of the JRE library. Basically has nothing to do with garbage collection)

Objects in the new generation "die every day". Every GC, a large number of objects will die, a small number of objects will survive, and replication algorithms will be used. The Cenozoic is subdivided into Eden region and Survivor region (Survivor from, Survivor to), and the default size ratio is 8:1:1.

Objects in the old era used mark-clear or mark-collation algorithms because of their high survival rate and no extra space for allocation guarantee.

The newly generated objects first enter the Eden area, and then use Survivor from when the Eden area is full. When the Survivor from is full, then Minor GC (the new generation GC), enter the copy of the surviving objects in Eden and Survivor from into the Survivor to, and then empty the Eden and Survivor from. At this time, the original Survivor from becomes the new Survivor to, and the original Survivor to becomes the new Survivor from. When copying, if the Survivor to cannot accommodate all the surviving objects, then according to the allocation guarantee of the old age (similar to the loan guarantee of the bank), the object copy into the old age, and if the old age cannot accommodate it, then Full GC (the old age GC).

Large objects go directly to the old age: there is a parameter configuration in JVM-XX:PretenureSizeThreshold, which allows objects larger than this setting to enter the old age directly, in order to avoid a large amount of memory replication between the Eden and Survivor areas.

Long-lived objects enter the old age: JVM defines an object age counter for each object. If the object is born in Eden and survives after the first Minor GC, and can be accommodated by Survivor, it will be moved to Survivor and the age will be set to 1. Every time he gets through Minor GC, his age increases by 1, and when he reaches a certain age (the default is 15, which can be set through XX:MaxTenuringThreshold), he will move into the old age.

However, JVM does not always require that the age must reach the maximum age in order to promote the old age. If the total size of all objects of the same age (such as age x) in the Survivor space is more than half of the Survivor, all objects older than or equal to x will directly enter the old age without waiting for the maximum age requirement.

three。 Garbage collector

Garbage collection algorithm is methodology, garbage collector is implementation.

Serial collectors: serial collectors are the oldest, most stable, and efficient collectors that may cause long pauses and use only one thread to retrieve. (STW) it is the default new generation collector for virtual machines running in client mode: simple and efficient (compared to a single thread of other collectors, because there is no thread switching overhead, etc.).

ParNew collector: the ParNew collector is actually a multithreaded version of the Serial collector. (STW) is the preferred new generation collector for many JVM running in Server mode, and one of the most important reasons is that apart from Serial, only he can work with the old CMS collector.

Parallel Scavenge collector: the Parallel Scavenge collector is similar to the ParNew collector, and the Parallel collector is more concerned about the throughput of the system (that is, the ratio of the time CPU takes to run user code to the total time consumed by CPU, that is, throughput = time to run user code / [time to run user code + garbage collection time]).

CMS collector: the CMS (Concurrent Mark Sweep) collector is a collector that aims to obtain the shortest recovery pause time. If the pause time is short, the user experience is good

Based on the "tag removal" algorithm, concurrent collection, low pause, complex operation process, which is divided into four steps:

Initial tagging: marking only objects to which GC Roots can be directly associated, fast, but requires "Stop The World"

Concurrent tagging: the process of tracking reference chains, which can be executed concurrently with user threads.

Retag: fix the tag record of the object whose tag changes because the user thread continues to run in the concurrent marking phase, which is longer than the initial marking time but much shorter than the concurrent marking time, and requires "Stop The World"

Concurrent cleanup: cleanup is marked as recyclable and can be executed concurrently with user threads

Since the most time-consuming concurrency markup and concurrent cleanup of the entire process can work with user threads, in general, the memory recovery process and user threads of the CMS collector are executed concurrently.

The CSM collector has three disadvantages:

1) very sensitive to CPU resources

Although concurrent collection does not pause user threads, it still slows down the application and reduces overall throughput because it takes up part of the CPU resources.

The default number of collection threads for CMS is = (number of CPU + 3) / 4; when the number of CPU is more than 4, the collection thread takes up more than 25% of CPU resources, which may have a greater impact on user programs; when there are less than 4, the impact is even greater and may be unacceptable.

2) unable to handle floating garbage (during concurrent cleanup, the new garbage generated by the user thread is called floating garbage), and a "Concurrent Mode Failure" failure may occur.

Concurrent cleanup requires a certain amount of memory space, which cannot be collected after almost filling up like other collectors in the old years. If the CMS reserved memory space cannot meet the needs of the program, there will be a "Concurrent Mode Failure" failure. At this time, JVM enables the backup plan: temporarily enable the Serail Old collector, resulting in another Full GC generation.

3) generate a large number of memory fragments: based on the "mark-clear" algorithm, CMS does not compress after cleaning, resulting in a large number of discontiguous memory fragments, which will result in not finding enough contiguous memory when allocating large memory objects, which requires another Full GC action to be triggered in advance.

Serial Old collector: an old version of the Serial collector, single-threaded, "tagging" algorithm, mainly for virtual machines in Client mode. It can be used as the back scheme of CMS, and Concurrent Mode Failure is used when CMS occurs.

Parallel Old Collector: an old version of Parallel Scavenge, multithreaded, "tag finishing" algorithm, JDK 1.6. Before this, Parallel Scavenge can only be used with Serial Old. Due to the poor performance of Serial Old, the advantages of Parallel Scavenge can not be brought into full play. With the emergence of Parallel Old collector, the "throughput first" collector finally has a veritable combination. Parallel Scavenge/Parallel Old combinations can be used in situations where throughput and CPU are sensitive.

G 1 collector: G 1 (Garbage-First) is a server-oriented garbage collector for machines with multiple processors and large memory. While meeting the GC pause time requirements with a very high probability, it also has the characteristics of high throughput performance. G1 is a garbage collector for server applications. Its mission is to replace the CMS collector in the future.

On how to understand Java common knowledge points in the garbage collection mechanism to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Internet Technology

Wechat

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

12
Report