The recent time is tense, must write the thing also has many, can only think a bit to write a point. About the GC, the information on the Internet too much, before a system tuning time and review, find a few widely circulated information, most of them are similar, here is a summary, I hope to be able to do a relatively complete, and write some new points, such as card marking (card tag) and so on.
The first is the basic algorithm that everyone has to mention in GC: Tag cleanup, tagging, copying, generational. The first step in these algorithms is to do one thing: Mark.
The JVM's tagging algorithm uses the root search algorithm (root tracing). There are several types of roots:
1. References inside the frame of the JVM stack
2. A static class, a reference to a constant
3. References in the local stack
4. References to local methods
In general, we can control references in the JVM stack and static classes, constants. Markers are also divided into several stages, such as
1. Mark objects directly and in the root reference
2. Labeling indirect and root-referenced objects
3. Because of the generational algorithm, the new generation object referenced by the old age object
For the third, the JVM uses card marking (Card tag) method to avoid the need to scan the entire old age when doing minor GC. The specific method is as follows:
1. The memory of the old age is fragmented, 1 tablets by default is 512byte
2. If the object of the old age has changed, mark the slice of the old age object as dirty dirty. Or an old age object pointing to a new generation of objects, then the slice it's in is also marked as dirty.
3. No old age marked as dirty it does not point to new Cenozoic objects, so it does not need to be scanned
4. Minor GC scan the old space, only need to scan the dirty card objects, do not need to scan the entire old age space
So do minor gc when tagged time = t (Stack_scan) + t (Card_scan) + t (Old_root_scan).
T (Stack_scan): The time the cascade scans the root in the JVM stack
T (Card_scan): The time of the dirty card in the Cascade Scan card table
T (Old_root_scan): Scans the time of the direct root in the old age. Note that it is the direct root that does not go to the object of the old age of cascading scans. Because the scans are all starting from the root, do not know at first whether the root is in the old age or the new generation
One of the important JVM parameters associated with card marking is -xx:usecondcardmark . The reason for using this parameter is that in the case of high concurrency, the card marked as dirty is inherently competitive, and using this parameter can prevent the card from being repeatedly flagged as dirty, thereby improving performance.
After saying the mark, here are some basic GC algorithms, there is no new point, direct reference to the online diagram
Tag-Purge algorithm
Replication Algorithms
Tagging--Finishing algorithms
The generational algorithm is to divide the object into the new generation and the old age, and then use different GC strategies to recover and improve the overall efficiency.
Because most of the new generation of objects will die in a minor GC, there are few surviving objects, so the new generation GC collector uses the replication algorithm. The Cenozoic is divided into Eden + S0 + S1. S0 and S1 are used for replication, and after any minor GC, S0 and S1 always have only one region of data, and the other is empty, so that the next replication uses
When the Cenozoic space can not meet the large object allocation, the old-age space provides it with the distribution guarantee, the large object can enter the old age directly. There are two JVM parameters to control the threshold of the new generation entering the old age:
Pretenuresizethreshold: Unit is B, set the threshold of the object size
Maxtenuringthreshold: Sets the threshold for the age of entry into the old era
Old age objects are generally survival time long, the old age of space is originally large, so there is no more space to provide distribution guarantees, so the old age is generally used to mark-clean or mark-sorting algorithm.
The following diagram is a good introduction to the various GC collectors of JDK6 and their respective features:
1. Replication algorithms are used in the Cenozoic
2. The CMS uses the tag-purge algorithm, because the tag cleanup algorithm generates memory fragmentation, so the JVM provides parameters to enable the CMS to clean up after a few cleanup
-xx:cmsfullgcsbeforecompaction: Because the concurrent collector does not compress and defragment the memory space, it can produce "fragmentation" after a period of time, which makes the operation less efficient. This value sets how many times a GC is run to compress and defragment the memory space.
-xx:+usecmscompactatfullcollection: Turn on compression for older generations. Performance may be affected, but fragmentation can be eliminated
3. Serial old (MSC) and parallel old both use the tag grooming algorithm
4. USESERIALGC uses the serial collector in the new generation by default, using the serial old collector in the older era, both single-threaded collectors
5. USECONCMARKSWEEPGC will use the Parnew collector again by default, which is a concurrent collector. The older generation will use the CMS + Serial old collector, and when the CMS fails, it will enable Serial to do full GC
6. USEPARALLELOLDGC defaults to using the parallel scavenge collector in the new generation, using the parallel old collector in the older age. Both collectors are throughput-first, and the so-called throughput priority is that it can strictly control GC time, thus guaranteeing throughput. But the throughput has improved, and the new generation and the old years are dynamically tuned, not by the size of the initial configuration. Since the amount of garbage purged per unit of time is near a constant, since the time is guaranteed, the total amount of garbage must be guaranteed, and the total amount of rubbish can be controlled by the size of the new generation and the old age .
7. For applications that interact with users, such as Web applications, an important consideration is the response time of the system, to ensure that the response time of the system is guaranteed to be less than the number of stops caused by the GC, or to let the user thread and the GC thread run together. So Web applications are an important scenario for using CMS collectors. The CMS reduces the number of stops in the world and inevitably makes the overall GC time elongated .
8. For compute-intensive applications, the calculated throughput may be considered, and the parallel scavenge collector can be used to ensure throughput
9. Serial, Parnew, Parallel Scanvange, Parallel old, Serial old all the way stop the WORLD,JVM this time only the GC thread is running and the user thread is not running
CMS is mainly divided into initial mark, Concurrent Mark, ReMark, Concurrent sweep and so on, initial mark and ReMark account for the overall time is relatively small, they will stop the world. Concurrent Mark and Concurrent sweep will run with the user thread.
The following diagram illustrates the GC log information:
About the various parameters of the JVM tuning settings, a lot of online grab, not much to say. There is a principle of tuning the whole:
1. Perform a performance test of the JVM to understand the current state
2. Clearly tuned goals, such as reducing the number of full GC, reducing GC total time, increasing throughput, etc.
3. Adjust the parameters and then conduct a number of tests, analysis, and finally achieve a more ideal state. The various parameters should be determined according to the system's own situation, there is no unified solution
There are a lot of article pages for various tools, here are a few from the point of view of problem solving.
View JVM Startup Parameters
1. Jps-v
2. Jinfo-flags PID
3. Jinfo PID--list JVM startup parameters and System.properties
4. Ps-ef | grep java
View the configuration of the current heap
1. JSTAT-GC PID 1000 3--Lists the size of each area of the heap
2. Jstat-gcutil PID 1000 3--lists the proportions used for each area of the heap
3. Jmap-heap PID--Lists the currently used GC algorithm, the size of each area of the heap
To view the stack information for a thread
1. Jstack-l PID
Objects within the Dump heap
1. Jmap-dump:live,format=b,file=xxx PID
2.-xx:+heapdumponoutofmemoryerror-xx:heapdumppath=xxx--Set JVM parameters, output heap dump when JVM Oom
3. Ulimit-c Unlimited--Set the Linux ulimit parameter, which can produce coredump and is not limited by size. Before on-line encountered an extremely strange problem, the JVM entire process suddenly hangs, this time relies on the JVM itself to generate dump file is not enough, only relies on the Linux, lets the system to build the process hangs out the core dump file
Use Jstack to get the thread stack information for this coredump: Jstack "$JAVA _home/bin/java" core.xxx > Core.log
Gets the current system consumes the highest CPU 10 processes, the thread
PS Hh-eo Pid,tid,pcpu,pmem | Sort-nk3 |tail > Temp.txt
Graphical interface
1. There are many plugins in the JVISUALVM, such as Visual GC, which can visualize the state of each heap area, so as to have an overall understanding of the overall GC performance.
Let's just say, there's something missing.
Reference: Understanding GC pauses in JVM, HotSpot ' s minor GC.
In-depth understanding of the JVM
"Think in GC"
Talk about the JVM (a) a relatively comprehensive GC summary