Heap settings
-Xmx3550m: Set the maximum heap memory of JVM to 3550M.
-Xms3550m: Set the initial heap memory of JVM to 3550M. This value can be set the same as -Xmx to avoid the JVM reallocating memory every time the garbage collection is complete.
-Xss128k: Sets the stack size of each thread. After JDK5.0, each thread stack size will be 1M, and before that, each thread stack size will be 256K. It should be adjusted according to the required memory size of the application's thread. In the same physical memory, reducing this value can generate more threads. However, the operating system still has a limit on the number of threads in a process and cannot be generated infinitely, with experience values ranging from about 3000 to 5000.
-Xmn2g: Set the heap memory young generation size to 2G. The entire heap memory size = young generation size + old generation size + persistent generation size. The permanent generation is generally fixed in size of 64m, so after increasing the younger generation, the size of the older generation will be reduced. This value has a great impact on system performance, and Sun officially recommends configuration as 3/8 of the entire heap.
-XX:PermSize=256M: Set the initial value of the heap memory persistent generation to 256M. (It seems to be the initialization parameters of IDEs such as Eclipse)
-XX:MaxNewSize=size: The maximum value that the newly generated object can occupy memory.
-XX:MaxPermSize=512M: Set the maximum value of the persistent generation to 512M.
-XX:NewRatio=4: Set the ratio of the heap memory young generation (including Eden and two Survivor regions) to the heap memory old generation (excluding the persistent generation). If set to 4, the ratio of the younger generation to the older generation is 1:4.
-XX:SurvivorRatio=4: Set the ratio of the Eden area to the Survivor area in the young generation of heap memory. Set to 4, the ratio of the two Survivor zones (the JVM heap memory young generation has 2 Survivor zones by default) to one Eden zone is 2:4, and one Survivor zone accounts for 1/6 of the entire young generation.
-XX:MaxTenuringThreshold=7: means that an object is placed in the elderly if it moves 7 times in the rescue space (Survivor area) and has not been recycled.
If set to 0, the young generation objects do not pass through the Survivor area and directly enter the old generation. For more applications in the old generation, this can improve efficiency.
If this value is set to a larger value, the young generation object will be copied multiple times in the Survivor area, which can increase the survival time of the object in the young generation and increase the probability that the object will be recycled in the young generation.
Recycler selection
JVM gives three options: serial collector, parallel collector, and concurrent collector, but serial collector is only suitable for small data volumes, so the choice here is mainly for parallel collector and concurrent collector.
By default, JDK5.0 used serial collectors before. If you want to use other collectors, you need to add corresponding parameters at startup. After JDK5.0, the JVM will make intelligent judgments based on the current system configuration.
Serial Collector
-XX:+UseSerialGC: Set the serial collector
Parallel collector (throughput priority)
-XX:+UseParallelGC: Select the garbage collector as the parallel collector. This configuration is only valid for younger generations. That is, under the above configuration, the younger generation uses concurrent collection, while the older generation still uses serial collection.
-XX:ParallelGCThreads=20: Configure the number of threads of the parallel collector, that is, how many threads are garbage collected at the same time. This value is best configured to be equal to the number of processors.
-XX:+UseParallelOldGC: Configure the old generation garbage collection method to parallel collection. JDK6.0 supports parallel collection for older generations.
-XX:MaxGCPauseMillis=100: Sets the maximum time (in milliseconds) for each young generation garbage collection. If this time cannot be met, the JVM will automatically adjust the size of the young generation to meet this value.
-XX:+UseAdaptiveSizePolicy: After setting this option, the parallel collector will automatically select the size of the young generation area and the corresponding Survivor area ratio to achieve the minimum response time or collection frequency specified by the target system.
This parameter is recommended to be turned on when using the parallel collector.
Concurrent collector (response time is preferred)
-XX:+UseParNewGC: Set young generation to concurrent collection. Can be used simultaneously with CMS collection. JDK5.0 or above, the JVM will set it by itself according to the system configuration, so there is no need to set this value again.
CMS, full name ConcurrentLowPauseCollector, is a new gc algorithm introduced in the later version of jdk1.4. It has been further improved in jdk5 and jdk6. Its main suitable scenario is that the importance of response time is greater than the requirements for throughput, can withstand garbage collection threads and application threads sharing processor resources, and there are relatively many long-lifetime objects in the application. CMS is used for recycling of tenuredgeneration, that is, recycling of the elderly. The goal is to minimize the application pause time, reduce the chance of FullGC occurrence, and use garbage collection threads concurrent with application threads to mark and clear the elderly.
-XX:+UseConcMarkSweepGC: Set the old generation to concurrent collection. After configuring this in the test, the configuration of -XX:NewRatio=4 is invalid. Therefore, it is best to set the size of the young generation with -Xmn at this time.
-XX:CMSFullGCsBeforeCompaction=: Since the concurrent collector does not compress or organize the memory space, "fragments" will be generated after running for a period of time, which reduces the operation efficiency. This parameter sets the memory space to be compressed and organized after running FullGC.
-XX:+UseCMSCompactAtFullCollection: Turn on compression for older generations. May affect performance, but can eliminate memory fragmentation.
-XX:+CMSIncrementalMode: Set to incremental collection mode. Generally applicable to single CPU situation.
-XX:CMSInitiatingOccupancyFraction=70: means that when the space of the old generation reaches 70%, CMS will be started to be executed to ensure that the old generation has enough space to accept objects from the young generation.
Note: If you use two garbage collectors, throughputcollector and concurrentlowpausecollector, you need to have a proper high memory size to prepare for multi-threading.
other
-XX:+ScavengeBeforeFullGC: The next generation of GC takes precedence over FullGC.
-XX:-DisableExplicitGC: Inhibit calling System.gc(), but the JVM's gc is still valid.
-XX:+MaxFDLimit: Maximize the limit on the number of file descriptors.
-XX:+UseThreadPriorities: Enable the local thread priority API, even if java.lang.Thread.setPriority() takes effect, it will be invalid.
-XX:SoftRefLRUPolicyMSPerMB=0: The "soft reference" object can survive 0 milliseconds after the last access (default is 1 second).
-XX:TargetSurvivorRatio=90: Allows 90% of Survivor space to be occupied (default is 50%). Increase the usage rate of Survivor - if you exceed it, you will try garbage collection.
Auxiliary information
-XX:-CITime: Printing consumes time in JIT compilation
-XX:ErrorFile=./hs_err_pid.log: Save error log or data into the specified file
-XX:-ExtendedDTraceProbes: Turn on the solaris-specific dtrace probe
-XX:HeapDumpPath=./java_pid.hprof: Specifies the path or file name when exporting heap information
-XX:-HeapDumpOnOutOfMemoryError: Export relevant information in the heap at this time when the memory overflow is first encountered.
-XX:OnError=";": Run custom command after a fatal ERROR appears
-XX:OnOutOfMemoryError=";": Execute custom command when encountering memory overflow for the first time
-XX:-PrintClassHistogram: Print columnar information of class instance after encountering Ctrl-Break, the same as jmap-histo function
-XX:-PrintConcurrentLocks: Print related information about concurrent locks after encountering Ctrl-Break, the same as jstack-l function
-XX:-PrintCommandLineFlags: Print the mark that appears in the command line
-XX:-PrintCompilation: Print relevant information when a method is compiled
-XX:-PrintGC: Print relevant information every time GC
-XX:-PrintGCDetails: Print details every time GC
-XX:-PrintGCTimeStamps: Print the timestamp of each GC
-XX:-TraceClassLoading: Track the loading information of the class
-XX:-TraceClassLoadingPreorder: Tracks the loading information of all classes referenced to
-XX:-TraceClassResolution: Tracking constant pool
-XX:-TraceClassUnloading: Tracking class unloading information
-XX:-TraceLoaderConstraints: Tracking related information about class loader constraints
JVM service tuning practice
Server: 8cup, 8Gmem
eg
java-Xmx3550m-Xms3550m-Xss128k-XX:NewRatio=4-XX:SurvivorRatio=4-XX:MaxPermSize=16m-XX:MaxTenuringThreshold=0
Tuning plan:
-Xmx5g: Set the maximum available memory of JVM to 5G.
-Xms5g: Set the initial JVM memory to 5G. This value can be set the same as -Xmx to avoid the JVM reallocating memory every time the garbage collection is complete.
-Xmn2g: Set the size of the young generation to 2G. The entire heap memory size = young generation size + old generation size + persistent generation size. The permanent generation is generally fixed in size of 64m, so after increasing the younger generation, the size of the older generation will be reduced. This value has a great impact on system performance, and Sun officially recommends configuration as 3/8 of the entire heap.
-XX:+UseParNewGC: Set the young generation to parallel collection. Can be used simultaneously with CMS collection. JDK5.0 or above, the JVM will set it by itself according to the system configuration, so there is no need to set this value again.
-XX:ParallelGCThreads=8: The number of threads to configure the parallel collector, that is, how many threads are garbage collected at the same time. This value is best configured to be equal to the number of processors.
-XX:SurvivorRatio=6: Sets the size ratio of the Eden area and the Survivor area in the young generation. According to experience, the ratio of the two Survivor zones to one Eden zone is 2:6, and one Survivor zone accounts for 1/8 of the entire young generation.
-XX:MaxTenuringThreshold=30: Set the maximum age of garbage (number of times). If set to 0, the younger generation objects will directly enter the older generation without going through the Survivor area. For more applications in the older generation, efficiency can be improved. If this value is set to a larger value, the young generation object will copy multiple times in the Survivor area, which can increase the survival time of the object and the younger generation and increase the probability that it will be recycled in the younger generation. Set to 30 means that an object is placed in the old generation if it moves 30 times in the Survivor space and has not been recycled.
-XX:+UseConcMarkSweepGC: Set the old generation to concurrent collection. After testing and configuring this parameter, the parameter -XX:NewRatio=4 will be invalid. Therefore, it is best to set the size of the young generation with -Xmn, so this parameter is not recommended.
References - Generation of JVM heap memory
The heap memory of virtual machines is divided into three generations: Young Generation, Old Generation and Permanent Generation. Among them, the persistent generation mainly stores the Java class information, which has little to do with the Java objects to be collected by the garbage collector. Therefore, the division between the younger generation and the older generation has a greater impact on garbage collection.
Young Generation
All newly generated objects are first placed in the younger generation. The goal of the younger generation is to collect objects with short life cycles as quickly as possible. Young generations are divided into three areas. One Eden area, two Survivor areas (generally).
Most objects are generated in the Eden area. When the Eden area is full, the still surviving objects will be copied to the Survivor area (one of the two). When one Survivor area is full, the surviving objects in this area will be copied to another Survivor area. When the other Survivor area is also full, the objects copied from the previous Survivor area and still surviving at this time will be copied to the "Tenured Area".
It should be noted that the two Survivor areas are symmetrical and have no relationship in sequence, so there may be objects copied from the Eden area and objects copied from the other Survivor area in the same Survivor area at the same time; and only objects copied to the old area (relative). Moreover, there is always one in the Survivor area that is empty. In special cases, according to program needs, the Survivor area can be configured in multiple (more than two), which can increase the time of existence of objects in the younger generation and reduce the possibility of being placed in the older generation.
Older generation
Objects that still survive after N (configurable) garbage collections in the younger generation will be placed in the older generation. Therefore, it can be considered that the elderly people store objects with longer life cycles.
Persistent generation
Used to store static data, such as JavaClass, Method, etc. Persistent generation has no significant impact on garbage collection, but some applications may dynamically generate or call some Classes, such as Hibernate, etc. At this time, a relatively large persistent generation space needs to be set up to store these types that are dynamically increased during operation. The persistent generation size is set by -XX:MaxPermSize=.
Summarize
The above is the entire content of this article on optimizing Java virtual machine (jvm tuning). I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!