jvm performance optimization
**************************
Project optimization: operating system, project architecture, program code, database, jvm parameters
Operating system optimization: memory, CPU, storage devices, etc.
Project architecture optimization: design a reasonable architecture for the application
Program code optimization: avoid loading too many objects at once, do not refer to short-period objects with long-lived variables, etc.
Database optimization: design a reasonable database and table structure for the project
jvm optimization: garbage collector and memory parameter settings
Note: Before the jvm optimization, the project architecture, code, etc. need to be optimized. If the project architecture or code design is insufficient, adjusting the jvm parameters will have little effect on the performance of the application.
**************************
JVM tuning evaluation indicators: memory, throughput, latency
Memory: the memory required for the normal operation of the program
Throughput: The ratio of application running time to total time (application running time + garbage collection time)
Delay: the pause time of the application during garbage collection
The goal of jvm tuning is to achieve greater throughput or lower latency with less memory , but these three goals cannot be achieved at the same time:
If you set a larger memory , garbage collection will take a longer time, resulting in a longer delay ;
If you set a lower memory , it will trigger frequent garbage collection, affecting throughput
**********************
Garbage collector : single-threaded, parallel, concurrent, need to choose the appropriate garbage collector according to the application scenario
***************
Single-threaded garbage collector: serial (new generation), serial old (old generation)
Features: During garbage collection, user threads stop working and there is only one garbage collection thread
Application scenarios: suitable for desktop applications, with dozens or hundreds of megabytes of garbage collected, and short pause times caused by garbage collection
Related parameters
-XX: + UseSerialGC: use serial + serial old garbage collection combination
***************
Parallel garbage collector: parallel scavenge (new generation), parallel old (old generation)
Features: During garbage collection, the user thread stops and there can be multiple garbage collection threads
Application scenario: parallel scavenge is a throughput-first garbage collector, which provides parameters to control the maximum pause time and throughput
Related parameters
-XX: + UseParallelGC: use parallel scavenge + parallel old garbage collector combination
-XX: + UseParallelOldGC: use parallel scavenge + parallel old garbage collector combination, this parameter java14 will be disabled and may be removed in the future
Java HotSpot(TM) 64-Bit Server VM warning: Option UseParallelOldGC was deprecated
in version 14.0 and will likely be removed in a future release.
-XX: ParallelGCThreads: set the number of concurrent recycling threads
-XX: MaxGCPauseMillis: the maximum pause milliseconds, if the heap memory is too large, the pause time during garbage collection may exceed the set value
-XX: GCTimeRatio: Throughput, the default is 99, and the maximum use of 1% of the time to collect garbage
-XX: + UseAdaptiveSizePolicy: automatically adjust the size of the young generation (-Xmn), the ratio of eden to survivor (-XX: SurvivorRatio) and other parameters
***************
Concurrent garbage collector: G1 (new generation, old generation)
Features: During garbage collection, user threads can work, only a short pause, there can be multiple garbage collection threads
Application scenario: suitable for low pause scenarios, compared with the parallel scavenge + parallel old combination, the pause time of G1 is more controllable
Related parameters
-XX: + UseG1GC: use G1 garbage collector
**********************
Memory parameter setting: Under normal circumstances, using the default parameters can meet the performance requirements
***************
Common parameters
-Xms: set the minimum value of the heap
-Xmx: set the maximum value of the heap
-XX: NewSize: set the minimum value of the new generation
-XX: MaxNewSize: set the maximum value of the new generation
-Xmn: Set the new generation size, which is equivalent to setting NewSize and MaxNewSize to the same value
-XX: NewRatio: set the ratio of the new generation to the old generation, the default is 2, that is, the new generation: old generation = 1: 2
-XX: SurvivorRatio: set the ratio of eden and survivor, the default is 8, that is eden: survivor = 8: 1
***************
Optimization measures
Set a reasonable initial heap size, or set the maximum and minimum heap size to the same, to prevent heap memory scaling from affecting performance;
Adjust the memory size of the old generation to avoid triggering full gc frequently;
Adjust the size of the young generation memory to avoid frequent triggering of the young generation gc, resulting in a large number of objects entering the old generation;
Use parallel or concurrent garbage collectors for garbage collection ;
Consider using a high version of the jdk, the stable release of the high version of the jdk generally has higher performance