"Java noodles question" 49 The advantages and principles of garbage collection. and consider 2 kinds of recycling mechanisms.

Source: Internet
Author: User

1. The most notable feature of the Java language is the introduction of a garbage collection mechanism, which allows Java programmers to write programs without taking into account memory management issues.
2, because of this garbage collection mechanism, the object in Java no longer has the concept of "scope", only the referenced objects have "scope".
3, the garbage collection mechanism effectively prevents the memory leakage , can effectively use the memory that can be used .
4. The garbage collector is typically run as a separate low-level thread, and in unpredictable cases it cleans up and reclaims objects that have been dead or have not been used in the heap for a long time.
5. The programmer cannot call the garbage collector in real time on an object or all objects for garbage collection.

The garbage collection mechanism has generational replication garbage collection , token garbage collection , and incremental garbage collection .

First, generational replication garbage collection

The life cycle of different objects is not the same. Therefore, different life cycle objects can be collected in different ways to improve the efficiency of recycling. In the course of running a Java program, a large number of objects are generated, some of which are related to business information, such as those in HTTP requests.

Session object, thread, socket connection, such objects are directly linked to the business, so the life cycle is longer. However, there are some objects, mainly the temporary variables generated during the run of the program, which are shorter life cycles, such as: string objects, because of their invariant class characteristics, the system will produce a large number of these objects, some objects even once can be recycled. If each garbage collection is a collection of the entire heap space, takes a relatively long time, and the lifetime of the object is still there, so the introduction of generational recycling, the different life cycle of objects in different generations, different generations of the most suitable for its garbage collection method for recycling.

The total of the virtual machines is divided into three generations: young Generation, Old Generation, and persistent (Permanent Generation). Because objects are processed in a generational way, garbage collection areas and times are different. There are two types of GC: Scavenge GC and full GC.

Scavenge GC: Typically, when a new object is generated and the Eden application space fails, the scavenge GC is triggered, GC is performed on the Eden Zone, the non-surviving objects are cleared, and the surviving objects are moved to the survivor area. Then tidy up the two districts of survivor. This method of GC is carried out on the young generation of the Eden area and does not affect the old generation. Because most objects start in the Eden area, and the Eden area is not very large, GC in the Eden area is frequent. Thus, it is generally necessary to use fast and efficient algorithms, so that Eden can be free as soon as possible.

Full GC: Organizes the entire heap, including young, tenured, and perm. Full GC is slower than SCAVENGEGC because it needs to be recycled for the entire pair, so you should minimize the total GC number of times. In the process of tuning the JVM, a large part of the work is to adjust the FULLGC. The full GC may be caused by the following reasons:

· The old generation (tenured) was written full

· Persistent generation (Perm) is full

· System.GC () is displayed call

Dynamic changes in the domain allocation policy of the heap after the last GC

Second, Mark Garbage collection

It is the first garbage collection algorithm that can reclaim a data structure that is being recycled. There are still many commonly used garbage collection techniques that use a variety of markers to clear the variant of the algorithm.

Unreferenced objects are not reclaimed immediately when using the tag cleanup algorithm. Instead, the garbage object will accumulate until the memory runs out. When memory runs out, the program is suspended and garbage collection starts. The program will continue to execute when all unreferenced objects have been cleaned up.

The markup cleanup algorithm consists of two phases:

① marks the stage, marking all accessible objects.

② collection phase, the garbage collection algorithm scans the heap and reclaims all unmarked objects.

The advantage of marking garbage collection: Tag-clear garbage collection tracks all objects accessed by root (root), so it can correctly flag and perform garbage collection work even when there is a circular reference. In addition, general operations that reference objects do not incur any additional overhead.

Disadvantage: When the garbage collection algorithm executes, the normal program is suspended. In particular, this becomes a problem if a program is an interactive program or there are some real-time operations in progress. For example, an interactive program that is in the process of garbage collection will not respond to the cycle.

Third, incremental garbage collection

This kind of garbage collection mechanism is always unable to understand thoroughly, can only be explained briefly here, specifically how to define also please Google yourself.

Simply put, it exists in order to solve the problem of long pauses of Mark clearing. Incremental reclamation is performed by dividing the GC into several parts. Set the conditional limit of "GC up to interrupt 10ms" to make the GC's terminal time as predictable. However, the reference relationship may have changed between two sections of the GC program. Therefore, this GC algorithm also writes the barrier to record the change of the reference relationship. Although this method controls the maximum interrupt time, the total GC time is increased due to the increased number of interrupts.

With regard to incremental garbage collection, http://blog.csdn.NET/u011320646/article/details/38640703 this blog post is more vivid and needs to be read by itself.

"Java noodles question" 49 The advantages and principles of garbage collection. and consider 2 kinds of recycling mechanisms.

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.