Basic Parameter Configuration
# MetaspaceSize=128M -xx :MaxMetaspaceSize=256MCopy the code
JVM garbage collection
- This part of the content collation source
https://www.cnblogs.com/blythe/p/7488061.html
Garbage collection algorithm
- Mark-sweep algorithm
- Replication algorithm
- Mark-compact algorithm
- Generational collection algorithm
Garbage collector
- 1. Seven kinds of garbage collectors:
Serial (Serial GC) – Copy ParNew (Parallel GC) – Copy Parallel Scavenge (Parallel RECLAIM GC) – Copy Serial Old (MSC) (Serial GC) – Mark – Sort CMS (Concurrent GC) – Mark – Clear Parallel Old (parallel GC) — Tag-collation
1 3 For young generation garbage collection: Young generation garbage collection is called minor GC 4 6 for ten-year-old garbage collection (of course, can also be used for method area collection) : Ten-year-old garbage collection is called full GC G1 complete “generational garbage collection” independently note: parallel and concurrent
Parallel: Multiple garbage collection threads operate concurrently. Concurrent: Garbage collection threads operate together with user threads
- 2, commonly used five combinations:
ParNew/CMS: be insane/exploiture/avenge/ exploiture Automatically managed combination G1: state-of-the-art collector, but requires JDK1.7update14 +
2.1) Serial/Serial Old:
Features:
- The young Serial collector uses a single GC thread to implement the “copy” algorithm (including scanning and copying)
- The Serial Old collector uses a single GC thread to implement the mark-collation algorithm
- Both Serial and Serial Old pause all user threads (i.e. STW)
Description:
STW (Stop the World) : Inject SafePoint (the point at which the loop in a method ends, the point at which the execution of the method ends) into each method when compiling the code. When suspending the application, wait for all user threads to enter SafePoint, then suspend all threads, and then garbage collect.
Application:
- A machine with less than 2 CPU cores and less than 2 gb of physical memory (in simple terms, a single CPU, the new generation of small space and low STW time requirements)
- -xx :UseSerialGC: the GC combination is mandatory
- – XX: PrintGCApplicationStoppedTime: check the STW time
- Because it is relatively simple to implement and has no thread-related overhead (mainly thread switching and synchronization), it is ideal for small applications running on client PCS, or desktop applications (such as user interface programs written in Swing), as well as our usual development, debugging, testing, and so on.
(2.2) ParNew/Serial Old:
Description:
ParNew is the same as Serial except that multiple GC threads are used to implement the replication algorithm, but Serial Old in this combination is also a single GC thread, so this combination is a rather awkward combination. It’s not as fast as Serial/Serial Old in the single-CPU case (because the ParNew threads need to switch), and it’s not as fast as the next three combinations in the multi-CPU case (because Serial Old is a single GC thread), so it’s not really used much.
-xx :ParallelGCThreads: Specifies the number of ParNew GC threads. The default value is the same as the number of CPU cores. This parameter can be used when CMS GC is combined
(2.3), the Parallel Scavenge/Parallel Old:
Features:
- The Parallel Scexploiture uses multiple GC threads to apply the exploiture algorithm.
- The Parallel Old collector uses multiple GC threads to implement the “mark-collation” algorithm
- Avenge and Parallel Old suspend all user threads (STW).
Description:
- Throughput: CPU running code time /(CPU running code time +GC time)
- CMS mainly focuses on shortening STW (the shorter the time, the better the user experience, so it is mainly used for dealing with many interactive tasks)
- Apply to the APPLICATION. Parallel Scavenge/Parallel Old focuses on throughput. The application is based on a higher CPU utilization.
Parameter Settings:
- -xx :+UseParallelOldGC: Uses the GC combination
- -xx :GCTimeRatio: sets the throughput directly. If the value is set to 19, the maximum GC time allowed is 1/(1+ 19) of the total time. The default value is 99, that is, 1/(1+99).
- -xx :MaxGCPauseMillis: The maximum GC pause time. Smaller is not better
- – XX: + UseAdaptiveSizePolicy: Open the parameter, Xmn / – XX: SurvivorRatio / – XX: PretenureSizeThreshold these parameters doesn’t work, virtual opportunity to automatically collect monitoring information, dynamic adjustment of these parameters in order to provide the most appropriate pause time or the maximum throughput adaptive adjustment strategy (GC), All we need to do is set -xmx, -xx :+UseParallelOldGC or -xx :GCTimeRatio (of course -xms specifies the same as -xmx).
Application:
- Lots of CPU computing and little user interaction
- I don’t want to pay too much attention to GC parameters myself, I want to let the virtual machine do the tuning itself
- The throughput requirement is high, or need to reach a certain amount.
(2.4) ParNew/CMS:
Description:
- ParNew “2.2, ParNew/Serial Old” is good
- CMS is multi-recycle threads, don’t be misled by the figure above, the default number of threads is (number of cpus +3)/4
- CMS mainly focuses on shortening STW (the shorter the time, the better the user experience, so it is mainly used for dealing with many interactive tasks)
Features:
1. The young generation of ParNew collector uses multiple GC threads to achieve the “copy” algorithm (including scanning, copying)
2. The elderly CMS collector adopts multi-threading to implement the “mark-clean” algorithm
- Initial tag: Tags the node directly associated with the root collection node. The time is very short and STW is required
- Concurrent marking: Iterates over the associated nodes previously marked and continues marking down all surviving nodes. Longer time.
- Re-mark: Re-walk the reference objects that have been modified during trace concurrency. The time is between the initial tag and the concurrent tag, and is usually not very long. Need to STW
- Concurrent cleanup: Direct cleanup of nonliving objects, after the cleanup, the CPU occupied by the thread to switch to the user thread
3. Initial marking and re-marking pause all user threads (STW), but for a short time; Concurrent marking and concurrent cleanup take longer, but STW is not required
How do you record reference objects that change during concurrent marking and how do you scan them during recasting
Disadvantages:
- Concurrent marking and concurrent cleanup: As indicated in the second point, if you have 2 cpus, one CPU will be used for garbage collection and the other for user threads. If you have 2 cpus running user threads and 1 CPU running user threads, the efficiency is dramatically reduced. That is, it reduces throughput (that is, CPU usage).
- Concurrent cleanup: During this process, the generated garbage cannot be cleaned up (because it happens after the re-marking)
- Concurrent marking and concurrent cleanup: Since it is concurrent with the user thread, the user thread may allocate objects, either directly into the ten-year-old (for example, large objects), or after it enters the young generation, minor GC occurs in the young generation, which in effect requires space in our ten-year-old generation. That is to say, garbage collection should be done when the tenderly generation has a certain amount of space, leaving a certain amount of memory for other threads to use, rather than waiting until the tenderly generation is almost full. Through – XX: CMSInitiatingOccupancyFraction to specify the old generation after how much space is full of garbage collection
- Mark-clean algorithm: will cause memory fragmentation and, due to the old age, may trigger Full GC prematurely (which is what we want to minimize)
Parameter Settings:
- -xx :+UseConcMarkSweepGC: Uses the GC combination
- – XX: CMSInitiatingOccupancyFraction: specify the old generation after how much space is full of garbage collection
- – XX: + UseCMSCompactAtFullCollection: (default is open) in the CMS to FullGC collector is unable to open the memory defragmentation process, this process requires STW
- – XX: CMSFullGCsBeforeCompaction: specify how many times after FullGC
- -xx :ParallelCMSThreads: Specifies the number of CMS threads to recycle. The default value is: (number of cpus +3)/4
Application:
For situations where you are dealing with many interactive tasks
Methods of recycling generally use CMS, configure two parameters: – XX: + CMSPermGenSweepingEnabled and – XX: + CMSClassUnloadingEnabled
It is suitable for some background programs which need to run for a long time and have certain requirements for corresponding time
G1 (2.5)
Description:
- As you can see from the figure above, G1 differs from CMS only in the final “filter reclamation” section (CMS is concurrent cleanup), in fact the entire heap memory partition of G1 collector is different from other collectors.
- CMS needs to cooperate with ParNew, and G1 can reclaim the entire space separately
Principle:
- The G1 collector divides the entire heap into equally-sized regions
- G1 tracks the value of garbage accumulation in each region (the experience value of the space obtained after collection and the time required for collection), maintains a priority list in the background, and collects the region with the largest value first according to the allowed collection time. The idea is as follows: In the specified time, scan part of the most valuable region (rather than scanning the entire heap memory), and reclaim, as far as possible in the limited time to obtain the highest collection efficiency.
Operation process:
- Initial tag: Identifies all reference objects directly associated with the root node. Need to STW
- Concurrent marking: Iterates over the associated nodes previously marked and continues marking down all surviving nodes. All objects whose references change during this period are recorded in Remember Set Logs
- Final markup: Markup new garbage generated during concurrent markup. Need to STW
- Filter reclamation: Recycle objects with high value according to the expected reclamation time specified by the user (see Principle 2). Need to STW
Advantages:
- The pause time can be predicted: we specify the time and only recycle part of the space with the largest value within the specified time, while CMS needs to scan the whole ten-year-old generation and cannot predict the pause time
- No memory fragmentation: space will be consolidated after garbage collection. CMS adopts the “mark-clean” algorithm, and memory fragmentation exists
- Screening recycling stage:
- Since only part of regions are reclaimed, we can control the STW time, so we do not need to compete for CPU resources with user threads concurrently. However, concurrent CLEANUP of CMS requires a part of THE CPU, which will reduce the throughput.
- Because of STW, “floating garbage” (that is, unrecyclable garbage generated by CMS during concurrent cleanup) is not generated
Scope of application:
- Go for STW short: If ParNew/CMS works, use this; If not, use G1
- Exploiture/Application of Parallel Scavenge/Parallel Old, while the G1 has no advantage in throughput