JVM garbage collection (GC) sorting summary and learning, jvm garbage collection

Source: Internet
Author: User

JVM garbage collection (GC) sorting summary and learning, jvm garbage collection
Basic recycling Algorithm

1. Reference Counting)
The old recycling algorithm. The principle is that this object has a reference, that is, adding a count. deleting a reference reduces the count. During garbage collection, only objects with zero collection count are used. The most critical issue of this algorithm is that it cannot handle circular references.
2. Mark-clear (Mark-Sweep)
This algorithm is executed in two phases. In the first stage, all referenced objects are marked from the reference root node. In the second stage, the whole heap is traversed to clear unmarked objects. This algorithm suspends the entire application and generates memory fragments.
3. Copy)
This algorithm divides the memory space into two equal regions and uses only one of them at a time. During garbage collection, traverse the current region and copy the objects in use to another region. The algorithm only processes objects in use at a time, so the replication cost is relatively small. At the same time, the corresponding memory can be organized after the replication, but there is a "Fragmentation" problem. Of course, the disadvantage of this algorithm is also obvious, that is, it requires two times of memory space.
4. Mark-Compact)
This algorithm combines the advantages of "tag-clear" and "copy" algorithms. It is also divided into two phases. In the first phase, all referenced objects are marked from the root node. In the second stage, the whole heap is traversed to clear unlabeled objects and compress the surviving objects to one of the heap, discharge in sequence. This algorithm avoids the "tag-clear" fragmentation problem, and also avoids the space problem of the "copy" algorithm.
5. Incremental Collecting)
The garbage collection algorithm is implemented, that is, garbage collection is performed simultaneously by the application. I don't know why the Collector in JDK5.0 does not use this algorithm.
6. Generational Collecting)

Based on the garbage collection algorithm obtained after object lifecycle analysis. Objects are divided into young, old, and persistent generations. Different algorithms (one of the above methods) are used to reclaim objects in different lifecycles. The current garbage collector (beginning with J2SE1.2) uses this algorithm.

A. Young (Young generation)
The young generation is divided into three zones. One Eden zone and two vor zones. Most objects are generated in the Eden area. When the Eden zone is full, the surviving objects will be copied to the primary vor zone (one of the two). When the primary vor zone is full, the surviving objects in this region will be copied to another region vor. When the region VOR is full, the surviving objects will be copied from the first region vor, it will be copied as "Tenured )". Note that the two regions of the same vor are symmetric and irrelevant. Therefore, objects copied from Eden and copied from the previous vor may exist in the same region, only objects copied from the first vor region are copied to the old district. In addition, there is always a blank vor area.
B. Tenured (elder generation)
The old generation stores objects that survive from the young generation. Generally, the old generation stores objects with long life periods.
C. Perm (permanent generation)
Used to store static files. Currently, Java classes and methods are supported. Persistent generation has no significant impact on garbage collection, but some applications may dynamically generate or call some classes, such as Hibernate, in this case, you need to set up a large persistent storage space to store the classes added during the running process. The persistent generation size is set by-XX: MaxPermSize = <N>.

GC type

1. Scavenge GC
Generally, when a new object is generated and the Eden application fails, Scavenge GC is triggered, and GC is performed in the heap Eden area to clear non-surviving objects, and move the surviving objects to the same vor area. Then, sort out the two zones in the same vor.
2. Full GC
Organize the entire heap, including Young, Tenured, and Perm. Full GC is slower than Scavenge GC, so Full GC should be minimized. Full GC may occur due to the following reasons:
* The Tenured is full.
* The Perm domain is full.
* System. gc () is displayed and called
* After the last GC, Heap's allocation policies for various domains are dynamically changed.

Garbage Collector

1. Serial collector
A single thread is used to process all garbage collection tasks. Because multi-thread interaction is not required, the efficiency is relatively high. However, the advantages of multi-processor cannot be used, so this collector is suitable for single-processor machines. Of course, this collector can also be used on a multi-processor machine with a small amount of data (about MB. You can enable-XX: + UseSerialGC.
2. Parallel collector
1. Parallel garbage collection for the young generation can reduce the garbage collection time. Generally used on multi-threaded Multi-processor machines. Use-XX: + UseParallelGC. To open. The parallel collector is introduced in J2SE5.0 update and is enhanced in Java SE6.0-it can be used for parallel collection by heap elders. If the old generation does not use concurrent collection, it uses a single thread for garbage collection, which restricts the scalability. Use-XX: + UseParallelOldGC.
2. Use-XX: ParallelGCThreads = <N> to set the number of parallel garbage collection threads. This value can be set to be equal to the number of machine processors.
3. This collector can be configured as follows:
* Maximum garbage collection pause: specifies the maximum pause time for garbage collection, which is specified through-XX: MaxGCPauseMillis = <N>. <N> millisecond. If this value is specified, the heap size and garbage collection parameters are adjusted to reach the specified value. Setting this value may reduce the application throughput.
* Throughput: the ratio of the garbage collection time to the non-garbage collection time. The formula is 1/(1 + N) by-XX: GCTimeRatio = <N> ). For example, when-XX: GCTimeRatio = 19, it indicates that 5% of the time is used for garbage collection. The default value is 99, that is, 1% of the time is used for garbage collection.

3. Concurrent collector
It can ensure that most of the work is performed concurrently (the application does not stop), and garbage collection only suspends a small amount of time. This collector is suitable for medium and large scale applications with high response time requirements. Enable-XX: + UseConcMarkSweepGC.
1. the concurrent collector mainly reduces the pause time of the old generation. It uses an independent garbage collection thread to track reachable objects without stopping the application. In each old generation garbage collection cycle, the concurrent collector will temporarily pause the entire application at the early stage of collection, and pause again during collection. The second pause will be a little longer than the first one. In this process, multiple threads perform garbage collection at the same time.
2. the concurrent collector uses the processor for a short pause. In a system with N processors, the concurrent collection part is recycled using K/N available processors. Generally, 1 <= K <= N/4.
3. Use the concurrent collector on a host with only one processor. If it is set to incremental mode, a short pause time can also be obtained.
4. floating Garbage collection: because the application is running at the same time for Garbage collection, some Garbage may be generated when the Garbage collection is completed, resulting in "Floating Garbage ", this garbage can be recycled only in the next garbage collection cycle. Therefore, the concurrent collector generally requires 20% of the reserved space for these floating spam.
5. concurrent Mode Failure: the Concurrent collector collects data when the application is running. Therefore, make sure that the heap has sufficient space for the application during garbage collection. Otherwise, garbage collection is not completed, the heap space is full first. In this case, "concurrent mode failure" will occur, and the entire application will be paused for garbage collection.
6. Start the Concurrent COLLECTOR: Because Concurrent collection is collected during application running, you must ensure that there is sufficient memory space for the program to use before the collection is complete. Otherwise, "Concurrent Mode Failure" appears ". You can set-XX: CMSInitiatingOccupancyFraction = <N> to specify the number of remaining heaps to start concurrent collection.

4. Summary
* Serial processor:
-- Applicability: applications with a small data volume (about MB) and a single processor that have no requirements for response time.
-- Disadvantage: it can only be used for small applications
* Parallel processor:
-- Applicability: medium and large applications with high throughput requirements, multiple CPUs, and no application response time requirements. Example: background processing and scientific computing.
-- Disadvantage: The application response time may be long.
* Concurrent processors:
-- Applicability: medium and large applications with high requirements on response time, multiple CPUs, and high requirements on application response time. Example: Web server/application server, telecom exchange, and integrated development environment.

Optimization Summary

1. Young Generation Size Selection
* Applications with priority over Response Time: set as large as possible until it is close to the minimum response time limit of the system (depending on the actual situation ). In this case, the collection frequency of the young generation is also the smallest. At the same time, reduce the number of objects that reach the old generation.
* Applications with high throughput priority: the maximum throughput may reach the Gbit level. Because there is no requirement on the response time, garbage collection can be performed in parallel, which is generally suitable for applications with more than 8 CPUs.

2. Select the size of the Elder Generation
* Applications with priority over response time: the concurrency collector is used in older generations. Therefore, you need to carefully set the response size. Generally, you need to consider the concurrency session rate, session duration, and other parameters. If the heap settings are small, it may cause memory fragmentation, high recovery frequency, and application suspension. The traditional mark clearing method is used. If the heap is large, it takes a long time to collect. For the optimal solution, you generally need to obtain the following data:
O concurrent garbage collection information
O number of persistent generation concurrent collection times
O traditional GC Information
O ratio of time spent on the recovery of young and old generations
Reducing the time spent by the young and old generations will generally improve application efficiency.
* Throughput-first applications: Generally, throughput-first applications have a large young generation and a small old generation. The reason is that most of the short-term objects can be recycled as much as possible to reduce the medium-term objects, while the old generation will store the long-term surviving objects as much as possible.

3. fragmentation problems caused by small heaps
The heap is not compressed because the concurrent collector of the old generation uses the marking and clearing algorithms. When the collector recycles the object, it merges the adjacent space and assigns it to a large object. However, when the heap space is small and runs for a period of time, "fragments" will appear. If the concurrent collector cannot find enough space, the concurrent collector will stop, then, use the traditional marking and clearing methods for recycling. If "fragments" appear, you may need to configure the following:
*-XX: + UseCMSCompactAtFullCollection: Enable compression for the old generation when concurrent collectors are used.
*-XX: CMSFullGCsBeforeCompaction = 0: When the preceding configuration is enabled, how many Full GC times are set here to compress the old generation?

I am the dividing line of tiantiao

 

 

 

Reference: http://chenchendefeng.iteye.com/blog/455883


How does jvm call gc for garbage collection? Thank you!

Heap memory is divided into three parts: Permanent storage zone + new-generation zone + pension Zone
The new zone is the region where classes are born, grown, and extinct. A class is generated here, applied, and finally collected by the garbage collector to end its life. The new-generation zone is divided into two parts: the Eden space and the Survivor zone. All classes are new in the edian zone. There are two surviving zones: Zone 0 (same vor 0 space) and Zone 1 (same vor 1 space ). When the space in the Garden of Eden is used up, the program needs to create an object. The JVM garbage collector will recycle the garbage in the Garden of Eden and destroy the objects in the Garden of Eden that are no longer referenced by other objects. Then, move the remaining objects in the Garden of Eden to the surviving Zone 0. If the surviving Zone 0 is full, the area will be reclaimed and moved to zone 1. What if Zone 1 is full? Then move to the elder care area.

Detailed explanation of jvm garbage collection mechanism

1. JVM gc Overview
Gc (garbage collection mechanism) is used by jvm to release the memory occupied by objects that are no longer in use. The java language does not require jvm to have gc, nor does it specify how gc works. However, common JVMs have gc, and most gc uses similar algorithms to manage memory and perform collection operations.
Only after fully understanding the garbage collection algorithm and execution process can we effectively optimize its performance. Some garbage collections are dedicated to special applications. For example, real-time applications primarily aim to avoid spam interruptions, while most OLTP applications focus on overall efficiency. After understanding the workload of the application and the garbage collection algorithm supported by jvm, You can optimize the configuration of the garbage collector.
The purpose of garbage collection is to clear objects that are no longer in use. Gc determines whether to collect the object by determining whether the object is referenced by the active object. Gc must first determine whether the object can be collected at the time. Two common methods are reference count and Object Reference traversal.
1. Reference count
Reference count stores the number of all references to a specific object. That is to say, when the application creates a reference and the reference is out of the range, the jvm must increase or decrease the number of references as appropriate. When the reference number of an object is 0, garbage collection can be performed.
1. 2. Object Reference Traversal
Early JVMs used reference counting. Currently, most JVMs use Object Reference traversal. Object Reference traversal starts from a group of objects and recursively identifies reachable objects along each link in the entire object graph. If an object cannot be reached from one (at least one) of these root objects, it is collected as garbage. In the object traversal stage, gc must remember which objects can arrive in order to delete inaccessible objects. This is called a mark (marking) object.
Next, the gc will delete the inaccessible objects. During deletion, some gc simply scans the stack, deletes untagged objects, and releases their memory to generate new objects. This is called clearing ). The problem with this method is that the memory will be divided into many small segments, but they are not enough for new objects, but they are very large in combination. Therefore, many gc can reorganize the objects in the memory and compact them to form available space.
To this end, gc needs to stop other activities. This method means that all the work related to the application is stopped and only gc is run. As a result, many mixed requests are added or subtracted during the response. In addition, more complex gc increases or runs at the same time to reduce or clear application interruptions. Some gc uses a single thread to complete this task, while others use multiple threads to increase efficiency.
2. Several garbage collection mechanisms
2. 1. Mark-clear collector
The collector first traverses the object graph and marks reachable objects, then scans the stack to find untagged objects and releases their memory. This type of collector generally uses a single thread to work and stops other operations.
2. Mark-compression collector
It is also called the Mark-clear-compression collector, which has the same mark stage as the Mark-clear collector. In the second stage, the tag object is copied to the new domain of the stack to compress the stack. This collector also stops other operations.
2. 3. Copy the Collector
This collector divides the stack into two fields, which are often called semi-space. Only half of the space is used each time, and the new object generated by jvm is placed in the other half of the space. During gc running, it copies the reachable objects to the other half of the space, thus compressing the stack. This method is applicable to objects with a short lifetime, and objects with a long lifetime of continuous replication result in lower efficiency.
2. 4. incremental collector
The incremental collector divides the stack into multiple domains and collects garbage from only one domain at a time. This can cause small application interruptions.
. Generational collectors
This collector divides the stack into two or more fields to store objects of different lifetime. New objects generated by jvm are generally placed in a certain domain. After a period of time, the existing object will get the validity period and be transferred to a domain with a longer life cycle. The generational collectors use different algorithms for different domains to optimize performance.
. Concurrent collector
Concurrent collectors and applications run simultaneously. These collectors generally have to stop other operations at a certain point (such as during compression) to complete specific tasks, but because other applications can perform other background operations, so interrupt it ...... remaining full text>

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.