From: http://winco304.blog.163.com/blog/static/94283891201042011353424/
Common configuration examples
Heap size settings
The maximum heap size in JVM has three restrictions: the data model (32-BT or 64-bit) of the relevant operating system; the available virtual memory limit of the system; and the available physical memory limit of the system. generally, 32-bit systems are limited to Gbps ~ 2G; 64 for the operating system does not limit the memory. I tested in Windows Server 2003, 1478 GB physical memory, and jdk5.0. The maximum value is MB.
Typical settings:
Java-xmx3550m-xms3550m-xmn2g-xss128k
-Xmx3550m: sets the JVM maximum available memory to 3550 MB.
-Xms3550m: sets JVM to prompt the memory to 3550m. This value can be set to the same as-xmx to avoid JVM re-allocating memory after each garbage collection.
-Xmn2g: Set the young generation to 2 GB. total heap size = size of the young generation + size of the old generation + size of the persistent generation. the permanent generation usually has a fixed size of 64 m, so after the young generation is increased, the size of the old generation will be reduced. this value has a great impact on the system performance. Sun officially recommends setting it to 3/8 of the total heap.
-Xss128k: Set the stack size of each thread. after JDK, the stack size of each thread is 1 MB, and the previous stack size of each thread is K. the memory size required for more application threads is adjusted. reduce this value to generate more threads in the same physical memory. however, the operating system still has a limit on the number of threads in a process. It cannot be generated infinitely. The experience is between 3000 and ~ About 5000.
Java-xmx3550m-xms3550m-xss128k-XX: newratio = 4-XX: Export vorratio = 4-XX: maxpermsize = 16 m-XX: maxtenuringthreshold = 0
-XX: newratio = 4: Set the ratio of the young generation (including Eden and two region vor regions) to the old generation (excluding the permanent generation ). if this parameter is set to 4, the ratio of the young generation to the old generation is, and the young generation accounts for 1/5 of the entire stack.
-XX: Region vorratio = 4: Set the ratio of Eden to region vor in the young generation. if this parameter is set to 4, the ratio of two vor zones to One Eden zone is, and one vor zone accounts for 1/6 of the young generation.
-XX: maxpermsize = 16 M: sets the persistent generation size to 16 m.
-XX: maxtenuringthreshold = 0: sets the maximum age of spam. if it is set to 0, the young generation object directly enters the old generation without going through the VOR area. for applications with many older generations, the efficiency can be improved. if this value is set to a greater value, the young generation object will be copied multiple times in the same vor area, which can increase the survival time of the young generation object, added an overview of being recycled in the young generation.
Collector Selection
JVM provides three options: Serial collector, parallel collector, and concurrent collector, but the serial collector is only applicable to small data volumes. Therefore, the options here are mainly for parallel collector and concurrent collector. by default, jdk5.0 used a serial collector before. To use another collector, you must add the relevant parameters at startup. after jdk5.0, the JVM will judge based on the current system configuration.
Throughput-first parallel collector
As described above, parallel collectors are mainly aimed at reaching a certain throughput and are suitable for Science and Technology and background processing.
Typical Configuration:
Java-xmx3800m-xms3800m-xmn2g-xss128k-XX: + useparallelgc-XX: parallelgcthreads = 20
-XX: + useparallelgc: select the garbage collector as the parallel collector. This configuration is only valid for the young generation. In the preceding configuration, the young generation uses concurrent collection, while the old generation still uses serial collection.
-XX: parallelgcthreads = 20: Number of threads configured for the parallel collector, that is, the number of threads at the same time for garbage collection. It is best to set this value to be equal to the number of processors.
Java-xmx3550m-xms3550m-xmn2g-xss128k-XX: + useparallelgc-XX: parallelgcthreads = 20-XX: + useparalleloldgc
-XX: + useparalleloldgc: configure the garbage collection mode for the old generation to parallel collection. jdk6.0 supports parallel collection for the old generation.
Java-xmx3550m-xms3550m-xmn2g-xss128k-XX: + useparallelgc-XX: maxgcpausemillis = 100
-XX: maxgcpausemillis = 100: sets the maximum time for garbage collection for each young generation. If this time cannot be met, JVM automatically adjusts the size of the young generation to meet this value.
Java-xmx3550m-xms3550m-xmn2g-xss128k-XX: + useparallelgc-XX: maxgcpausemillis = 100-XX: + useadaptivesizepolicy
-XX: + useadaptivesizepolicy: After this option is set, the parallel collector automatically selects the ratio of the young generation zone and the corresponding region vor zone, to achieve the minimum corresponding time or collection frequency specified by the target system, we recommend that you enable the parallel collector all the time.
Concurrent collectors with response time precedence
As described above, the concurrent collector is mainly used to ensure the system response time and reduce the pause time during garbage collection. It is suitable for application servers and telecom fields.
Typical Configuration:
Java-xmx3550m-xms3550m-xmn2g-xss128k-XX: parallelgcthreads = 20-XX: + useconcmarksweepgc-XX: + useparnewgc
-XX: + useconcmarksweepgc: set the age for concurrent collection. after this is configured in the test, the configuration of-XX: newratio = 4 is invalid. The reason is unknown. therefore, it is best to set the size of the young generation with-xmn.
-XX: + useparnewgc: Set the young generation to collect data in parallel. It can be used together with CMS for collection. jdk5.0 or later. JVM will set this value based on the system configuration, so you do not need to set this value.
Java-xmx3550m-xms3550m-xmn2g-xss128k-XX: + useconcmarksweepgc-XX: cmsfullgcsbeforecompaction = 5-XX: + usecmscompactatfullcollection
-XX: cmsfullgcsbeforecompaction: Because the concurrent collector does not compress and tidy up the memory space, "fragments" are generated after running for a period of time, reducing the running efficiency. this value sets the number of GC operations to compress and sort out the memory space.
-XX: + usecmscompactatfullcollection: Enable compression for older generations. This may affect performance, but can eliminate fragments.
Auxiliary Information
JVM provides a large number of command line parameters and prints information for debugging. The main features are as follows:
-XX: + printgc
Output Format: [GC 118250 K-> 113543 K (130112 K), 0.0094143 secs]
[Full GC 121376 K-> 10414 K (130112 K), 0.0650971 secs]
-XX: + printgcdetails
Output Format: [GC [defnew: 8614 K-> 781 K (9088 K), 0.0123035 secs] 118250 K-> 113543 K (130112 K), 0.0124633 secs]
[GC [defnew: 8614 K-> 8614 K (9088 K), 0.0000665 secs] [tenured: 112761 K-> 10414 K (121024 K ), 0.0433488 secs] 121376 K-> 10414 K (130112 K), 0.0436268 secs]
-XX: + printgctimestamps-XX: + printgc: printgctimestamps can be used together with the above two
Output Format: 11.851: [GC 98328 K-> 93620 K (130112 K), 0.0082960 secs]
-XX: + printgcapplicationconcurrenttime: print the execution time of the program that is not interrupted before each garbage collection. It can be used together with the above
Output Format: application time: 0.5291524 seconds
-XX: + printgcapplicationstoppedtime: Specifies the pause time of the program during garbage collection. It can be used together with the above
Output Format: total time for which application threads were stopped: 0.0468229 seconds
-XX: printheapatgc: prints detailed stack information before and after GC.
Output Format:
34.702: [GC {heap before GC invocations = 7:
Def New Generation total 55296 K, used 52568 K [0x1ebd0000, 0x227d0000, 0x227d0000)
Eden space 49152 K, 99% used [0x1ebd0000, 0x21bce430, 0x21bd0000)
From space 6144 K, 55% used [0x221d0000, 0x22527e10, 0x227d0000)
To space 6144 K, 0% used [0x21bd0000, 0x21bd0000, 0x221d0000)
Tenured generation total 69632 K, used 2696 K [0x227d0000, 0x26bd0000, 0x26bd0000)
The space 69632 K, 3% used [0x227d0000, 0x22a720f8, 0x22a72200, 0x26bd0000)
Compacting perm Gen total 8192 K, used 2898 K [0x26bd0000, 0x273d0000, 0x2abd0000)
The space 8192 K, 35% used [0x26bd0000, 0x26ea4ba8, 0x26ea4c00, 0x273d0000)
RO space 8192 K, 66% used [0x2abd0000, 0x2b12bcc0, 0x2b12be00, 0x2b3d0000)
RW space 12288 K, 46% used [0x2b3d0000, 0x2b972060, 0x2b972200, 0x2bfd0000)
34.735: [defnew: 52568 K-> 3433 K (55296 K), 0.0072126 secs] 55264 K-> 6615 K (124928 K) heap after GC invocations = 8:
Def New Generation total 55296 K, used 3433 K [0x1ebd0000, 0x227d0000, 0x227d0000)
Eden space 49152 K, 0% used [0x1ebd0000, 0x1ebd0000, 0x21bd0000)
From space 6144 K, 55% used [0x21bd0000, 0x21f2a5e8, 0x221d0000)
To space 6144 K, 0% used [0x221d0000, 0x221d0000, 0x227d0000)
Tenured generation total 69632 K, used 3182 K [0x227d0000, 0x26bd0000, 0x26bd0000)
The space 69632 K, 4% used [0x227d0000, 0x22aeb958, 0x22aeba00, 0x26bd0000)
Compacting perm Gen total 8192 K, used 2898 K [0x26bd0000, 0x273d0000, 0x2abd0000)
The space 8192 K, 35% used [0x26bd0000, 0x26ea4ba8, 0x26ea4c00, 0x273d0000)
RO space 8192 K, 66% used [0x2abd0000, 0x2b12bcc0, 0x2b12be00, 0x2b3d0000)
RW space 12288 K, 46% used [0x2b3d0000, 0x2b972060, 0x2b972200, 0x2bfd0000)
}
, 0.0757599 secs]
-Xloggc: Filename: Used in conjunction with the above several items to record relevant log information to the file for analysis.
Common configuration Summary
Heap settings
-XMS: initial heap size
-Xmx: Maximum heap size
-XX: newsize = N: Set the young generation size
-XX: newratio = N: Ratio of the young generation to the old generation. for example, if the value is 3, the ratio of the young generation to the old generation is. The young generation accounts for 1/4 of the young generation and the old generation.
-XX: Ratio vorratio = N: Ratio of the Eden zone in the young generation to the two region vor zones. note that there are two vor zones. for example, 3 indicates EDEN: Primary vor =. One primary vor zone accounts for 1/5 of the young generation.
-XX: maxpermsize = N: sets the persistent generation size.
Collector settings
-XX: + useserialgc: sets the serial collector.
-XX: + useparallelgc: set parallel collectors
-XX: + useparalledloldgc: sets the parallel elder generation collector.
-XX: + useconcmarksweepgc: sets the concurrent collector.
Garbage collection statistics
-XX: + printgc
-XX: + printgcdetails
-XX: + printgctimestamps
-Xloggc: Filename
Parallel collector settings
-XX: parallelgcthreads = N: Number of CPUs used for parallel collector collection. Number of parallel collection threads.
-XX: maxgcpausemillis = N: sets the maximum pause time for parallel collection.
-XX: gctimeratio = N: Set the percentage of the garbage collection time to the running time. The formula is 1/(1 + n)
Concurrent collector settings
-XX: + cmsincrementalmode: Set to incremental mode. applicable to single CPU.
-XX: parallelgcthreads = N: set the number of CPUs used by the concurrent collector when the young generation collects data in parallel mode. The number of parallel collection threads.
Optimization Summary
Young Generation Size Selection
Applications with priority over Response Time: set as large as possible until it is close to the minimum response time limit of the system (based on the actual situation ). in this case, the collection frequency of the young generation is also the smallest. at the same time, reduce the number of objects that reach the old generation.
Throughput-first applications: as large as possible, it may reach the Gbit level. Because there is no requirement on the response time, garbage collection can be performed in parallel, which is generally suitable for applications with more than 8 CPUs.
Old Generation Size Selection
Response time-first application: The concurrency collector is used in the old generation. Therefore, you need to set the response time carefully. Generally, you need to consider the concurrency session rate and session duration parameters. if the heap setting is small, it may cause memory fragmentation, high recovery frequency, and application suspension. The traditional mark clearing method is used. If the heap is large, it takes a long time to collect. for the optimal solution, you generally need to obtain the following data:
Concurrent garbage collection information
Number of persistent generation concurrent collection times
Traditional GC Information
Ratio of time spent on the recovery of young and old generations
Reducing the time spent by the young and old generations will generally improve application efficiency.
Throughput-first applications: Generally, throughput-first applications have a large young generation and a small old generation. the reason is that most of the short-term objects can be recycled as much as possible to reduce the medium-term objects, while the old generation will store the long-term surviving objects as much as possible.
Fragmentation problem caused by a small heap
The heap is not compressed because the concurrent collector of the old generation uses tags to clear algorithms. when the collector recycles the object, it merges the adjacent space and assigns it to a large object. however, when the heap is empty for an hour and runs for a period of time, "fragments" will appear. If the concurrent collector cannot find enough space, the concurrent collector will stop, then, we use the traditional mark and clear method to recycle it. if "fragment" appears, you may need to configure it as follows:
-XX: + usecmscompactatfullcollection: Enable compression for the old generation when the concurrent collector is used.
-XX: cmsfullgcsbeforecompaction = 0: When the preceding configuration is enabled, how many full GC times are set here to compress the old generation?
In the same project, there are two classes. There are only a few changes in these two classes, but the most important for is not changed at all, however, when I run these two programs separately, a very serious problem occurs. One program loops fast and one loop slows down. what is this ~??? After searching for a long time, I did not think of why, because the root of the program change does not affect the speed of my loop, but the results are quite different, one loop can be completed in about one minute, but the other takes six or seven minutes, which is not a matter of data processing. there are two completely identical loops, and there is basically no problem in the code. I can't ask for help from my colleagues, but my colleagues also felt very strange after reading the code for more than an hour. Finally, my colleagues asked me to clean up and shut down and restart. when I got to be obedient, I went along with what my colleagues meant, but at the time of shutdown, he suddenly said whether it was a memory problem, and I thought of nothing, it may also be a memory issue, because kuaidi can provide 1 GB of memory before running the program. However, I didn't seem to have set the memory, the machine is up. Let's take a look at this idea. The result is very important. The slow one is not on memory, but the JVM default memory is used when the program runs. my preliminary analysis is that the memory is too small, and the memory used by my program is stuck on the memory side opened by JVM, so as not to overflow. it takes most of the time to run the program. The GC is called, which leads to two different efficiency issues in the same cycle ~!
By the way, the memory usage method is also posted:
Public static string getmemusage (){
Long free = java. Lang. runtime. getruntime (). freememory ();
Long Total = java. Lang. runtime. getruntime (). totalmemory ();
Stringbuffer Buf = new stringbuffer ();
Buf. append ("[mem: used"). append (total-free)> 20)
. Append ("m free"). append (free> 20)
. Append ("m total"). append (total> 20). append ("m]");
Return Buf. tostring ();
}
Google, it is probably said that the JVM is like this to operate the memory:
Heap and non-heap memory
According to the official statement: "A Java virtual machine has a heap. The heap is the runtime data area, and the memory of all class instances and arrays is allocated from this place. the heap is created when the Java Virtual Machine is started. "" memory outside the heap in JVM is called non-heap memory )". JVM manages two types of memory: heap and non-heap. in short, heap is the memory available for Java code and is reserved for developers; non-heap is reserved for JVM, so in the Method Area, the memory required for JVM internal processing or optimization (such as the code cache after JIT compilation), and the structure of each class (such as the number of runtime pools, fields, and method data) and the method and constructor Code are in non-heap memory.
Heap Memory Allocation
The initial memory allocated by JVM is specified by-XMS. The default value is 1/64 of the physical memory. The maximum memory allocated by JVM is specified by-xmx. The default value is 1/4 of the physical memory. by default, when the free heap memory is less than 40%, the JVM will increase the heap until the maximum limit of-xmx. When the free heap memory is greater than 70%, the JVM will reduce the minimum limit of heap until-XMS. therefore, the server generally sets-XMS and-xmx to be equal to each other to avoid adjusting the heap size after each GC.
Non-heap memory allocation
JVM uses-XX: permsize to set the non-heap memory initial value. The default value is 1/64 of the physical memory. The maximum non-heap memory size is set by XX: maxpermsize. The default value is 1/4 of the physical memory.
JVM memory limit (maximum)
First, JVM memory is limited by the actual maximum physical memory. If the physical memory is infinitely large, the maximum JVM memory has a great relationship with the operating system. simply put, although the 32-bit processor has a controllable memory space of 4 GB, the specific operating system will impose a limit, this limit is generally 2 GB-3 GB (1.5 GB-2 GB in windows and 2 GB-3 GB in Linux ), the 64-bit and above processors will not be limited.
JVM Memory Optimization
1. heap configuration and garbage collection Java heap are divided into three zones: young, old, and permanent. young stores the just-instantiated object. when the area is filled up, GC will move the object to the old area. the permanent area stores the reflection object. This section does not discuss this area. JVM heap allocation can be set using the-x parameter,
-XMS
Initial heap size
-Xmx
Java heap maximum
-Xmn
Heap size of young generation
JVM has two GC threads. the first thread is responsible for revoking heap's young zone. the second thread traverses the heap when the heap is insufficient and upgrades the young area to the older area. the size of the older area is equal to-xmx minus-xmn, and the value of-XMS cannot be set too large, because the second thread is forced to run will reduce JVM performance.
Why does GC occur frequently in some programs? There are the following reasons:
L The program calls system. GC () or runtime. GC ().
L some middleware software calls their own GC methods. In this case, you need to set parameters to disable these GC methods.
L Java heap is too small. Generally, the default heap value is very small.
L frequently instantiate objects and release objects. Save and reuse objects as much as possible, such as using stringbuffer () and string ().
If you find that the remaining heap space after each GC is 50% of the total space, it indicates that your heap is healthy. many Java programs on the server end have a better 65% free space after each GC. experience:
1. It is best to set-XMS and-xmx to the same value as the server-side JVM. To optimize GC, it is best to set-xmn value to approximately 1/3 of-xmx [2].
2. It is best to run a GC every 10 to 20 seconds for a GUI program to be completed within half a second.
Note:
1. although increasing the heap size will reduce the GC frequency, it also increases the time of each GC. during GC running, all user threads will be suspended, that is, Java applications will not do any work during GC.
2. The heap size does not determine the memory usage of the process. The memory usage of the process must be greater than the value defined by-xmx, Because Java allocates memory for other tasks, such as the stack of each thread.
2. stack settings
Each thread has its own stack.
-XSS
Stack size of each thread
The size of the stack limits the number of threads. If the stack is too large, the memory may leak.-The XSS parameter determines the stack size, for example,-xss1024k. If the stack is too small, the stack may leak.
3. hardware environment
The hardware environment also affects GC efficiency, such as the machine type, memory, swap space, and number of CPUs.
If your program needs to frequently create many transient objects, it will lead to frequent jvm gc. In this case, you can increase the machine memory to reduce the use of swap space [2].
4.4 Gc Types
The first type is single-threaded GC, which is also the default GC. This GC is applicable to single-CPU machines.
Type 2: throughput GC, a multi-thread GC, applicable to multi-CPU programs that use a large number of threads. The second type of GC is similar to the first type of GC. The difference is that the GC is collected
The young area is multi-threaded, but in the old area and the first one, the GC is still started using the single thread.-XX: + useparallelgc parameter.
The third type: Concurrent low pause GC, similar to the first type, suitable for multiple CPUs, and requires shorter time for program stagnation due to GC. This GC can be in the old area
At the same time, run the application.-XX: + useconcmarksweepgc parameter to start the GC.
Type 4: Incremental low pause GC, applicable to the requirement to shorten the time of program stagnation caused by GC. This GC can be recycled in the young area at the same time.
Some old objects.-The xincgc parameter starts the GC.
For details about the four types of GC, refer to [3].
References:
1. JVM tuning.
Http://www.caucho.com/resin-3.0/performance/jvm-tuning.xtp#garbage-collection
2. Performance Tuning Java: tuning steps
Http://h21007.www2.hp.com/dspp/tech/tech_TechDocumentDetailPage_IDX/1,1701,1604,00.html
3. tuning garbage collection with the 1.4.2 javatm virtual machine.
Http://java.sun.com/docs/hotspot/gc1.4.2/