Introduction
Client mode and server Mode
One is the client mode and the other is the server mode.. The usual development modeThe default mode is client mode.You can also use command line parameters-ServerForce enable server mode, the biggest difference between the two isJVM has made many optimizations in server mode..
The JAVA application in server mode is slow to start.However, due to JVM Optimization in server mode,When the program runs for a long time, the running speed will get faster and faster. On the contrary,Although JAVA applications in client mode start fast, they are not suitable for long running.If the running time is long, the performance will be significantly lower than the server mode.
Collector details
Serial Garbage Collector
Algorithm: Using a replication algorithm
Memory Area: Designed for the new generation
Execution Method: Single thread, serial
Execution Process: When the new generation of memory is insufficient, firstPause all user programsAnd then enableOne GC threadThe replication algorithm is used to recycle garbage. In this process, some objects may be upgraded to the old generation.
Features: Because a single thread is running and user programs must be suspended throughout the GC phase, the application will pause for a long time.Small ScaleBut it is very suitable for the program.
Applicable scenarios: Usually used for development and debugging programs, as well as desktop application interaction programs.
Enable Parameters:-XX: + UseSerialGC (default value of client Mode)
Serial Old Garbage Collector
Elder GenerationThereforeMark/organizeAlgorithm. For other dimension features, serial old is the same as the serial collector.
ParNew Garbage Collector
Algorithm: Using a replication algorithm
Memory Area: Designed for the new generation
Execution Method: Multithreading, parallel
Execution Process: When the new generation of memory is insufficient, suspend all user programs and startSeveral GC threadsUsing the replication algorithm for garbage collection in parallel, some objects may be upgraded to the old generation.
Features: Multi-thread parallel operation is adopted. Therefore, it is sensitive to the number of kernel processors in the system and requires at least one processor,Several processors will open several threads (however, the number of threads can be controlled using the-XX: ParallelGCThreads = <N> parameter), So onlySuitable for multi-core multi-processor systems. Although the user program needs to be suspended throughout the GC phase, multi-threaded parallel processing does not cause too long pauses. Therefore, in terms of throughput, ParNew must be greater than serial. The more processors, the more obvious the effect. But this is not absolute,For a single processor, the performance of ParNew is lower than that of the serial collector due to the overhead of parallel execution (such as synchronization ).. In addition to a single processor, if the performance of ParNew is not necessarily higher than serial in the case of a small heap or even two processors.
Applicable scenarios: In a medium to large stack with at least one system processor
Enable Parameters:-XX: + UseParNewGC
Parallel Scavenge Garbage Collector
New GenerationDesign, adoptCopy Algorithm. More precise control of GC pause time and throughput.
Control the maximum pause time (use-XX: MaxGCPauseMillis = <N>) and throughput (use-XX: GCTimeRatio = <N>). It can be seen that parallel scavenge is a collector to provide throughput control.
Maximum pause time, throughput, and minimum value of the new generation Region.
The goal of meeting the maximum pause time is given priority, followed by throughput, and finally the minimum value of the new generation region..
Adaptive Policy.
Parallel Old Garbage Collector
Parallel collectors designed by the old generationSo it usesMark/organizeAlgorithm.
It is the only older collector except serial old that can work with parallel scavenge.To avoid the reputation that serial old affects the controllable throughput of parallel scavenge,Parallel old is the true partner of parallel scavenge..
Concurrent Mark Sweep Garbage Collector
Applications work with GC threads(For customers, but not necessarily the real one, it may be a fast alternative) Collector.
Elder GenerationDesign the collector and useMark/clearAlgorithm, which is alsoThe only oneUsed in the old generationMark/clearAlgorithm collector.
Initial tag: You need to pause the application and quickly mark the surviving object.
Concurrent tag: Restore the application and track GC Roots concurrently.
Remark: You need to pause the application and re-mark the objects missed by the trail.
Concurrent cleanup: Restore the application and concurrently clear unmarked spam objects.
Concurrent mark and concurrent clearThese two stages are also in the GC phase.The longest time-consuming stageHowever, because both stages are executed concurrently with the application, the pause time caused by the CMS collector is very short. This is quite understandable.
DisadvantagesIt also needs to be briefly mentioned, mainly including the following points.
Overall throughput reduction. That is to say, from the perspective of throughput indicators, CMS collectors are weaker than parallel scavenge collectors. Here, LZ extracts the next section about CMS from the oracle official website, which mentions the relationship between CMS performance and CPU count.
Because at least one processor is used for garbage collection in the concurrent phase, using a concurrent collector in the case of a single processor won't be of any benefit. However, on a single or two processor system, there is an independent method that can effectively achieve low pause. For details, see the incremental mode below ).
In the case of a single processor, the concurrent collector will seize the processor, resulting in reduced performance.. Finally, a processing method of incremental mode is provided. However, in the book "deep understanding of Java Virtual Machine", it is pointed out that,The incremental mode has been defined as not recommended.. The official introduction provided by LZ is based on the introduction of JDK, while the book "deep understanding of Java Virtual Machine" refers to the version of JDK. Therefore, LZ is still guessing that, the incremental mode was deprecated when JDK6.0 was released, but the obsolete time or version is no longer important.
Memory fragmentation. Therefore, the JVM provides the-XX: + UseCMSCompactAtFullCollection parameter for performing a fragment after the global GC (full GC, since the fragmentation of each global GC operation will greatly affect the pause time, JVM provides the-XX: CMSFullGCsBeforeCompaction parameter.After several global GC operations, fragment is performed..
Concurrent Mode Failure). This is the official explanation of this term.
Concurrent mode failureAnd indicates the need to adjust the concurrent collector parameters.
Concurrent collectors cannot recycle unreachable objects before the old generation fills up., OrThe valid idle memory space in the old generation cannot meet a memory allocation request.In this case, the application is paused and garbage collection starts during the pause period until the recycle process is completed. This is the case that the collection cannot be completed concurrently.Concurrent mode failure)And this also means we need to adjust the parameters of the concurrent collector.
The old generation cannot be cleared in time in the concurrent clearing stageAs a result, the idle memory is insufficient. But cannot meet the memory allocation request, it mainly refers to when the new generation is upgraded to the old generation, becauseThe old generation has too many memory fragments.As a result, some distributions cannot be satisfied due to lack of continuous memory.
Concurrency mode failedIn this case,Serial oldA global GC (Full GC) is performed as an alternative collector. Therefore, serial old is also a substitute for CMS ". Apparently, the intervention of serial old may cause a great pause.
Power of Combination
Serial & serial old
Small applications running on the client PC, or desktop applications (such as user interface programs written by swing), as well as our usual development, debugging, and testing.
Parallel Scavenge & Parallel Old
Background programs that require long-term operation and have certain throughput requirements.
ParNew & CMS (Serial Old as a substitute)
Background programs that require long-term operation and have certain time requirements.