Java garbage collection mechanism

Source: Internet
Author: User
Tags compact

Java garbage collection mechanism: The JVM dynamically reclaims memory that is occupied by objects without any references in an idle time, in an unscheduled manner.

Garbage collection can also clear the memory fragmentation in addition to releasing useless objects. Garbage collection mechanisms are: Generational replication garbage collection and standard garbage collection, incremental garbage collection.

After determining which garbage can be recycled, what the garbage collector has to do is start a garbage collection, but there is one problem: how to effectively recycle. Because the Java Virtual Machine specification does not explicitly stipulate how to implement a garbage collector, each vendor's virtual machine can implement a garbage collector in different ways, so it is only a discussion of the core ideas of several common garbage collection algorithms.

1.mark-sweep (Mark-Clear) algorithm

This is the most basic garbage collection algorithm, the reason is that it is the most basic because it is the easiest to achieve, the idea is the simplest. The tag-purge algorithm is divided into two stages: the tagging phase and the purge phase. The task of the tagging phase is to mark out all objects that need to be recycled, and the purge phase is to reclaim the space occupied by the tagged objects. The exact process is as follows:

It is easy to see that the tag-purge algorithm is easier to implement, but one of the more serious problems is that it is prone to memory fragmentation, and too many fragments can cause the subsequent process to allocate space for large objects without finding enough space to trigger a new garbage collection action ahead of time.

2.Copying (copy) algorithm

In order to solve the defect of mark-sweep algorithm, the copying algorithm is proposed. It divides the available memory by capacity into two blocks of equal size, using only one piece at a time. When this piece of memory is used up, copy the surviving object to another piece, and then clean up the used memory space once, so the memory fragmentation problem is not easy. The exact process is as follows:

This algorithm is simple, efficient, and not prone to memory fragmentation, but it has a high cost of using memory space because it can use less memory than half the original.

Obviously, the efficiency of the copying algorithm is very much related to the number of surviving objects, if there are many surviving objects, then the efficiency of the copying algorithm will be greatly reduced.

3.mark-compact (marker-collation) algorithm

In order to solve the defect of copying algorithm and make full use of memory space, the mark-compact algorithm is proposed. The algorithm marks the same stage as Mark-sweep, but after the token is completed, it does not clean the recyclable object directly, but instead moves the surviving object to one end and then cleans up memory outside the end boundary. The exact process is as follows:

  

4.Generational Collection (generational collection) algorithm

The generational collection algorithm is the algorithm used by most of the JVM's garbage collectors today. Its core idea is to divide the memory into several different regions based on the life cycle of the object's survival. In general, the heap zoning is divided into the old age (tenured Generation) and the New Generation (young Generation), the characteristics of the old age is that each garbage collection only a small number of objects need to be recycled, and the new generation is characterized by a large number of objects to be recycled each time the garbage collected, Then we can take the most suitable collection algorithm according to the characteristics of different generations.

At present, most of the garbage collectors take the copying algorithm for the new generation, because each garbage collection in the Cenozoic has to reclaim most of the objects, that is, the number of operations that need to replicate is less, but the actual is not in accordance with the ratio of 1:1 to divide the new generation of space, In general, the Cenozoic is divided into a larger Eden space and two smaller survivor space, each time using Eden space and one of the survivor space, when recycling, Copy objects that are still alive in Eden and survivor to another survivor space, and then clean up Eden and the survivor space you just used.

Because of the characteristics of the old age is that each recycling only a small number of objects, the general use of the mark-compact algorithm.

Note that there is another generation outside the heap that is the permanent generation (permanet Generation), which is used to store class classes, constants, method descriptions, and so on. The recovery of the permanent generation mainly recycles two parts: obsolete constants and useless classes.

Three. Typical garbage collector

Garbage collection algorithm is the theoretical basis of memory recovery, and garbage collector is the concrete implementation of memory recovery. Here's a look at some of the garbage collectors available from the hotspot (JDK 7) virtual machine, which allows users to assemble the collectors used in each generation according to their needs.

1.serial/serial old

The serial/serial old collector is the most basic and oldest collector, which is a single-threaded collector and must suspend all user threads when it is garbage collected. Serial Collector is for the new generation of collectors, the use of the copying algorithm, Serial old collector is a collector for the older era, using the mark-compact algorithm. Its advantages are simple and efficient, but the disadvantage is that it will bring a pause to the user.

2.ParNew

The Parnew Collector is a multithreaded version of the serial collector that uses multiple threads for garbage collection.

3.Parallel Scavenge

The Parallel scavenge collector is a new generation of multi-threaded collectors (parallel collectors) that do not need to pause other user threads during recycling, using the copying algorithm, which differs from the first two collectors, primarily to achieve a manageable throughput.

4.Parallel old

Parallel old is the older version of the Parallel scavenge collector (parallel collector), using multithreading and Mark-compact algorithms.

5.CMS

The CMS (current Mark Sweep) collector is a collector that targets the shortest payback time and is a concurrent collector, using the mark-sweep algorithm.

6.g1

The G1 collector is the forefront of today's collector technology, a collector for service-side applications that leverages multi-CPU, multicore environments. So it is a parallel and concurrent collector, and it can build a predictable pause-time model.

Let's add something about memory allocation:

  

The memory allocation of the object, which is allocated on the heap in the general direction, is mainly allocated to the new generation of Eden space and from space, and in rare cases it is directly allocated in the old age. If there is not enough space in the new generation of Eden space and from space, a GC is initiated, and if a GC is made, Eden space and from space can hold the object in Eden space and from space. During GC, the surviving objects in Eden Space and from space are moved to space, and then the Eden space and from space are cleaned. If the to space is not sufficient to store an object during the cleanup process, the object is moved to the old age. After the GC is performed, Eden Space and to space are used, and the next GC will replicate the surviving object to the from Space, so that it loops back and forth. When an object escapes a GC in the Survivor area, its object age increases by 1, and by default, if the object reaches 15 years old, it will move to the old age.

In general, large objects are allocated directly to the old age, so-called large objects are objects that require a large amount of contiguous storage space, and the most common large objects are large arrays, such as:

byte[] data = new byte[4*1024*1024]

This typically allocates storage space directly in the old age.

Of course, the rules for allocation are not completely fixed, depending on which garbage collector combination is currently being used and the parameters of the JVM.

Content Source: http://www.cnblogs.com/ywl925/p/3925637.html

Java 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.