JVM GC Summary

Source: Internet
Author: User
Tags xms

A: A brief introduction to the Java Memory Area 1. Heap

The memory initially allocated by the JVM is specified by-XMS, which defaults to 1/64 of the physical memory.

The maximum allocated memory for the JVM is specified by-XMX, which defaults to 1/4 of the physical memory.

When the default free heap memory is less than 40%, the JVM increases the heap until the maximum limit of-xmx, which can be specified by the-xx:minheapfreeratio= parameter.

When the default free heap memory is less than 70%, the JVM reduces the heap until the minimum limit of-XMS, which can be specified by the-xx:maxheapfreeratio= parameter.

Note: The server generally sets-xms and-xmx equal to avoid resizing the heap frequently after each GC.

2. Sub-generational

Generational is a great highlight of Java garbage collector, according to the object's declaration period, the heap is divided into 3 generations, young,old and permanent, according to the characteristics of different generations using different collection algorithms, weaknesses are also.

Young (Nursery), younger generation. studies have shown that most of the objects are ephemeral, and are born with them. So all collectors choose a copy algorithm for the younger generation . The advantage of a replication algorithm is that it accesses only active objects, with the disadvantage of high replication costs. Because only a small number of objects in the young generation can survive the garbage collector, a small amount of replication costs are required. And the copy collector only accesses active objects, ignoring those who are the largest percentage of dead objects, and giving full play to the advantages of the low cost of traversing space.

2.1 Years of Light generation (young)

The young generation is divided into three districts, one is Eden area and two survivor District. Most objects are generated in the Eden area. When the Eden Zone is full, the surviving objects will be copied to the Survivor area (one of two), and when the survivor area is full, the surviving objects of this area will be copied to another survivor area, when the survivor area is full, Objects that are copied from the first survivor area and that are still alive will be copied to the old quarter (tenured). It is important to note that the two districts of survivor are symmetrical and have no succession, so the same area may have objects copied from Eden, and objects copied from the previous survivor, and copied to the old quarter only from the Survivor area. And there is always an empty survivor area.

2.2 tenured (old generation)

Older generations store objects that survive from younger generations. In general, older generations are storing objects of longer life cycles.

2.3Perm (Durable)

Used to store static files, such as Java classes, methods, and so on. The persistence generation has no significant impact on garbage collection, but some applications may dynamically generate or invoke some classes, such as Hibernate, which should be set to a larger number of persistent generation spaces to hold the new class in the run. The persistent generation size is set by-xx:maxpermsize=<n>.

II: The basic concept of GC

GC is divided into full GC and minor GC, and GC is raised when each chunk is filled.

1.Scavenge GC

In general, when a new object is generated and the Eden application space fails, the scavenge GC is triggered, the heap Eden Zone is GC, the non-surviving objects are cleared, and the surviving objects are moved to the Survivor area, and then the two extents of the survivor are organized.

2.Full GC

Organize the entire heap, including young, tenured and perm. The full GC is slower than the scavenge GC, so the full GC should be minimized as much as possible. The full GC may be caused by the following reasons:

2.1 tenured was written full

2.2 Perm Area is full

2.3 System.GC () is displayed call

2.4 The domain allocation policy for the heap after the last GC has changed dynamically.

III: Introduction to GC 1. Introduction to the garbage collector 1.1 serial collector

Use single-threaded processing for all garbage collection work, because there is no need for multi-threaded interaction, so the efficiency is high. However, it is also not possible to use the advantages of multiple processors, so this collector is suitable for single processor machines. Of course, this collector can also be used on multiprocessor machines with a small amount of data (around 100M). Can be opened using-XX:+USESERIALGC.

1.2 Parallel collector

Parallel garbage collection for younger generations can reduce garbage collection time. Typically used on multi-threaded processor machines. Open with-XX:+USEPARALLELGC. The parallel collector was introduced in JDK5.0 and was enhanced in JDK6.0 to allow for parallel collection of older generations. If the older generation does not use concurrent collections, it is a single thread for garbage collection and therefore restricts the ability to scale. Open with-XX:+USEPARALLELOLDGC.

Use-xx:parallelgcthrads=<n> to set the number of threads for parallel garbage collection. This value can be set equal to the number of machine processors.

You can also use the parallel collector to adjust some other parameters, such as setting the maximum garbage collection pause that specifies the longest pause time when a garbage collection is specified, as specified by-xx:maxgcpausemills=<n>. <N> is milliseconds, and if this value is specified, the heap size and garbage collection related parameters are adjusted to reach the specified value. Setting this value may reduce the throughput of your app. You can also set the throughput (the ratio of the throughput to the garbage collection time versus the non-garbage collected time), which is set by-xx:gctimeratio=<n>, with a formula of 1/(n+1). For example,-xx:gctimeratio=19 indicates that 5% of the time is used for garbage collection. The default is 99, which is 1% of the time used for garbage collection.

1.3 Concurrent Collectors

Can ensure that most of the work is concurrent (the application does not stop), garbage collection only pause for a very small amount of time, this collector is suitable for the response time requirements of high-scale applications. Open with-XX:+USECONCMARKSWEEPGC.

The concurrent collector mainly reduces the time of the old generation to pause, and he uses a separate garbage collection thread to track the available objects without stopping the application. In each old generation garbage collection cycle, the concurrency collector briefly pauses the entire application during the collection, and pauses it again in the collection. The second pause is slightly longer than the first pause, during which multiple threads are garbage collected at the same time.

The concurrent collector uses the processor for a short pause time. On a system of N processors, the concurrent collection portion is recycled using k/n available processors, typically 1<=K<=N/4.
The concurrent collector is used on a host with only one processor, and a short pause time can also be set to incremental mode mode.

Floating garbage: Because garbage collection occurs while the app is running, some of the garbage may be generated when the garbage collection is complete, resulting in "floating garbage", which can be reclaimed at the next garbage collection cycle. Therefore, the concurrent collector typically requires 20% of the reserved space for these floating garbage.

Concurrent Mode Failure: The concurrent collector collects when the app is running, so you need to ensure that the heap has enough space for the program to use during the garbage collection, otherwise the heap space is full before the garbage collection is complete. In this case, a "concurrency mode failure" will occur and the entire app will be paused for garbage collection.

Start the concurrency Collector: Because concurrent collections are collected when the app is running, you must ensure that there is enough memory space for the program to use before the collection is complete, or "Concurrent Mode Failure" will appear. Concurrent collection is performed by setting-xx:cmsinitiaingoccupancyfraction=<n> to specify how many remaining heaps are still available.

Collector Summary

#串行处理器

Usage: Small amount of data (around 100MB), single processor and no requirement for response time application.

Cons: Only for small applications.

#并行处理器

Usage: High demand for throughput, multi-CPU, medium and large applications with no requirement for the corresponding time of application. Examples: Background processing, scientific calculation.

Disadvantage: The appropriate time for the application may be longer.

#并发处理器: The legendary CMS

Usage: High demand for response time, multi-CPU, high demand for application response time, large and medium-sized applications. Examples: Web server/Application server, Telecom Exchange, integrated development environment.

2. Basic collection Algorithm 2.1 Reference counting method

The implementation of the reference counting method is simple, for an object A, as long as any one object refers to a, then the reference counter of A is incremented by 1, and when the reference is invalidated, the reference counter is reduced by 1. Object A can no longer be used as long as the value of the reference counter for object A is 0. You can then clean up unreferenced objects, such as:

Question of the reference counting method:

References and de-referencing accompany addition and subtraction, affecting performance.

It is difficult to handle circular references.

2.2 Mark Clear

The Mark clearing algorithm is the basic idea of modern garbage collection algorithm. Mark Purge is a cut that divides garbage collection into two phases: the tagging phase and the purge phase. A feasible implementation is to mark all the objects that can be reached from the root node, first through the root node, in the tagging phase. Therefore, an object that is not marked is an object that is not referenced. Then, in the purge phase, all unmarked objects are cleared. Such as:

Issues with the Labeling cleanup method

Once the tag is complete, it needs to traverse the entire memory area again, retrieving all objects that are not marked active. The cost of the algorithm traversing the whole space is large, the pause time increases linearly with the space size and the debris in the heap is many.

2.3 Tag Compression method

the tag compression algorithm is suitable for many surviving objects, such as the old age . It does some optimizations on the basis of the tag-clearing algorithm, and, like the tag-clearing algorithm, the tag-compression algorithm first needs to mark all the objects that can be reached, starting with the node. However, it does not simply clean up unmarked objects, but instead compresses all the surviving objects to one end of the memory. After that, clean up all the space outside the boundary. Such as:

2.4 Copy algorithm

Compared with the tag clearing algorithm, the replication algorithm is a relatively efficient recovery method. It is not suitable for occasions where there are many surviving objects, such as the old age. The idea is to divide the original memory space into two pieces (identical), use only one piece at a time, and at garbage collection, copy the surviving object in the memory being used into the unused block of memory, then clear all objects in the memory block in use, swap the two memory roles, and complete the garbage collection. Such as:

Problems with the replication algorithm

There is no doubt that the biggest problem with replication algorithms is wasting space.

Tag Grooming

Comprehensive replication algorithm and the idea of the tag clearing algorithm, the multiple recovery is not dead and large objects are saved to a security area, the remaining small objects copied to a new area, and finally the guarantee area and the completion of the assignment after the area of the reservation, other cleanup. Such as:

GC Algorithm Summary

The reference counting method is not used by Java, and tag compression has an advantage over tag cleanup because tag cleanup also needs to traverse the memory space to clear unmarked objects once the tag is complete, and if the memory space is large, the cost will be very high. According to the idea of generational, in different generations, the selection of appropriate collection algorithm to optimize, for a small number of survival objects of the new generation more suitable for the use of replication algorithm, and for a large number of surviving objects of the old age is more suitable for the use of tag cleaning or tag compression algorithm. For collectors, the younger generation uses the parallel collector well, and the older generation uses the concurrency collector well.

JVM GC Summary

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.