Deep understanding of Java Virtual Machines (iv)-garbage collection algorithm __ Code

Source: Internet
Author: User
Tags garbage collection
Overview

The garbage collection of the current commercial virtual machines uses the "generational collection" (generational Collection) algorithm, which has no new idea but divides the memory into several pieces according to the different life cycle of the object. In general, the Java heap is divided into Cenozoic and old age, so that the most appropriate collection algorithm can be used according to the characteristics of each age. In the Cenozoic, each garbage collection has found a large number of objects to die, only a small number of survival, then choose the replication algorithm, only to pay a small number of living objects of the replication costs can be completed collection. In the old age, because of the high survival rate of the object, there is no extra space to allocate it to guarantee, you must use the "tag-clean" or "tag-collation" algorithm for recycling.

New generation (young Generation)

The Cenozoic is where all new objects are created. When the Eden area does not have enough space to allocate, the virtual machine initiates a minor GC. The young generation is divided into 3 parts: the Enden area and the survivor from and survivor to districts.
The main features of the younger generation: Most new objects are located in the Eden area. When the Eden area is filled with objects, the minor GC is executed. and transfer all surviving objects to one of the survivor areas. The Minor GC also examines the surviving objects and shifts them to another survivor area. In this way for some time, there will always be an empty survivor area. "Endure" multiple minor GC, the surviving objects will be transferred to the older generation of memory space. If the object is born in Eden and survives after the first minor GC, and can be accommodated by survivor, it will be moved into survivor space and the object age is set to 1. Objects in the survivor area each endure "once minor GC, age increases 1 years old, when its age increases to a certain extent (the default is 15 years old), will be promoted to the old age.

The Cenozoic size can be controlled by parameter-xmn10m, and the size of the Eden and survivor areas can be controlled by the parameter-xx:survivorratio, which defaults to 8:1. older Generation (old)

The older generation contains long lived objects and objects that survived after several minor of GC. Typically, garbage collection occurs when the old age memory is filled up. The old age of garbage collection is called Major Gc,major GC will take more time.

Minor GC and major GC distinction Cenozoic GC (Minor GC): Refers to the generation of garbage collection action, because Java objects mostly have the characteristics of the face to death, so Minor
GC is very frequent, the general recovery speed is also relatively fast. Old age GC (Major gc/full GC): Refers to the garbage collection work occurring in the old age, where a Major GC appears, often accompanied at least once minor
GC (but not absolute, there is a policy selection process directly for Major GC in the collection strategy of the parallel scavenge collector), Major
The GC speed is generally 10 times times slower than the minor GC. garbage Collection Algorithm mark-Purge Algorithm (mark-sweep)

Principle:
The tag-purge algorithm (mark-sweep) is divided into "mark" and "purge" phases: First mark all objects that need to be reclaimed, and then uniformly recycle all tagged objects after the tag completes.
Disadvantages:
1. Efficiency problems, the efficiency of marking and clearing two processes is not high;
2. Space problems, marking the removal of a large number of fragmented memory fragments, too much space debris may cause later programs to run the process of allocating large objects, unable to find enough contiguous memory and had to trigger another garbage collection action. Replication Algorithm

Principle:
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 run out, the surviving object is copied to another piece, and then the memory that has been used is cleaned up once. This makes each of the entire half of the memory collection, memory allocation is not to consider the memory fragmentation and other complex situations, as long as the move heap top pointer, in order to allocate memory, to achieve simple, efficient operation.
Disadvantages:
Need to replicate, reduce efficiency, waste space.

Today's commercial virtual machines use this collection algorithm to reclaim the new generation. IBM's research has shown that 98% of the new generation of objects are "facing death", so there is no need to divide the memory space according to the ratio of 1:1, but to divide the memory into a larger Eden space and two smaller survivor, each using Eden and one of the survivor. When recycled, the surviving objects in Eden and survivor are copied to another piece of survivor space at once, and then the survivor space that Eden and just used are cleared away. Mark-Finishing Algorithm (mark-compact)

Principle:
The tag process is still the same as the tag-purge algorithm, but the next step is not to clean the recyclable object directly, but to have all the surviving objects move to one end and then clean out the memory directly from the end of the drop boundary. Generational collection algorithm (generational Collection)

The generational garbage collection strategy is based on the fact that the lifecycle of different objects is not the same. As a result, objects of different lifecycles can be collected in different ways to improve recovery efficiency. garbage collector

If the collection algorithm is the methodology of memory recycling, then the garbage collector is the specific implementation of memory recovery. The Java Virtual Machine specification has no provision for how the garbage collector should be implemented, as a result, different vendors, different versions of the virtual machine to provide the garbage collector can be very different, and generally provide parameters for users according to their own application characteristics and requirements of the use of the collector in various eras. The collector discussed here is based on the hotspot virtual machine after JDK1.7 Update 14 (a commercially available G1 collector is officially provided in this release, G1 is still in the test state), and the virtual machine contains all the collectors as shown in the following illustration:



The figure above shows 7 collectors that act on different generations, and if there is a connection between the two collectors, it means they can be used in combination. The area in which the collector is, indicates whether it belongs to the Cenozoic collector or the old age collector.

Before introducing the various features of these collectors, let's make a point: although we are comparing the various collectors, we are not trying to pick out the best collectors. Because so far there is no best collector to appear, there is no universal collector, so we chose only the most suitable for the specific application of the collector. Serial collector

It's a single thread collector, but its "single-threaded" meaning does not only mean that it uses only one CPU or a collection thread to complete the garbage collection, but, more importantly, that it must suspend all other worker threads until it is collected when it is garbage collected. parnew Collector

The Parnew collector is actually a multi-threaded version of the serial collector, which includes all the control parameters available to the serial collector, the collection algorithm, the Stop the world, the object allocation rules, and the rest of the garbage collection using more than one thread. The recycle policy is exactly the same as the serial collector. Parallel Scavenge collector

The Parallel scavenge collector is a new Era collector, which is also a collector with a replication algorithm and a parallel multithreaded collector.

The feature of the Parallel scavenge collector is that its focus is different from other collectors, and the focus of collectors such as CMS is to shorten the time that a user's thread pauses when garbage collection is minimized, and Parallel The goal of the scavenge collector is to achieve a controllable throughput (throughout). Throughput is the ratio of the CPU's running user code to the total time elapsed, i.e. throughput = Run user code time/(run user code time + garbage collection time). Serial Old collector

Serial old is the older version of the serial collector, which is also a single-threaded collector, using the "tag-collation" algorithm. Parallel Old collector

Parallel old is a vintage version of the Parallel scavenge collector. CMS collector

The CMS (Concurrent Mark Sweep) collector is a collector with the goal of obtaining the shortest recovery pause time. G1 Collector

G1 collector Memory allocation and recycling policy

Automated memory management, as advocated in the Java technology system, can ultimately be automated to solve two problems: allocating memory to objects and reclaiming memory allocated to objects.
The memory allocation of the object, in the general direction, it is allocated on the heap (but may also be JIT-compiled to be split into scalar types and indirectly distributed on the stack), the object is mainly distributed in the new generation of Eden area, if the local thread allocation buffer is started, will be assigned by thread priority on the Tlab.

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.