Deep understanding of Java Virtual Machines-garbage collection mechanism (GC)

Source: Internet
Author: User

The garbage collection mechanism (GC) is one of the most important features of Java. It frees developers from the need to focus on the creation and release of memory, but instead automatically reclaims garbage (useless objects) through GC.

Which memory needs to be recycled

Java heap and method area is the main memory area of garbage collection, program counter, virtual machine stack, local method stack, the memory area is out of the box, and the memory is recycled when the thread ends.

How can I tell if an object is recyclable?

In the Java heap, where almost all object instances are stored, the first thing a garbage collector can do before garbage collection is to determine which objects are "alive" and which objects are "dead" (objects that are no longer used);

The notation method of the number of arguments

The number of arguments is to add an argument counter to the object, whenever there are other places to refer to the object, the counter adds 1, when the reference fails, the counter is reduced by 1, if the counter at any time is 0, then the object cannot be used, the garbage collector can be recycled. The notation notation is simple and efficient, and Microsoft's COM technology, Python language, and Game scripting language squirrel are all using the argument notation for memory management. But mainstream Java virtual machines do not use the quoted notation for memory management, mainly because it is difficult for him to solve the problem of referencing each other between objects.

Accessibility analysis algorithm

The Java Virtual machine determines whether an object survives by an accessibility analysis algorithm. The main idea of the accessibility analysis algorithm is to search through a series of objects called "GC Roots" starting from these starting points, searching through the path called the reference chain, when an object to "GC Roots" does not have any reference chain connected ("GC Roots" to this object unreachable), It proves that this object is not available and recyclable.

In the Java language, objects that are available as "GC Roots" include the following

    • Objects referenced in the virtual machine stack ()
    • Objects referenced by class static variables in the method area
    • Objects referenced by constants in the method area
    • Objects in the local method stack that are referenced by JNI
Garbage collection algorithm

Brief introduction of several garbage collection algorithms

Tag-Purge algorithm

The simplest garbage collection algorithm is the "mark-clear" algorithm, the algorithm is divided into "mark" and "clear" two stages: first to mark the objects that need to be recycled, after the completion of the tag, the unified collection of all tagged objects. The tagging process is about determining which objects need to be recycled.
The tag-purge algorithm has two disadvantages:
One is the efficiency problem, the mark-clear efficiency is not high;
Another spatial problem, marked-clear, generates a large amount of discontinuous memory fragmentation, and too much memory space fragmentation can cause a garbage collection action to be triggered when a Java virtual machine cannot find enough contiguous memory to allocate a large object.

Replication Algorithms

The replication algorithm is created to solve the efficiency problem of the tag-cleanup algorithm. How does it solve the problem of efficiency?

The replication algorithm divides the memory into two two blocks of equal size, one at a time. When this piece of memory is used up, the surviving object is copied to the other piece, and then the used piece of memory is cleared out. So every time the entire half of the garbage collection, memory allocation will not need to worry about the memory fragmentation problem. The replication algorithm is easy to implement and efficient to run. The price is that the memory shrinks to half the original.

The replication algorithm is mainly used to recover the new generation, IBM experts have studied, the new generation of object 98% is "to die", do not need to divide the memory space according to the ratio of 1:1, but the memory is divided into a larger Eden space and two smaller survivor space, Use only Eden and one piece of survivor space at a time. When garbage collection is done, the objects that are still alive in Eden and survivor are copied to another survivor space, and finally the Eden and the used survivor space are cleared away. The ratio between Eden Space and the two-block survivor space is 8:1:1, and the Cenozoic free space is 90% (80% + 10%), and only 10% of the memory space is wasted. When there are more than 10% surviving objects, survivor space is not enough, and these objects will enter the old age directly through the allocation of security mechanisms.

Marker Grooming algorithm

The old age was garbage collected through the tagging-sorting algorithm. The tagging process, like the tag-purge algorithm, does not clean the recyclable object directly, but instead allows all objects to move toward one end, and then cleans out the memory outside the boundary directly.

Generational collection Algorithms

The garbage collection of modern commercial virtual machines uses the "generational collection" algorithm, which divides the Java heap memory into the new generation and the old age according to the different object lifetime. When a large number of objects die during each generation of garbage collection, and only a small number of objects survive, the replication algorithm is used. In the old age, with high survival rates and no additional memory space to vouch for, use the tag cleanup algorithm or the tagging algorithm.

Garbage collector

Not finished ...

Resources

"In-depth understanding of Java virtual machines"

Deep understanding of Java Virtual Machines-garbage collection mechanism (GC)

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.