5 ways to tune a typical JVM configuration!

Summary of common configurations:

1. Heap settings

-Xms: initial heap size

-Xmx: Maximum heap size

-XX:NewSize=n: set the size of the young generation

-XX:NewRatio=n: Set the ratio between the young generation and the old generation. For example: 3, which means that the ratio of the young generation to the old generation is 1:3, and the young generation accounts for 1/4 of the total of the young generation and the old generation.

-XX: SurvivorRatio=n: The ratio of the Eden area to the two Survivor areas in the young generation. Note that there are two Survivor areas. For example: 3, which means Eden: Survivor=3: 2, a Survivor area occupies 1/5 of the entire young generation

-XX:MaxPermSize=n: set the persistent generation size

2. Collector settings

-XX:+UseSerialGC: Set the serial collector

-XX:+UseParallelGC: Set the parallel collector

-XX:+UseParalledlOldGC: Set the parallel old generation collector

-XX:+UseConcMarkSweepGC: set concurrent collector

Garbage collection statistics

-XX:+PrintGC

-XX:+PrintGCDetails

-XX:+PrintGCTimeStamps

-Xloggc:filename

3. Parallel collector settings

-XX:ParallelGCThreads=n: Set the number of CPUs used by the parallel collector for collection. The number of parallel collection threads.

-XX:MaxGCPauseMillis=n: set the maximum pause time for parallel collection

-XX:GCTimeRatio=n: Set garbage collection time as a percentage of program running time. The formula is 1/(1+n)

4. Concurrent collector settings

-XX:+CMSIncrementalMode: set to incremental mode. Applicable to single CPU situation.

-XX:ParallelGCThreads=n: The number of CPUs used when the young generation collection mode of the concurrent collector is set to parallel collection. The number of parallel collection threads.

5. Tuning summary

Young generation size selection

Response time priority application: Set as large as possible until it is close to the minimum response time limit of the system (choose according to the actual situation). In this case, the frequency of the young generation collection is also the smallest. At the same time, reduce the number of objects reaching the old generation.

Applications with throughput priority: Set as large as possible, possibly reaching the level of Gbit. Because there is no requirement for response time, garbage collection can be performed in parallel, which is generally suitable for applications with 8 CPUs or more.

Old generation size selection

Response time priority applications: The old generation uses concurrent collectors, so its size needs to be carefully set. Generally, some parameters such as concurrent session rate and session duration should be considered. If the heap setting is small, it may cause memory fragmentation, high recovery frequency, and application suspension and use the traditional mark removal method; if the heap is large, a longer collection time will be required. The most optimized solution generally needs to be obtained by referring to the following data.

♦ Concurrent garbage collection information

♦ Persistent generation concurrent collection times

♦ Traditional GC information

♦ Proportion of time spent on recycling of young and old generations

Reducing the time spent by the young and old generations generally improves the efficiency of the application.

Throughput priority applications

Generally, throughput-first applications have a large young generation and a smaller old generation. The reason is that most of the short-term objects can be recycled as much as possible, the medium-term objects can be reduced, and the old generation can store long-term surviving objects.

Fragmentation problems caused by smaller heaps?

Because the old generation of concurrent collectors use marking and clearing algorithms, they will not compress the heap. When the collector reclaims, it merges adjacent spaces so that they can be allocated to larger objects. However, when the heap space is small, after running for a period of time, "fragmentation" will occur. If the concurrent collector cannot find enough space, the concurrent collector will stop, and then use the traditional marking and cleaning methods for recycling. If "fragments" appear, the following configuration may be required.

-XX:+UseCMSCompactAtFullCollection: When using the concurrent collector, enable compression for the old generation.

-XX:CMSFullGCsBeforeCompaction=0: When the above configuration is turned on, here is how many Full GCs are set to compress the old generation

Reader benefits

Thank you for seeing here!
I have compiled a lot of 2021 latest Java interview questions (including answers) and Java study notes here, as shown below
Insert picture description here

The answers to the above interview questions are organized into document notes. As well as interviews also compiled some information on some of the manufacturers & interview Zhenti latest 2021 collection (both documenting a small portion of the screenshot) free for everyone to share, in need can click to enter signal: CSDN! Free to share~

If you like this article, please forward it and like it.

Remember to follow me!
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_49527334/article/details/115287999