[Java] Understanding JVM Four: garbage collection mechanism

Source: Internet
Author: User
Tags high cpu usage

Will all areas in the JVM's memory be recycled?

First we know that the Java stack and the local method stack after the completion of the method execution of the corresponding stack frame immediately out of the stack destroyed, the recovery of the two can be considered as the 100%;java heap object is not referenced, that is, the use of the completion will be recycled; The data in the method area is not generally recycled. Only at the same time: All instances are recycled, loading the class loader is recycled, class objects can not be accessed through any means (including reflection) will be recycled, and the program counter is mainly to record instruction execution information, in the Hostspot virtual machine will not be recycled;

The garbage collection mechanism in the heap is as follows:

First, when to trigger the collection of objects

    • When an object is not referenced
    • An uncaught exception occurred at the scope
    • The program executes properly at scope
    • The program executed the System.exit ()
    • The program was terminated unexpectedly (the process of being killed)

Second, the detection of garbage

1. Reference counting method (before JDK1.2)

When this class is loaded into memory, it generates a series of information, such as the method area, stack, program counter, and so on, when the object is created, the object is allocated in the stack space, a reference counter is generated, and the counter +1 is referenced, and when there is a new reference, the reference counter continues + 1, and when one of the references is destroyed, the reference counter-1, when the reference counter is reduced to zero, indicates that the object has no references, can be recycled. However, if there are two objects referencing each other, this method cannot be detected.

2. Root Search method

Consider all reference relationships as a graph, starting with a GC Root node, and not the nodes that are referenced when the search is complete, that is, useless nodes that mark them as garbage.

The object that can be used as the GC Root node is

    • Objects referenced in the Java stack
    • Objects referenced by static properties in the method area
    • Objects referenced by constants in the method area
    • Objects referenced in the local method stack

There are four types of reference in Java for references

    • Strong reference: The object that is directly new is a strong reference and is the most common reference used. If an object has a strong reference, the garbage collector will never recycle it. When there is not enough memory space, the Java virtual Machine prefers to throw a outofmemoryerror error, which causes the program to terminate abnormally, and does not rely on random recycling of strongly referenced objects to resolve out-of-memory issues.

    • Soft references: Packaged by class SoftReference. When there is not enough memory in the JVM, the garbage collector frees the objects that are only pointed to by soft references and throws a outofmemory error if the memory is not enough. Use soft reference objects to determine if they are still alive. Soft references are ideal for creating caches and releasing them when memory is low.

    • Weak references: Wrapping through class weakreference. If all references to an object are weak references, the object is recycled. The weak reference solves the coupling relationship of the object on the surviving relationship. Weak references are most common in collections, especially in hash tables, where the interface of a hash table allows any object to be used as a key, and when a key value is placed in a hash table, the hash tables have references to these key and value objects. If it is a strong reference, the key and value objects will not be recycled as long as the hash table itself survives, and the weak reference is meant to solve the problem. Weakhashmap is available in Java to meet this requirement.

    • Virtual reference: Wrapped by the class phantomreference, mainly used to detect whether the object has been cleared.

third, garbage collection

Prior to JDK1.2, the garbage was marked by reference counting, followed by a root search method to mark the garbage. There are three ways to recycle trash after the spam tag:

    • Mark-Clear
    • Copy
    • Mark-Organize

1. Mark-Clear

The mark-and-sweep algorithm is to reclaim the space it occupies when the garbage is marked. This approach does not require the movement of objects and is highly efficient in the case of many surviving objects, but it can cause memory fragmentation.

2. Copy

The replication algorithm copies the surviving objects into a new space during the search for garbage, and then cleans up the garbage. This approach is highly efficient in the case of fewer surviving objects, but requires a piece of memory space for object movement.

3. Marking-Finishing

The mark-and-sweep algorithm cleans up objects just like the tag-purge algorithm, but on the basis of the tag-purge algorithm, you move all the surviving objects to the same area and update the corresponding references. This is a costly approach, but it solves the problem of memory fragmentation.

Iv. garbage collection strategies for generation of reactors

Because the life cycle of different objects is not the same, different recycling algorithms can be adopted according to the life cycle of the objects in order to improve the efficiency of recycling.

1. Young generation

In the young generation's memory, the 8:1:1 is divided into one Eden area and two Survivor districts. Most of the objects are generated in Eden, and when the Eden Zone is full, the Minor GC is triggered (not necessarily when the Eden area is full), and the Eden and non-idle Survivor-Zone surviving objects are copied to another free Survivor area. The young generation minor GC replicates the surviving objects between the two Survivor zones until a Survivor area is full and copies the surviving objects into the old age.

2. Old generation

The life cycle of older generations of objects is longer, because the objects survive many times after the young generation has undergone multiple recoveries. The memory ratio of the older generation to the younger generation is about 1:2, and the old generation is full, triggering the Major GC, which is fully GC, for garbage collection of the entire heap, including the new generation, the old generation, and the durable generation.

3. Persistent generation

The method area, which holds constant, bytecode file information, is relatively stable, because objects are not created frequently. If the full GC is triggered after a persistent generation.

v. Types of GC

In the younger generation the GC is called the Minor GC, the GC in the old generation is called the Major GC, plus the full GC refers to the entire heap being recycled (including Cenozoic, old generation, and persistent generations).

Conditions that lead to Minor GC:

Conditions that lead to Major GC:

Conditions that result in full GC: The old age is filled, the persistent generation is full, the call System.GC (), the last GC after the heap's various domain allocation policy dynamic changes

The above Minor GC, Major GC, and full GC My understanding is that this is just a call to represent the area of GC action. There are several types of GC, as follows:

    • Serial GC: Young Generation single-threaded collector, using a copy algorithm, used extensively before JDK1.3. When garbage collection occurs, all user threads need to be interrupted.

    • Serialold GC: The older generation of single-threaded collectors, using the tag-collation algorithm, is the old-age version of the Serial GC.

    • Parnew GC: Young generation multi-threaded collector, using a copy algorithm. Can be understood as a multithreaded version of the Serial GC. Efficiency is much lower than Serial GC in a single CPU environment.

    • Parallelscavenge GC: Parallel collector, using replication algorithms, pursues high throughput.

    • Parallelold GC: The parallel collector, using the replication algorithm, is the Parallelscavenge GC older generation version.

    • CMS GC (Concurrent Mark Sweep GC): High concurrency, high CPU usage, tag-cleanup algorithm.

    • Garbagefirst GC (G1 GC)

[Java] Understanding JVM Four: 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.