Note source: Silicon Valley JVM complete tutorial, millions of playback, the peak of the entire network (Song Hongkang details Java virtual machine)

Update: gitee.com/vectorx/NOT…

Codechina.csdn.net/qq_35925558…

Github.com/uxiahnan/NO…

[TOC]

11. Overview and algorithm of garbage collection

11.1. Overview of garbage collection

11.1.1. What is garbage?

Garbage collection is not a companion product of the Java language. Back in 1960, Lisp was the first language to use dynamic memory allocation and garbage collection techniques.

There are three classic questions about garbage collection:

  • What memory needs to be reclaimed?
  • When is it recycled?
  • How to recycle?

Garbage collection is Java’s signature capability and greatly improves development efficiency. Nowadays, garbage collection has almost become the standard of modern languages. Even after such a long time of development, Java garbage collection mechanism is still evolving. Different sizes of devices and application scenarios with different characteristics pose new challenges to garbage collection, which is certainly a hot topic for interviews.

Big factory interview question

The ant gold dress

  • Which garbage collectors do you know, and their pros and cons, specifically CMS and G1?

  • What are the JVM GC algorithms, and what is the collection algorithm used in the current JDK version?

  • G1 collector What is GC? Why GC?

  • Two kinds of determination methods of GC? Features of CMS collector and G1 collector

baidu

  • Let’s talk about GC. Let’s talk about generational collection

  • Garbage collection policies and algorithms

Tmall

  • How does a JVM reclaim memory

  • CMS features, garbage collection algorithms? What are their strengths and weaknesses? What are their common weaknesses?

drops

  • What are the Java garbage collectors? Tell us about g1 application scenarios. How do you usually use garbage collectors together

jingdong

  • Do you know which garbage collectors, their pros and cons, specifically CMS and G1,

  • Including principles, processes, advantages and disadvantages. The realization principle of garbage collection algorithm

Ali.

  • Let’s talk about garbage collection algorithms.

  • When is garbage collection triggered?

  • How to choose the right garbage collection algorithm?

  • What are the three garbage collectors for the JVM?

Bytes to beat

  • What are the common garbage collector algorithms, and what are the pros and cons of each?
  • What do system.gc () and Runtime.gc () do?
  • Java GC mechanism? What GC Roots do you have?
  • Java object recycling method, recycling algorithm.
  • CMS and G1, what kind of problem does CMS solve? Talk a little bit about the recycling process.
  • CMS recycle paused several times, why did it pause twice?

What is garbage?

An object is considered garbage when it can no longer be reached from any pointer in the running program

Garbage is an object that has no pointer to it in the running program. This object is garbage that needs to be collected.

If garbage objects in the memory are not cleaned up in time, the memory space occupied by these garbage objects will be reserved until the end of the application, and the reserved space cannot be used by other objects, or even memory overflow may occur.

The day of disk defragmentation

Mechanical hard disks need to be disorganized and have bad tracks

11.1.2. Why is GC needed

To learn about GC, you need to understand why GC is needed in the first place.

For high-level languages, the basic understanding is that without garbage collection, memory will run out sooner or later, because constantly allocating memory without recycling is like constantly producing garbage without ever cleaning it up.

In addition to releasing useless objects, garbage collection can also clear memory of record fragments. Defragmentation moves occupied heap memory to one end of the heap so that the JVM can allocate it to new objects.

As applications deal with larger and more complex businesses and more users, they cannot be kept running without GC. The GC that often causes STW can’t keep up with the actual requirements, so there are constant attempts to optimize GC.

11.1.3. Early garbage collection

In the early days of C/C++, garbage collection was largely manual. Developers can use the new keyword for memory allocation and the delete keyword for memory release. For example:

MibBridge *pBridge= new cmBaseGroupBridge(a);// If the registration fails, use Delete to free the memory area occupied by the object
if (pBridge->Register(kDestroy) ! = NO ERROR)delete pBridge;
Copy the code

This method can flexibly control the time of memory release, but it will bring management burden to the developer to frequently apply for and release memory. If an area of memory is forgotten to be collected due to programmer coding problems, a memory leak can occur, and garbage objects can never be cleaned up. As the system runs longer, garbage objects can continue to consume more memory until they overflow and cause the application to crash.

With a garbage collection mechanism, the above code will most likely look like this

MibBridge *pBridge = new cmBaseGroupBridge(a); pBridge->Register(kDestroy);
Copy the code

Nowadays, in addition to Java, C#, Python, Ruby and other languages all use the idea of automatic garbage collection, which is also the future development trend. It can be said that this automatic memory allocation and collection method has become a necessary standard of online development languages.

11.1.4. Java garbage collection mechanism

Automatic memory management reduces the risk of memory leaks and spills by eliminating the need for developers to manually allocate and reclaim memory

  • Without the garbage collector, Java would be like CPP, with dangling Pointers, wild Pointers, and leaks.

The automatic memory management mechanism frees programmers from the heavy memory management and allows them to concentrate more on business development

Oracle’s website about the introduction of recycling docs.oracle.com/javase/8/do…

Concerns about

Automatic memory management is a black box for Java developers, and relying too heavily on “automatic” can be a disaster, at its worst weakening the Java developer’s ability to locate and resolve problems when a program runs out of memory.

At this point, it is important to understand how the JVM automatically allocates and reclaims memory. Only with a true understanding of how the JVM manages memory can we quickly locate and resolve outofMemoryErrors based on the error exception log.

These “automated” technologies need to be monitored and tuned when it comes to troubleshooting memory spills and leaks, and when garbage collection becomes a bottleneck for higher concurrency.

Areas of primary concern for GC

GC focuses on method areas and garbage collection in the heap

Garbage collectors can collect from young generations, old generations, and even full stacks and method areas. The Java heap is the focus of the garbage collector

In terms of times:

  • Frequent collection of Young areas
  • Less collection of Old zones
  • Almost no Perm area is collected

11.2. Algorithms related to garbage collection

Object survival judgment

Almost all Java object instances are stored in the heap, and before the GC can perform garbage collection, it first needs to distinguish between alive and dead objects in memory. Only objects marked as dead are freed by GC during garbage collection, so this process can be referred to as the garbage marking phase.

So how exactly do you mark a dead object in the JVM? Simply put, an object is declared dead when it is no longer referenced by any living object.

There are two ways to judge the survival of objects: reference counting algorithm and reachability analysis algorithm.

11.2.1. Marking stage: reference counting algorithm

Method 1: Reference counting algorithm

The Reference Counting algorithm is relatively simple and stores a Reference counter attribute of an integer for each object. Used to record when an object is referenced.

For an object A, if any object references A, then A’s reference counter is incremented by 1. When a reference is invalid, the reference counter is decayed by one. As long as the reference counter of object A has A value of 0, it indicates that object A can no longer be used and can be reclaimed.

Advantages: simple implementation, garbage object easy to identify; High judgment efficiency and no delay in recovery.

Disadvantages:

  • The need for separate fields to store counters has increasedStorage space cost.
  • Each assignment requires updating the counter, which increases with addition and subtraction operationsThe time overhead.
  • Reference counters have a serious problem, namelyUnable to handle circular referencesIn the case. This is a fatal flaw that prevents such algorithms from being used in the Java garbage collector.

A circular reference

When the pointer to P breaks, the internal reference forms a loop, which is called a circular reference

For example,

Test whether a reference counting algorithm is used in Java

public class RefCountGC {
    // The only function of this member attribute is to take up a bit of memory
    private byte[] bigSize = new byte[5*1024*1024];
    / / reference
    Object reference = null;

    public static void main(String[] args) {
        RefCountGC obj1 = new RefCountGC();
        RefCountGC obj2 = new RefCountGC();
        
        obj1.reference = obj2;
        obj2.reference = obj1;
        
        obj1 = null;
        obj2 = null;
        // The garbage collection behavior is displayed
        Obj1 and obj2 are collected?System.gc(); }}// Run the result
PSYoungGen: 15490K->808K(76288K)] 15490K->816K(251392K)
Copy the code

The GC collection behavior described above proves that the JVM is not using a reference counter algorithm

summary

Reference counting algorithms are the recycle of choice for many languages, such as Python, which has become more popular due to artificial intelligence, and supports both reference counting and garbage collection.

Which is optimal depends on the scenario, and there are large-scale attempts to retain only reference counting mechanisms in practice to improve throughput.

Java did not choose reference counting because of the basic difficulty of handling circular reference relationships.

How does Python solve circular references?

  • Manual dereference: It is well understood that when appropriate, the dereference relationship. Use weakreference WeakRef, a standard library provided by Python that is designed to address circular references.

11.2.2. Marking stage: Accessibility analysis algorithm

Reachability analysis algorithm (root search algorithm, traceable garbage collection)

Compared with reference counting algorithm, reachability analysis algorithm not only has the characteristics of simple implementation and efficient execution, but also can effectively solve the problem of cyclic reference in reference counting algorithm and prevent the occurrence of memory leakage.

In contrast to the reference counting algorithm, the reachability analysis is chosen by Java and C#. This type of Garbage Collection is often called Tracing Garbage Collection

The “GCRoots “root collection is a set of references that must be active.

The basic idea

  • The reachability analysis algorithm takes the root object set (GCRoots) as the starting point and works from top to bottomSearches for reachability of the target object connected by the root object collection.
  • After using the reachability analysis algorithm, the living objects in memory are directly or indirectly connected by the root object set, and the search path is calledReference Chain
  • If the target object is not connected by any reference chain, it is unreachable, which means that the object is dead and can be marked as a garbage object.
  • In the reachability analysis algorithm, only the objects that can be connected directly or indirectly by the root object set are the viable objects.

In the Java language, GC Roots includes the following classes of elements:

  • Object referenced in the virtual machine stack
    • For example: parameters, local variables, etc. used in methods called by each thread.
  • Objects referenced by JNI (commonly referred to as local methods) in the local method stack
  • The object referenced by the class static property in the method area
    • Example: Java class reference type static variable
  • The object referenced by the constant in the method area
    • For example, references in the String constant pool
  • All objects held by synchronized
  • Internal references of the Java VIRTUAL machine.
    • Class objects corresponding to basic data types, some resident exception objects (NullPointerException, OutOfMemoryError), system Class loaders.
  • Jmxbeans that reflect Java virtual machine internals, callbacks registered in JVMTI, local code caches, and so on.

In addition to these fixed COLLECTION of GC Roots, other objects can be added “temporarily” to form a complete collection of GC Roots, depending on the garbage collector selected by the user and the memory region currently reclaimed. For example: generational collection and partial recovery (PartialGC).

If you do garbage collection for only one area of the Java heap (for example: Typical only for new generation), must consider the memory region is a virtual machine implementation details, more is not isolated, sealed the area of the object can be referenced in other parts of the object, then you need will be along with all the associated region object joined GCRoots collection to consider, to ensure the accuracy of the accessibility analysis.

Tip: Since Root stores variables and Pointers on a stack, a pointer that holds objects in the heap but is not stored in the heap itself is Root.

Pay attention to

If a reachability analysis algorithm is used to determine whether memory is retrievable, the analysis must be performed in a consistent snapshot. If this is not satisfied, the accuracy of the analysis results cannot be guaranteed.

This is also an important reason why GC must “stop The World”.

  • Even CMS collectors, which claim to be (almost) non-pausing, are required to pause when enumerating root nodes.

11.2.3. Finalization mechanisms for objects

The Java language provides an object finalization mechanism that allows developers to provide custom processing logic for objects before they are destroyed.

When the garbage collector finds that there is no reference to an object, that is, before garbage collection of the object, the Finalize () method of the object is always called.

The Finalize () method allows to be overridden in subclasses for resource release when objects are reclaimed. This method usually does some resource freeing and cleaning, such as closing files, sockets, and database connections.

Never actively call finalize() method I of an object should be left to garbage collection. There are three reasons:

  • Objects may be resurrected when finalize().
  • The execution time of Finalize () method is not guaranteed. It is completely determined by GC thread. In extreme cases, if GC does not happen, Finalize () method will have no chance to execute.
  • A bad Finalize () can seriously impact Gc performance.

In terms of function, finalize() method is similar to the destructor in C++, but Java adopts automatic memory management mechanism based on garbage collector, so finalize() method is fundamentally different from the destructor in C++.

Objects ina virtual machine generally have three possible states, thanks to finalize().

To be or not to be?

If an object is unreachable from all root nodes, it is no longer in use. In general, this object needs to be reclaimed. But in fact, they are not necessarily dead. They are on probation. An unreachable object may “resurrect” itself under certain conditions, and if so, it is unreasonable to reclaim it. To this end, define three possible states for an object in a virtual machine. As follows:

  • palpable: The object can be reached from the root node.
  • Can the resurrection: All references to objects are released, but objects may be resurrected in Finalize ().
  • untouchableWhen the finalize() of: object is called and is not resurrected, it will enter the untouchable state. Untouchable objects can’t be resurrected becauseFinalize () will only be called once.

The three states above are distinguished by the existence of the inalize() method. Objects can only be reclaimed if they are not reachable.

The specific process

To determine whether an object objA is recyclable, we must go through at least two marking processes:

  1. The first mark is made if the object objA to GC Roots has no chain of references.
  2. Filter to determine if this object needs to be finalize()
  3. If the object objA does not override the Finalize () method, or if the Finalize () method has already been called by a virtual machine, then the virtual machine considers it “unnecessary to execute” and objA is judged untouchable.
  4. If the object objA overrides the Finalize () method and has not yet executed it, then the object will be inserted into the F-Queue and its Finalize () method will be triggered by a low-priority Finalizer thread automatically created by the virtual machine.
  5. The Finalize () method is an object’s last chance to escape deathLater, the GC marks the objects in the f-queue a second time. If objA is linked to any object in the reference chain in the Finalize () method, then on the second markup, objA is removed from the “to be collected” collection. After that, the object will again have no references. In this case, the Finalize method will not be called again, the object will directly become untouchable state, that is to say, the Finalize method of an object will only be called once.

For example,

public class CanReliveObj {
    // Class variable, part of GC Roots
    public static CanReliveObj canReliveObj;

    @Override
    protected void finalize(a) throws Throwable {
        super.finalize();
        System.out.println("Call finalize() method overridden by the current class");
        canReliveObj = this;
    }

    public static void main(String[] args) throws InterruptedException {
        canReliveObj = new CanReliveObj();
        canReliveObj = null;
        System.gc();
        System.out.println("----------------- First GC operation ------------");
        // Pause for 2 seconds to wait for the Finalizer thread because it has a lower priority
        Thread.sleep(2000);
        if (canReliveObj == null) {
            System.out.println("obj is dead");
        } else {
            System.out.println("obj is still alive");
        }

        System.out.println("----------------- Second GC operation ------------");
        canReliveObj = null;
        System.gc();
        // The following code is the same as above, but canReliveObj fails to save itself
        Thread.sleep(2000);
        if (canReliveObj == null) {
            System.out.println("obj is dead");
        } else {
            System.out.println("obj is still alive"); }}}Copy the code

The results

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the first time the gc operation -- -- -- -- -- -- -- -- -- -- -- -- call the current class of rewriting the finalize () method of the obj is still alive -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the second gc -- -- -- -- -- -- -- -- -- -- -- -- obj is  deadCopy the code

On the first GC, the Finalize method is executed, but the Finalize () method is only called once, so the second time the object is tagged and cleaned up by GC.

11.2.4. GC Roots tracing of MAT and JProfiler

What is MAT?

MAT, short for Memory Analyzer, is a powerful Java heap Memory Analyzer. Used to find memory leaks and view memory consumption.

MAT is based on Eclipse and is a free performance analysis tool.

You can download and use MAT at www.eclipse.org/mat/

Obtain dump file

Method 1: Use jmap for command lines

Method 2: Export using JVisualVM

The captured heap dump file is a temporary file that is automatically deleted when JVisualVM is shut down. To save it, you need to save it as a file.

Heap dump can be captured by:

  • In the Application subwindow on the left, right-click the Application and select Heap Dump.

  • Click the Heap Dump button in the Monitor subtab.

Heap DUMPS for the local application opens as a subtab of the application TAB. Also, the heap dump corresponds to a time-stamped node in the Application (Application) column on the left.

Right click on this node and select Save as to save the heap dump locally.

Method 3: Open the Dump file using MAT

GC Roots tracing of JProfiler

JProfiler can be used when we are not looking for all GC Roots in actual development, but just looking for the whole link of an object, or GC Roots tracing

11.2.5. Clearance phase: mark-clearance algorithm

After successfully separating live and dead objects in memory, the next task of the GC is to perform garbage collection to free up the memory space occupied by the unused objects so that there is enough available memory to allocate memory for the new objects.

Three garbage collection algorithms that are common in JVMS today are Mark-sweep, copying, and Mark-compact

Mark-sweep algorithm

Mark-sweep is a very basic and common garbage collection algorithm, which was proposed by J.McCarthy et al in 1960 and applied to Lisp.

Implementation process

When available memory in the heap is exhausted, the entire program (also known as stop the world) is stopped and two tasks are performed, the first is marked and the second is cleared

  • Tag: Collector traverses from the reference root node, marking all referenced objects. It is usually recorded as reachable in the Header of an object.

  • Cleanup: The Collector traverses the heap memory linearly from beginning to end and reclaims an object if it is not marked as reachable in its headers

disadvantages

  • The efficiency of the tag – clearing algorithm is not high
  • During GC, the entire application needs to be stopped and the user experience is poor
  • The free memory cleared in this way is discontinuous, resulting in internal fragmentation and the need to maintain a free list

What is clearance?

This does not mean that the object addresses need to be cleared are stored in the free address list. The next time a new object needs to be loaded, determine whether the garbage location space is enough, if so, store overwrite the original address.

11.2.6. Cleanup phase: Copy algorithm

Copying algorithms

In order to address the shortcomings of mark-sweep algorithms in garbage collection efficiency, M.L. Insky published a famous paper in 1963, Lisp Garbage Collector CA Lisp Garbage Collector Algorithm Using Serial Secondary Storage. The algorithms m.L. Insky described in this paper are known as Copying algorithms, which m.L. Insky himself successfully introduced into an implementation of Lisp.

core idea

The living memory space is divided into two pieces and only one piece is used each time. During garbage collection, the living objects in the used memory are copied to the unused memory block. After that, all objects in the used memory block are cleared, the roles of the two memory blocks are swapped, and finally garbage collection is completed

advantages

  • No marking and cleaning process, simple implementation, efficient operation
  • After copying the past to ensure the continuity of space, there will be no “fragmentation” problem.

disadvantages

  • The disadvantage of this algorithm is also obvious, that is, it requires twice the memory space.
  • For GC with a large number of regions, such as G1, replication rather than movement means that the GC needs to maintain object reference relationships between regions, which is not a small amount of memory or time

special

If the system has a lot of junk objects, the number of living objects that the replication algorithm needs to copy is not very large, or very low

Application scenarios

In the new generation, garbage collection for conventional applications can typically reclaim 70 to 99 percent of memory space at a time. Recycling is cost-effective. So now commercial virtual machines are using this collection algorithm to recycle the new generation.

11.2.7. Cleanup phase: mark-compress (collation) algorithm

Mark-compress (or mark-tidy, Mark-compact) algorithm

The high efficiency of the replication algorithm is based on the premise of fewer live objects and more garbage objects. This happens a lot in the new generation, but in the old age, it’s more common for most objects to be living objects. If the replication algorithm is still used, the cost of replication will be high due to the large number of living objects. Therefore, due to the nature of old-time garbage collection, other algorithms need to be used.

It is true that the tag-one cleanup algorithm can be used in the old days, but it is not only inefficient, but also generates memory fragmentation after a collection is performed, so JVM designers need to improve on this. The mark-compact algorithm was born.

Around 1970, g.L. Steele, C.J.Chene, D.S. Ise and other researchers published mark-compression algorithms. In many modern garbage collectors, mark-compression algorithms or improved versions of them are used.

Implementation process

  1. The first stage marks all referenced objects from the root node, as in the mark-clearing algorithm

  2. The second phase compresses all living objects to one end of memory and discharges them in sequence.

  3. After that, clean up all the space outside the boundary.

The end result of the mark-sweep-compact algorithm is the same as the memory defragmentation once the mark-sweep-compact algorithm is complete, so it can also be called a Mark-sweep-compact algorithm.

The essential difference between them is that the mark-sweep algorithm is a non-mobile recovery algorithm, while the mark-compression algorithm is mobile. Whether or not to move a reclaimed live object is a risky decision with both advantages and disadvantages. As you can see, marked living objects are sorted by memory address, while unmarked memory is cleaned up. This way, when we need to allocate memory for new objects, the JVM only needs to hold the starting address of memory, which is significantly less overhead than maintaining a free list.

Bump the Pointer

If the memory space is distributed in a regular and orderly manner, that is, the used and unused memory are on opposite sides of each other with a marker pointer that records the starting point of the next allocation. When allocating memory for a new object, it is only necessary to change the offset of the pointer to allocate the new object to the first free memory location. This allocation is called Bump tHe Pointer.

advantages

  • Eliminating the fragmentation of memory in the mark-clean algorithm, the JVM only needs to hold the starting address of memory when allocating memory to a new object.
  • Eliminates the high cost of halving memory in the replication algorithm.

disadvantages

  • In terms of efficiency, the mark-collation algorithm is lower than the copy algorithm.
  • When you move an object, you need to adjust the address of the reference if it is referenced by another object
  • You need to pause the user application throughout the movement. Namely: STW

Nodule 11.2.8.

Mark-Sweep Mark-Compact Copying
rate medium The slowest The fastest
The space overhead Less (but will pile up debris) Less (do not accumulate debris) Usually requires twice as much space as live objects (no shards)
Moving objects no is is

In terms of efficiency, the replication algorithm is the leader, but it wastes too much memory.

In order to take into account the three indicators mentioned above, the mark-collation algorithm is relatively smoother, but its efficiency is not satisfactory. It has one more mark stage than the copy algorithm, and one more memory collation stage than the mark-clean algorithm

Isn’t there an optimal algorithm?

Answer: no, there is no best algorithm, only the most suitable algorithm.

11.2.9. Generational collection algorithm

None of these algorithms can completely replace other algorithms, they all have their own unique advantages and characteristics. Generational collection algorithm came into being.

The generational collection algorithm is based on the fact that different objects have different life cycles. Therefore, objects with different life cycles can be collected in different ways to improve collection efficiency. Typically, the Java heap is divided into the new generation and the old generation, so that different collection algorithms can be used according to the characteristics of each generation to improve the efficiency of garbage collection.

In the running process of Java programs, a large number of objects will be generated, some of which are related to business information, such as Session objects, threads and Socket connections in Http requests. Such objects are directly linked to business, so their life cycle is relatively long. But there are some objects, mainly temporary variables generated in the process of running the program, the life cycle of these objects will be relatively short, such as: String object, because of its invariable class characteristics, the system will produce a large number of these objects, some objects can even be recycled only once.

At present, almost all GC uses generational mobile algorithm to perform garbage collection.

In HotSpot, based on the concept of generation, the memory reclamation algorithm used by GC must combine the characteristics of both the young generation and the old generation.

Young Gen

Characteristics of the young generation: the area is smaller than that of the old generation, the object life cycle is short, the survival rate is low, and the recycling is frequent.

In this case, the recovery of the replication algorithm is the fastest. The efficiency of the replication algorithm depends only on the size of the current living object, so it is suitable for young generation recycling. The problem of low memory utilization of the replication algorithm is alleviated by the two survivor designs in hotspot.

Tenured Gen

Characteristics of the old age: large area, long life cycle of objects, high survival rate, recycling less frequent than the young generation.

In this case, there are a large number of objects with high survival rates, and the replication algorithm becomes obviously unsuitable. It is usually implemented by mark-clean or a mixture of mark-clean and mark-clean.

  • The cost of the Mark phase is proportional to the number of viable objects.
  • The overhead of the Sweep phase is positively related to the size of the area managed.
  • The overhead of the Compact phase is proportional to the data of the living object.

Take the CMS collector in HotSpot as an example. CMS is implemented based on Mark-sweep and has a high recycling efficiency for objects. For the fragmentation problem, CMS uses Serial Old collector based on Mark-Compact algorithm as a compensation measure: When memory collection is poor (due to a Concurrent Mode Failure due to fragmentation), Serial Old is used to perform Full GC to defragment Old memory.

The generational idea is widely used by existing virtual machines. Almost all garbage collectors distinguish between the new generation and the old generation

11.2.x. Incremental collection algorithm, partitioning algorithm

Incremental collection algorithm

With the existing algorithm, the application will be in a Stop the World state during garbage collection. In the Stop the World state, all threads of the application are suspended, suspending all normal work and waiting for the garbage collection to complete. If garbage collection takes too long, the application will be suspended for a long time, which will seriously affect user experience or system stability. In order to solve this problem, the research on real-time garbage collection algorithm directly led to the birth of Incremental Collecting algorithm.

The basic idea

If processing all the garbage at once requires a long system pause, alternate the garbage collection thread with the application thread. Each time, the garbage collection thread collects only a small area of memory space, and then switches to the application thread. Repeat until garbage collection is complete.

In general, incremental collection algorithms are still based on traditional mark-sweep and copy algorithms. Incremental collection algorithms allow garbage collection threads to mark, clean, or copy in a phased manner by properly handling conflicts between threads

disadvantages

Using this approach reduces system downtime because application code is also intermittently executed during garbage collection. However, the overall cost of garbage collection increases due to the consumption of thread switches and context transitions, resulting in a decrease in system throughput.

Partitioning algorithm

In general, under the same conditions, the larger the heap space, the longer the time required for a Gc and the longer the pauses associated with Gc. To better control the pauses generated by GC, reduce pauses generated by a GC by splitting a large memory area into smaller chunks and reasonably reclaiming several cells at a time, rather than the entire heap space, based on the target pause times.

The generation algorithm divides the heap space into two parts according to the life cycle of the object, and the partition algorithm divides the whole heap space into continuous different cells.

Each area is used independently and recycled independently. The advantage of this algorithm is that it can control how many cells are recycled at a time.

Wrote last

Note that these are just the basic algorithm ideas, the actual GC implementation process is much more complex, currently under development frontier GC are composite algorithms, and both parallel and concurrent.