Java Virtual machine garbage collection mechanism

Source: Internet
Author: User

In a Java virtual machine, objects and arrays of memory are allocated in the heap, and the garbage collector's primary memory is the heap of memory. If the dynamically created objects or arrays are not recycled in time while the Java program is running, the final heap memory will be filled up, resulting in oom.

The JVM provides a garbage collection mechanism, referred to as the GC mechanism. Through the GC mechanism, the garbage objects in the heap can be recycled continuously during the operation, thus guaranteeing the normal operation of the program.

Determination of garbage objects

We all know that the so-called "garbage" object is the object that we are no longer useful in the process of running the program, that is, the object that no longer survives. So how do you judge that the objects in the heap are "garbage" and no longer live?

Reference counting method

Each object has a reference count property that is used to hold the number of times the object is referenced. When the number of references is 0 o'clock, it means that the object is not referenced, and will not be used in this object, it can be judged as a garbage object. However, there is a big bug in this approach, which is that there is no way to solve the problem of cross-referencing or circular referencing between objects: When two objects are referenced to each other, neither of them has a reference relationship with any other object, and neither of them has a reference count of 0, so they are not recycled, but actually they are no longer useful.

Accessibility analysis (Root search method)

To avoid the problem of using reference counting, Java uses the Accessibility analysis method to determine the garbage object.

This way you can think of the reference relationship of all objects as a tree, traversing all referenced objects from the root node of the tree, GC root, the tree node is the object, and the other objects that are not in the node are unreachable objects.

So what kind of object can be the root node of a GC?

    • Objects referenced in the virtual machine stack (local variable table in the frame stack)
    • Objects referenced by static properties in the method area
    • Objects referenced by constants in the method area
    • Objects in the local method stack that are referenced by JNI
Reference state

Garbage collection mechanisms, regardless of whether they are reference counting or accessibility analysis, are related to object references and there are four reference states in Java:

    • Strong references-Most of the references we use are actually strong references, and this is the most common reference used. If an object has a strong reference, it means that it is in a state of reach, the garbage collector never reclaims it, and even if the system memory is very tight, the Java virtual Machine prefers to throw an OutOfMemoryError error that causes the program to terminate unexpectedly, and does not reclaim objects referenced by a strong reference. Therefore, a strong reference is one of the main causes of Java memory leaks.

    • Soft reference-An object has only soft references, and if the memory space is sufficient, the garbage collector does not reclaim it, and if the memory space is insufficient, the memory of those objects is reclaimed. The object can be used by the program as long as it is not reclaimed by the garbage collector.

    • Weak reference-An object that has only a weak reference is similar to an optional one. Weak references are much like soft references, but weak references have lower reference levels. The difference between a weak reference and a soft reference is that an object with only a weak reference has a shorter life cycle. As the garbage collector thread scans the area of memory it governs, once an object with only a weak reference is found, its memory is reclaimed, regardless of whether the current memory space is sufficient or not.

    • Virtual reference-An object that only holds a virtual reference can be recycled at any time by the garbage collector, just as there are no references. Virtual references are primarily used to track the activities of objects that are garbage collected, which we do not normally use.

Garbage collection algorithm

By using the accessibility analysis algorithm to determine which objects need to be recycled, how do they need to be recycled?

Tag-Purge algorithm

You first need to mark the memory of the objects that can be reclaimed, and then clean up the reclaimed memory.

Tag-purge algorithm (before recycling)

Tag-purge algorithm (after recycling)

In this case, however, as the program runs, it allocates free memory, which in the heap produces a lot of discontinuous free memory areas, that is, memory fragmentation. This way, even with enough free memory, it is not always possible to allocate large enough memory, and it can cause frequent GC, impact efficiency, and even oom.

Tagging-sorting algorithms

Unlike the mark-and-sweep algorithm, the tag-grooming algorithm does not clean recyclable memory directly after tagging, but instead moves the surviving objects to one end and then clears the recyclable memory.

Tag-collation algorithm (before recycling)

Mark-and-Organize algorithm (after recycling)

The advantage of this is that memory fragmentation is not generated.

Replication Algorithms

The replication algorithm needs to divide the memory into two pieces, first allocating memory on one piece of memory, when the memory is allocated, then garbage collection is done, then all the surviving objects are copied to the other memory, and the first memory is emptied.

Replication algorithm (before recycling)

Replication algorithm (after recycling)

This algorithm does not produce memory fragmentation, but is equivalent to using only half of the memory space. At the same time, the replication algorithm and the number of surviving objects, if the number of surviving objects, then the efficiency of the replication algorithm will be greatly reduced.

Generational collection Algorithms

In a Java virtual machine, the life cycle of an object is long and short, the life cycle of most objects is short, and only a small number of objects persist for a long time in memory, so they can be placed in different regions depending on the length of the object's life cycle. In the Java Virtual Machine heap, which uses generational collection algorithms, it is generally divided into three regions, which are used to store these three types of objects separately:

    • New generation-objects that have just been created will continue to be created when the code is run, with many of the newly created objects being local variables that quickly become garbage objects. These objects are placed in an area of memory called the Cenozoic. The new generation is characterized by more garbage objects and fewer surviving objects.

    • Old age-some objects were created very early, and many times the GC was not recycled, but survived. These objects are placed in an area called the old age. The old age is characterized by the existence of more objects, less garbage objects.

    • Permanent generation-some objects that accompany the lifetime of a virtual machine, such as some static objects, constants, and so on. These objects are placed in an area called a permanent generation. The permanent generation feature is that these objects generally do not require garbage collection and will survive during the virtual machine run. (Before Java1.7, a permanent generation object was stored in the method area, and the permanent generation object of the Java1.7 method area was moved to the heap, and in the Java1.8 the permanent generation had been removed from the heap, this memory gave the meta space.) )

The generational collection algorithm is also based on the new generation and the old age of garbage collection.

For the Cenozoic region, every GC will have a lot of garbage objects recycled, only a small amount of survival. Therefore, using the copy-and-recycle algorithm, the GC will replicate the remaining few surviving objects in the past.

In the Cenozoic region, the 8:1:1 is divided into three regions of Eden, Survivora and Survivorb, rather than the ratio of 1:1 to the Copy collection. Eden, which is the garden, describes the creation of many new objects in it, while the survivor area is the survivor, the object that survives after the GC.

    1. The Eden area provides heap memory externally. When the Eden area is nearly full, the minor GC (new generation GC) is carried out, and the surviving objects are placed in the Survivora area, emptying the Eden area;
    2. After the Eden Zone is emptied, it continues to provide the heap memory externally;
    3. When the Eden Zone is filled again, the minor GC (new generation GC) is carried out at the same time for the Eden and Survivora areas, and the surviving objects are placed in the Survivorb area, at the same time emptying the Eden and Survivora areas;
    4. The Eden area continues to provide heap memory to the outside and repeats the process by placing the surviving objects in the Eden area and one survivor area in another survivor area after the Eden area is filled;
    5. When a survivor area is filled and there are still objects that are not copied, or if some objects are survive 15 times or so, the remainder of the object is placed in the old age area, and when the old age zone is filled, the major GC (the old age GC) is collected for garbage collection in the old age area.

The old age area object generally has a long life cycle, each time the GC, the survival of more objects, so the use of the tag-collation algorithm, GC when moving a small number of surviving objects, does not produce memory fragmentation.

Type of triggering GC

The Java Virtual Opportunity prints the information that triggers the GC every time, and the reason for triggering the GC can be analyzed based on the log.

    • Gc_for_malloc: Represents a GC that is not memory-triggered when allocating objects on a heap.
    • Gc_concurrent: When our application's heap memory reaches a certain amount, or it can be understood to be nearly full, the system automatically triggers a GC operation to free up memory.
    • GC_EXPLICIT: Represents the GC that is triggered when an application calls the System.GC, VMRUNTIME.GC interface, or receives a SIGUSR1 signal.
    • Gc_before_oom: Represents a GC that is triggered by a final effort before preparing to throw an OOM exception.

Java Learning Exchange QQ Group: 523047986 prohibit small talk, non-happy do not enter!

Java Virtual machine garbage collection mechanism

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.