Java GC mechanism and related

Source: Internet
Author: User

Java GC mechanism and related

Garbage (Garbage) is the object to be recycled by the program. If an object is not directly or indirectly referenced, it becomes "Garbage 」, the memory occupied by it needs to be released in a timely manner, otherwise it will cause "Memory leakage 」. Some languages require programmers to manually release the memory (recycle garbage), and some languages have garbage collection mechanisms (GC). For example, I am learning the Java language and there is a garbage collection mechanism.

Before learning Java GC, remember the word stop-the-world. It occurs in any GC algorithm. Stop-the-world means that JVM stops application execution because GC is required. When stop-the-world occurs, all threads except the GC are waiting until the GC task is completed. GC optimization often reduces the occurrence of stop-the-world, shortens the GC thread interruption time, and ensures the program running efficiency.

First Mark clearing

Mark and Sweep is the earliest developed GC algorithm.

Principle: two scans: the first time from the root, marking the objects that may be referenced in a recursive manner, and the second time, the unlabeled objects are recycled, after successful collection, mark the marked reference object as the next GC collection preparation.

GC time: it depends on the number of surviving objects and the total number of program objects, because it is necessary to recursively check whether all objects can be recycled.

Thinking: if the number of surviving objects is small, there are very few actual tags in the tag phase, and the rest are all non-referenced objects, the efficiency of the tag phase will be reduced, this reduces the efficiency of the entire GC process.

The Mark clearing method saves memory, but the two scans require more time, which leads to a small proportion of garbage.

 

 

Type 2 Copy collection

Copy and Collection will Copy the referenced objects starting from the root to another space, and then discard the old space and use the new space.

Principle: copying and collecting objects only requires one scan. Prepare a new space and scan the object from the root. If a reference to this object exists, copy it to the new space 」. After a scan, all objects in the new space are all non-spam objects.

Thinking: Replication and collection are local. During the process of copying and collection, objects are copied to the new space in the order that objects are referenced. As a result, objects with close relationships are more likely to be placed in a memory space that is close to each other. This is called locality. In the case of high locality, the memory cache will operate more effectively and the program performance will be improved.

Replication and collection are faster, but an additional piece of memory needs to be opened for replication, which is dominant in the case of a large proportion of garbage.

 

Third reference count

The reference count refers to the reference count for each object that stores a reference count for this object. When the reference count for this object increases, the reference count increases accordingly. If the reference count of an object is zero, the object is recycled.

Principle: each object stores the reference count of this object. When the reference increases or decreases, the count is updated. The time point is as follows: variable assignment, object content update, function termination (local variables are no longer referenced ). When the reference count of an object is zero, it indicates it is no longer referenced, so the corresponding memory space is released.

  

Thinking: The biggest advantage of reference counting is its ease of implementation. The cost is small. Basically, when the reference count is 0, the garbage will be immediately recycled. However, it is difficult to predict the object lifecycle in other methods, and the garbage will exist for a longer time than this method. This garbage collection method has the shortest interruption time.

However, if the object contains a circular reference, it cannot be recycled. The reference count is not suitable for parallel use. Multiple Threads operate the reference count at the same time, which may lead to different numeric values and memory errors. Therefore, the reference count must be exclusive. If the reference operation is frequent, the overhead of the lock and other concurrency control mechanisms is quite large.

Java GC (garbage collection mechanism)

Jvm gc only recycles objects in the heap and method zones. The data in the stack area will be automatically released by JVM after the scope is exceeded, so it is not within the management scope of jvm gc.

Criteria for determining whether objects can be recycled:

  1. Object not referenced
  2. Uncaptured error in scope
  3. The program is successfully executed in the scope.
  4. The program executes System. exit ()
  5. Unexpected program termination (killed thread, etc)

In Java programs, you cannot explicitly allocate or deregister the cache, because these tasks are all done by JVM, that is, GC.

Sometimes we can set the related object to null to try to clear the cache. However, if it is not set to null, it will be marked as recycled and may escape.

Setting an object to null is at least harmless, but the System is used. gc () is not available. Use System. gc () does not immediately execute the GC operation, but waits for a period of time, or even does not execute, and the System. if gc () is executed, Full GC is triggered, which seriously affects the performance.

 

Java garbage collection is divided into three generations: the new generation, the old generation, and the persistent generation.

Young generation: the vast majority of newly created objects will be allocated here, because most of them become inaccessible soon after creation, many objects will be created in the new generation and then "disappear ". The process of object "disappearing" from this region is called: Minor GC.

Old generation: objects that survive from the new generation cycle and survive on the new generation battlefield are not easy to carry, hey. Will be copied here. The region has more space than the new generation. Because of its relatively large space, GC occurs in the old age much less than that in the new generation. The process in which the object disappears from the old age is called Major GC or Full GC.

Permanent generation is also called Method area: used to save class constants and string constants. Note that this region is not used to store objects that survive the old age, and GC may also occur in this region. GC events that occur in this region are also considered as Major GC. The GC condition in this region is very strict, and it must meet the following three conditions to be recycled:

1. All instances are recycled

2. ClassLoader loaded for this class is recycled.

3. Class objects cannot be accessed through any means (including reflection)

 

Structure of New Generation and old generation: composition and logic of New Generation Space

To better understand GC, we will learn about the composition of the new generation, which is used to save the objects created for the first time. It is divided into three spaces:

· An Eden Space)

· Two Survivor space (Fron Survivor vor, To Survivor VOR)

By default, the new generation space is allocated: Eden: Fron: To = 8: 1: 1.

The execution sequence of each space is as follows:

1. Most newly created objects are stored in Eden ).

2. After the first GC (Minor GC) is executed in the Eden space, the surviving objects are moved to one of the Survivor spaces (Minor VOR ).

3. After GC is executed in the Eden space, the surviving objects are stacked in the same survivor space.

4. When a survivor's space is saturated, the surviving object will be moved to another survivor's space. It then clears the saturated survivor space.

5. objects that remain alive after N times (N = MaxTenuringThreshold (age threshold, default value 15) are repeated in the preceding steps, and then moved to the old age.

From the above steps, we can find that one of the two survivor spaces must be left empty. If two survivor spaces both have data or both are empty, it must have been a mistake in your system.

We need to remember that objects are saved in the Eden space after being created ). Objects that survive for a long time will be transferred to the Old generation space through the Survivor space ).

There are also exceptions. For some relatively large objects (a large continuous memory space needs to be allocated), they enter the old age directly. It usually occurs when the volume vor space is insufficient.

Composition and logic of Old Age Space

The composition of the Old Age space is actually very simple. It is not as divided into several regions as the new generation space. It has only one region, and the objects stored in it are not like the vast majority of the new generation space. Almost all objects here come from the VOR space, and they will never be easy to carry. Therefore, Full GC (Major GC) occurs less frequently than Minor GC, and takes a longer time to perform a Major GC than Minor GC (about 10 times ).

 

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.