Fourth. Garbage collection mechanism in HotSpot JVM

Source: Internet
Author: User

There are many well-known JVMs, but what we use most is Oracle's acquisition of Sun's hotspot.

The memory in the hotspot is divided into 3 generations: the younger generation (young generation), the older generation (old generation), and the persistent generation (permanent generation). The object was originally in the younger generation, and the elderly agent deposited objects that were still live after several generations of young generation, and some large objects that could be directly assigned to older generations. Persistent generations are objects that the JVM thinks can help simplify garbage collection management, such as descriptive information about classes and methods.


Young generation of memory models: an Eden Area (Eden), plus two small generation survivors (survivor space)

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7D/80/wKiom1bphpKjQgYqAABr_eWR7GU051.png "title=" screen shot 2016-03-16 pm 10.13.02.png "alt=" Wkiom1bphpkjqgyqaabr_ewr7gu051.png "/>

Most of the objects are directly allocated in the Eden area (some large objects may be allocated directly to the old generation), and after the last garbage collection the surviving objects are saved in the Survivor area, and they may die before they are promoted to old age. This object is saved in the From zone.

A collector may have multiple algorithms, some for younger generations, some for older generations. But in addition to the CMS collector, other generation collectors will use the old generation collection algorithm in younger generations, whenever the old generation is occupied by the generation too slow. The CMS collector does not have this reason because the CMS algorithm cannot be used on younger generations.


Fast distribution: In many cases there is a large contiguous block of memory for allocation. It is possible to allocate this memory with a simple point technique called a free pointer. The technique is to use a pointer to hold each allocation trace, and when there is an allocation request again, check whether the new object can be dropped, and if it can initialize the object with the update pointer.



Here are a few collectors:


Serial collection (Serial Collector):

Both the young generation and the old generation are serial generations, using only one CPU, and this collector uses a mode that stops everything.


The serial collector's young generation work steps:

The surviving objects in the Eden area are copied to the to-survivor area except for the too-large objects that are not fit to be placed, and those objects that are too large are placed directly into the old generation. Another from the survivors of the younger generation also put into the to survivors, the old age is copied to the old generation. If the to area is full, then the surviving objects in the place, full, then Eden and from, will not be promoted directly to the old generation. There is no need for any checks after the copy is complete, as it is not a viable object to remain in the Eden and from areas. (There is no algorithm to make the check or mark)

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7D/80/wKiom1bphs7AsQWyAACS2UcHKmc361.png "title=" screen shot 2016-03-16 pm 11.01.33.png "alt=" Wkiom1bphs7asqwyaacs2uchkmc361.png "/>

After the collection of the young generation, the Eden District and the from district are empty, only to the area of the sea with objects, then two biochemical district exchange roles.


the old generation work steps of the serial collector :

Use the tag-sweep-compress algorithm. The tag is: the tag collector of the sea oil which objects are connected to the root node, is alive. Sweep is: Sweep the entire generation to identify rubbish. Compression is: Moving the compression, moving the surviving object to the front end of the generation, leaving a whole block of contiguous space at the end, and then Sihong the free pointer to mark it.


When do you use this serial collector?

Are generally in the client environment (non-server-level dynamic), on short pauses can endure in the program. The serial collector can now effectively manage applications that have 64M of heap memory.

The serial collector can use the-XX:USESERIALGC directive to indicate that I want to use a serial collector.



Parallel collector (Parallel Collector):

These are mostly multicore machines, so the collector with multicore advantage is designed so that many CPUs are not idle.


The young generation of the parallel collector:

Unlike serial, this is a parallel version collected by the serial young generation and is still the replication algorithm for Stop the world. But with multiple CPUs, it is fast and increases throughput.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7D/7E/wKioL1bph4HAMRdgAABd0Wm4R5o143.png "title=" screen shot 2016-03-16 pm 11.17.43.png "alt=" Wkiol1bph4hamrdgaabd0wm4r5o143.png "/>


The old generation of the parallel collector is the same as the serial one.



Parallel compression collector (Paralle compacting Collector):

It uses a new algorithm for collecting in parallel in different places on the old generation.


old generation collection of parallel compression :

Using parallel parallel compression algorithm and stop the world mode. This algorithm can be divided into three phases:

The tagging phase , in which the code can directly reach objects in the initial application to the garbage collection thread, and then begins to tag all those objects that are directly reachable (alive), and if an object is alive, the size and location of the object is updated in its region data.


Abstract Stage , acting on the area, not on the object: usually after a few periods of short compression after each generation of the left is very dense, stored objects are mostly alive, so compressing them is not worth it. So the first thing the collector does at this stage is to check the density of the area, and when the density reaches a certain value, it's garbage collected (compressed) from the point and right to the area. At this stage, the position of the first byte of the surviving object in each compressed region is computed.


In the compression phase, the garbage collection thread is going to use the data again to determine which areas of the fit are to be filled, and then each thread's opposite copy data into the corresponding region. And finally produce a very dense at one end in a large free space


PS: I understand that the tagging phase and the summary phase are really preparing for the compression phase, which is to find a approximate location, the summary phase is to determine what needs to be compressed, and finally compress in the compression phase.


When to use parallel compression:

The parallel compression collector uses a parallel compression algorithm on the parallel collector at the age of generation, which allows it to have better performance on multiple CPU machines, such as the ability to reduce pause times. This collector is not suitable for running programs on a large shared machine, because a program on these machines cannot monopolize multiple CPUs for too long, and on these machines you should consider reducing the garbage collection thread (using command-line arguments-xx:parallelgcthreads=n) or selecting a different collector. If you want to use this collector, you can use the command line arguments:

-xx:useparalleoldgc





It's too late to write today, CMS collector (concurrent tag-sweep collector) Write it tomorrow (this is the collector that can make the response speed change quickly)


This article is from the "11315216" blog, please be sure to keep this source http://11325216.blog.51cto.com/11315216/1751977

Fourth. Garbage collection mechanism in HotSpot JVM

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.