Java Performance optimization JVM GC (garbage collection Mechanism)

Source: Internet
Author: User

Java performance optimization, sorting out an article for later restudying.

JVM GC (garbage collection Mechanism)

Before we learn the Java GC, we need to remember one word:stop-the-world. It will occur in any GC algorithm . Stop-the-world means that the JVM stops executing the application because it needs to perform a GC. When stop-the-world occurs, all threads go into a wait state, except for the thread required by the GC, until the GC task completes. GC optimization is a lot of time to reduce the occurrence of stop-the-world.



What area of garbage is reclaimed by the JVM GC?

It is important to note that the JVM GC reclaims only the objects in the heap and method areas . The data in the stack area is automatically freed by the JVM when it goes out of scope, so it is not within the scope of the JVM GC's management.

How does the JVM GC determine that objects can be recycled?

· Object does not have a reference

· No catch exception occurred at scope

· The program executes properly at scope

· The program executed the System.exit ()

· Unexpected termination of program (killed thread, etc.)

There is no way to explicitly allocate and unregister the cache in a Java program, because these things the JVM has done for us, that is, the GC.

Sometimes we can set the related object to null to try to display the clear cache, but not set to NULL will be marked as recyclable, it is possible to escape.

Setting the object to null is at least harmless, but using System.GC () is not available, and using System.GC () is not a GC operation immediately, but it waits for a period of time, not even execution, and System.GC (), if executed, triggers the full GC, which greatly affects performance.

When does the JVM GC execute?

The Eden Zone does not have enough space to hold the new object, execute the Minro GC. The object that rises to the old age is larger than the remaining space of the old age when the full GC is executed, or when it is less than the handlepromotionfailure parameter forces the full GC. Tuning is primarily to reduce the number of triggers for full GC, which can be controlled by Newratio to control the percentage of the age of the new generation, through maxtenuringthreshold setting objects into the old ages threshold (described later).

Garbage collection mechanism by generation

New generation (young Generation): The vast majority of newly created objects are assigned here, and since most of them become inaccessible soon after they are created, many objects are created in the Cenozoic and then "disappear." The process by which an object "disappears" from this area is called:Minor GC .

old generation: objects have not become unreachable and survived from the Cenozoic cycle and are copied here. Its region allocates more space than the Cenozoic. Because of its relatively large space, the number of GCs occurring in the old years is much less than in the Cenozoic. The process of disappearing an object from the old age, which is called:Major gc or full GC.

The persistent generation (Permanent generation) is also known as the method area: Used to hold class constants and string constants. Note that this area is not used to store objects that survived from the old age, and this area can also occur in GC. GC events that occur in this region are also counted as Major GC. Only the conditions in which GC is occurring in this area are very stringent and must meet the following three conditions to be recycled:

1. All instances are recycled

2. ClassLoader to load the class is recycled

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

Maybe we'll have a question:

What happens if the object of the old age needs to refer to a new generation of objects?

In order to solve this problem, there is a card table in the old age, it is a block of 512byte size. References to new generations of objects in all old eras will be recorded in this table. When performing GC for the new generation, it is only necessary to query the card table to determine whether it can be recycled without querying the entire old age. This card table is managed by a write barrier . Write barrier has brought a lot of performance gains to the GC, although it may cost some, but it's worth it entirely.

The default new generation (young generation), older generation (old generation) occupies a space ratio of 1:2.

The formation and logic of Cenozoic space

To better understand the GC, let's learn the composition of the new generation, which is used to preserve the objects that were created for the first time, and it is divided into three spaces:

· An Eden Space (Eden)

· Two survivor space (Fron Survivor, to Survivor)

Allocation of default Cenozoic space: Eden:Fron:To = 8:1: 1

The order of execution for each space is as follows:

1. Most of the objects that have just been created will be stored in the Eden Space (Eden).

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

3. Thereafter, each time a GC is performed in Eden Space, the surviving objects are stacked in the same survivor space .

4. When a survivor is saturated with space, the surviving object is moved to another survivor's space. It then empties which survivor space is already saturated.

5, repeat in the above steps n times (n = maxtenuringthreshold (age threshold setting, default 15)) still live objects, will be moved to the old age.

From the above steps can be found that two survivor space, must have one is kept empty . If two two survivor spaces have data, or two spaces are empty, there must be some kind of error in your system.

The important point to remember is that after the object has just been created, it is stored in the Eden space. Those long-lived objects are transferred to the old generation through Survivor Space (Survivor).

There are exceptions, for some larger objects (which need to allocate a larger contiguous memory space) go straight to the old age. Generally occurs in the case of survivor space shortage.

The composition and logic of the old age space

The composition of the old age space is actually very simple, it does not like the new generation of space as divided into several areas, it has only one region, the objects stored inside is not like the vast majority of the new generation of space is justified man road, Xi died. almost all of the objects here are from Survivor in the space, they will never be easy dog belt . As a result, the full GC (Major GC) does not occur as frequently as minor GC, and the time to do Major GC is longer (about 10 times times) than minor GC.

JVM GC Algorithm Explained

1. Root Search algorithm

The root search algorithm is introduced from graph theory in discrete mathematics, and the program considers all referential relationships as a graph, starting from a GC ROOT of a node, looking for the corresponding reference node, and after finding the node, continue to look for the node's reference node. When all the reference nodes are searched, the remaining nodes are considered nodes that are not referenced, that is, useless nodes.

Red is a useless node and can be recycled.

Currently, the objects in Java that can be used as GC root are:

1. Objects referenced in the virtual machine stack (local variable table)

2. Objects referenced by static properties in the method area

3. Objects that are normally referenced in the method area

4. Objects referenced in the local method stack (native objects)

Basic all GC algorithms refer to the concept of root search algorithm.

2. Mark-Sweep algorithm

The tag-purge algorithm takes a scan from the root collection, marks the surviving object , marks it, and then scans the entire space for non-tagged objects for direct recycling, such as.

The tag-purge algorithm does not need to move objects, and it only handles objects that are not alive, and is extremely efficient when there are many surviving objects, but because the tag-purge algorithm directly reclaims the objects that are not alive and does not defragment the surviving objects, it can cause memory fragmentation.

3. Copy algorithm

The replication algorithm divides memory into two intervals, and when this algorithm is used, all dynamically allocated objects can only be allocated in one interval (the active interval), while the other interval (space interval) is idle.

The replication algorithm uses a scan from the root set to copy the surviving objects to the idle interval, and when the active interval is scanned, the active interval is recycled all at once. at this point the original idle interval becomes the active interval . The next time the GC is repeated, it will repeat the operation as a loop.

The replication algorithm is extremely efficient when there are fewer surviving objects, but the cost is half the memory space used for object movement. so the usage scenario of the replication algorithm must be that the survival rate of the object is very low , and most importantly, we need to overcome the waste of 50% of memory.

4. Marking-Sorting algorithm

The mark-and-organize algorithm uses the tag-sweep algorithm to mark and purge objects in the same way, but after reclaiming the space occupied by the objects that are not alive, all the surviving objects are moved to the left free space and the corresponding pointers are updated. The tag-collation algorithm, which is more expensive but solves the problem of memory fragmentation, is based on the tag-purge algorithm and the object's moving sort.

In order to optimize the recovery of memory, the JVM uses the method of generational recovery, which mainly uses the replication algorithm for the recovery of Cenozoic memory (Minor GC). For the old age of recycling (Major GC), most of the use of marker-collation algorithm .

Introduction to the garbage collector

It is important to note that every collector has the stop the world problem , but each collector in the Stop the World Time optimization, the algorithm is different, according to their own needs to choose the appropriate collector.

1, Serial (-XX:+USESERIALGC)

From the name we can see that this is a serial collector.

The serial collector is the most basic, oldest collector in a Java virtual machine. Before JDK1.3 is the only option for the new generation of Java Virtual machine collectors . Currently also CLIENTVM under SERVERVM 4 core 4GB below machine default garbage collector. The serial collector is not collected using only one CPU, but when the JVM needs garbage collection, it pauses all user threads until the recycle is complete.

Using algorithms: replication Algorithms

The JVM Chinese name is a Java virtual machine, so it works like a virtual computer, and each of these threads is considered a processor of the JVM, so the CPU0, CPU1 in the diagram are actually user threads, not real machine CPUs, and don't misunderstand.

Although the serial collector is the oldest, it is the most efficient in this case compared to other collectors, because it does not have the overhead of thread interaction and is concentrating on garbage collection for a single CPU-constrained environment.

2, Serialold (-XX:+USESERIALGC)

Serialold is the old-age collector version of the serial collector , which is also a single-threaded collector, which is currently used primarily in client mode. In the case of server mode, it has two main purposes: one is to use the parallel scavenge collector in JDK1.5 and previous versions, and the other is as a backup plan for the CMS collector, if the CMS appears concurrent mode Failure, the serialold will be used as the fallback collector.

Using algorithms: tagging-sorting algorithms

Run with consistency.

3, Parnew (-XX:+USEPARNEWGC)

Parnew is actually a multithreaded version of the serial collector. In addition to the serial collector, only it can work with the CMS collector.

Using algorithms: replication Algorithms

Parnew is the preferred Cenozoic collector for many JVMs running in server mode. However, in the case of single CPU, it is far less efficient than the serial collector, so be sure to pay attention to the use of the scene.

4, Parallelscavenge (-XX:+USEPARALLELGC)

Parallelscavenge, also known as the throughput priority collector, is a new generation collector similar to the Parnew collector.

Using algorithms: replication Algorithms

The goal of the Parallelscavenge collector is to achieve a control throughput, so-called throughput is the ratio of the CPU's time spent running user code to the total CPU elapsed time, that is, throughput = Running user code time/(running user code time + garbage collection time). If the virtual machine runs for a total of 100 minutes and the garbage collection takes 1 minutes, the throughput is 99%.

5, Parallelold (-XX:+USEPARALLELOLDGC)

Parallelold is a parallel collector, and like Serialold,Parallelold is an old -age collector, a collector of older years ' throughput priorities. This collector is only available after JDK1.6, and before that, Parallelscavenge can only choose Serialold as the collector of its old age, which seriously drags the speed of parallelscavenge overall. With the advent of Parallelold, the "throughput first" collector is a veritable experience!

Using algorithms: tagging-sorting algorithms

The Parallelscavenge + Paralleloold collector is a priority in cases where the throughput and number of CPUs are greater than 1.

6. CMS (-XX:+USECONCMARKSWEEPGC)

CMS is an old age collector , full name Concurrent low Pause Collector, is a new GC collector that JDK1.4 later started referencing, and has been further improved in JDK1.5, 1.6. It is a collector that needs more than the throughput requirements for the importance of response time . Using a CMS is a good fit for a server that requires high response times.

A major feature of CMS is the long pause of two short pauses to replace serial or parallel tagging algorithms.

Using algorithms: tag-Clean

The implementation process for CMS is as follows:

· Initial tag (STW initial mark)

At this stage, the virtual machine is required to pause the application thread being executed, officially called STW (Stop Tow World). This process scans the object directly associated with the root object and makes the tag. The process will be completed very quickly.

· Concurrency token (Concurrent marking)

This phase is followed by the initial marking phase and continues to be traced down on the basis of the "initial tag". Note that this is a concurrency token, which means that the user thread can execute concurrently with the GC thread, and this phase does not halt the user's thread.

· Concurrent pre-Cleanup (Concurrent precleaning)

This phase is still concurrent , and the JVM looks for objects that are in the old age when the "concurrency tag" stage is being executed (this may be when objects are promoted from the new generation to the old age or to the old age). By Rescanning, the work of "re-tagging" at one stage is reduced, because the next stage is STW.

· Re-tagging (STW remark)

This phase pauses the application thread that is executing again, re-root the object to find and mark the objects that were missed in the concurrency phase (the update of the object state after the end of the concurrency tagging phase), and handle the object association. This time will take longer than the "initial tag", and this phase can be marked in parallel.

· Concurrent Cleanup (Concurrent sweeping)

This phase is concurrent , and the application thread and GC cleanup thread can execute concurrently.

· Concurrent Reset (Concurrent reset)

This phase is still concurrent , resetting the data structure of the CMS collector and waiting for the next garbage collection.

Disadvantages of the CMS:

1, memory fragmentation. memory fragmentation is generated in memory space due to the use of the tag-cleanup algorithm. However, the CMS collector did some small optimizations, that is, the unallocated space is summarized into a list, when the JVM needs to allocate memory space, it will search this list to find a qualifying space to store the object. However, the problem of memory fragmentation still exists, if an object requires 3 contiguous space to store, because of memory fragmentation, the search for this space will result in full GC.

2, need more CPU resources. because concurrent processing is used, in many cases, both GC threads and application threads execute concurrently, which requires more CPU resources and is a sacrifice of a certain throughput.

3, need a larger heap space. because the CMS tag phase of the application thread is still executing, there is a problem with the heap space to continue allocating, and some space must be reserved to ensure that the CMS has space allocated to the newly added object before the heap space is reclaimed. The CMS defaults to start garbage collection in the old age space using 68%. This threshold can be set by-xx:cmsinitiatingoccupancyfraction=n.

7, Garbagefirst (G1)

This is a new garbage collector, both can be recycled and can reclaim the old age, sunhotspot1.6u14 more than earlyaccess version of this collector, sun Company is expected to release the official version SunHotSpot1.7. Consolidate and optimize the CMS by re-partitioning the memory area, while focusing on throughput and response time. The cup is used by Oracle to acquire the collector and use it for the commercial charge collector. So for the time being, there is no company that uses it, so let's do it later.

Tidy up the collectors of the new generation and the old years.

New Generation Collectors:

Serial (-XX:+USESERIALGC)

Parnew (-XX:+USEPARNEWGC)

Parallelscavenge (-XX:+USEPARALLELGC)

G1 Collector

Old Age Collectors:

Serialold (-XX:+USESERIALOLDGC)

Parallelold (-XX:+USEPARALLELOLDGC)

CMS (-XX:+USECONCMARKSWEEPGC)

G1 Collector

Currently understand the GC collector is so much, if there is a friend who is destined to see this article, just have a better GC collector recommendation, welcome message exchange.

Transferred from: https://zhuanlan.zhihu.com/p/25539690

Reference article:

Become a JAVAGC expert part I--in layman's Java garbage collection mechanism

When was the JVM garbage collection triggered? Garbage collection algorithm? What are the garbage collector's?

Understanding the CMS (Concurrent mark-sweep) garbage collector

Java Performance optimization JVM GC (garbage collection Mechanism)

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.