JVM fast tune Liuyou manuals: JVM parameter setting and analysis

Whether or YGC Full GC, GC process will cause the program to run is interrupted, the right choice of different GC policy, adjust parameters JVM, GC, and running can greatly reduce the GC work, which led to the interruption aspects problem, then appropriate improve the efficiency of Java programs. However adjust GC is an extremely complicated process, because each program has different characteristics, such as: web GUI program and there is a great difference (Web can be an appropriate pause, but a pause GUI client is not acceptable), but also due to the run a different configuration on each machine (the main cup number of different memory), so the GC will be different types of use (see how to choose and how to choose the type of GC). This article will focus on introduction JVM, set some important parameters of the GC to improve system performance.

See previous articles JVM memory components and GC content: JVM memory composed of GC & memory allocation strategy

Meaning JVM parameters

See case study examples

parameter name meaning Defaults
-Xms Initial heap size Physical memory 1/64 (<1GB) Default (MinHeapFreeRatio parameters may be adjusted) spare heap memory is less than 40%, JVM stack will increase up to a maximum limit of -Xmx.
-Xmx The maximum heap size Physical memory 1/4 (<1GB Default (parameter may be adjusted MaxHeapFreeRatio) of free memory heap is greater than 70%, JVM will reduce the minimum limit of the stack until -Xms
-Xmn The size of the young generation 1.4or lator Note: The size here is (eden + 2 survivor space) and displayed jmap -heap New gen are different.

The young generation throughout the heap size = size + size + old generation of permanent generation size.

After the increase in the young generation, will reduce the size of the old generation. This value greater impact on system performance, Sun official recommended configuration for the entire heap 3/8

-XX: NewSize | Set the young generation size (for 1.3 / 1.4) |

-XX: MaxNewSize | younger generation of the maximum value (for 1.3 / 1.4) |

-XX: PermSize | disposed permanent generation (perm gen) Initial value | physical memory 1/64

-XX: MaxPermSize | Setting maximum permanent generation | physical memory 1/4

-Xss | stack size for each thread || JDK5.0 after each thread stack size is 1M, before each thread stack size is 256K application required more threads of memory size is adjusted in the same physical memory,. reducing this value can generate more threads, but the number of operating system threads within a process still limited, not unlimited generation, experience in 3000 and 5000.

Generally small application, if the stack is not very deep, it should be large enough for use Application recommendations 128k 256k. This option is relatively large impact on performance requires rigorous testing. (Principals) and threadstacksize options are explained very similar to the official document seems to be no explanation, there is such a word in the forum: "- Xss is translated in a VM flag named ThreadStackSize" generally set this value on it.

-XX:ThreadStackSize|Thread Stack Size||(0 means use default stack size) [Sparc: 512; Solaris x86: 320 (was 256 prior in 5.0 and earlier); Sparc 64 bit: 1024; Linux amd64: 1024 (was 0 in 5.0 and earlier); all others 0.]

-XX: NewRatio | younger generation (including Eden and two Survivor areas) and the old generation ratio (excluding the permanent generation) || -XX: NewRatio = 4 represents the young generation and the old generation occupied ratio of 1: 4, the young generation accounts for 1/5 of the entire stack

Xms = Xmx case and Xmn set, which parameter settings are not necessary.

-XX: SurvivorRatio | Eden area and size ratios region || Survivor set to 8, the ratio of the two regions with a Survivor Eden zone is 2: 8, a Survivor young generation area 1/10 of the total

-XX: LargePageSizeInBytes | memory page size can not be set too large, will affect the size of Perm || = 128m

-XX: + UseFastAccessorMethods | rapid optimization of the original type of

-XX: + DisableExplicitGC | Close System.gc () || This parameter requires rigorous testing

-XX:.. MaxTenuringThreshold | garbage maximum age || If set to 0, then, the younger generation of the object without Survivor areas, directly into the old generation to the old generation of more applications can improve efficiency if this value is set to a higher value, the young generation objects will be copied many times in Survivor areas, which can increase the object and then the young generation of survival time, increase the probability of recovery in the young generation ie

This parameter is effective only when the serial GC.

-XX: + AggressiveOpts | speed up the compilation

-XX: + UseBiasedLocking | performance improved locking mechanism

-Xnoclassgc | disable garbage collection

-XX:SoftRefLRUPolicyMSPerMB|每兆堆空闲空间中SoftReference的存活时间|1s|softly reachable objects will remain alive for some amount of time after the last time they were referenced. The default value is one second of lifetime per free megabyte in the heap

Invalid byte units using the new generation of Parallel Scavenge GC | PretenureSizeThreshold | much more than objects in the old generation is directly allocated | 0: -XX

Another old generation in the case of direct distribution of a large array of objects, and no external reference object in the array.

-XX: TLABWasteTargetPercent | TLAB percentage of eden region | 1%

-XX: + CollectGen0First | whether YGC FullGC first time | false

Parallel collector parameters

parameter name meaning Defaults
-XX:+UseParallelGC Full GC using parallel MSC (this to be authenticated) Select garbage collector parallel collector. This configuration is effective only for the young generation. That the above arrangement, the young generation concurrent collection, while still using the old generation of the serial collector. (This to be authenticated)
-XX:+UseParNewGC Parallel to collect on behalf of the young set CMS may collect use JDK5.0 above, the JVM may set their own according to the system configuration, it is no need to set this value
-XX:ParallelGCThreads The number of threads in parallel collector This value is equal to the number of processors is preferably arranged equally applicable to CMS
-XX:+UseParallelOldGC Old generation garbage collection for parallel collection (Parallel Compacting) This is the parameter options appear JAVA 6
-XX: MaxGCPauseMillis Every time the young generation garbage collection is the maximum time (maximum pause time) If you can not meet this time, JVM will automatically adjust the size of the young generation to meet this value.

-XX: + UseAdaptiveSizePolicy automatically select the young generation area size and the corresponding rate Survivor areas
this option is set, the parallel collector will automatically select the young generation area size and the corresponding rate Survivor areas, in order to achieve a minimum predetermined time corresponding to the target system or to collect frequency, this value is recommended when the parallel collector, has been opened.
-XX: GCTimeRatio | || garbage collection formula representing the percentage of time the program was run time. 1 / (n-+. 1)
-XX: + ScavengeBeforeFullGC | before Full GC call YGC | true | Do young generation GC prior to a full GC (Introduced in 1.4.1.).

CMS parameters

parameter name meaning Defaults
-XX:+UseConcMarkSweepGC Use CMS garbage collection After this test configuration, -XX: NewRatio = 4 configuration fails, for unknown reasons therefore, at this time the size of the young generation is preferably provided with -Xmn ???..
-XX:+AggressiveHeap Is trying to use a lot of physical memory, large memory usage optimization for a long time, and can check the computing resources (memory, number of processors), you need at least 256MB of memory, a large amount of CPU / memory, (in 1.4.1 already on the machine 4CPU showed upgrade)
-XX:CMSFullGCsBeforeCompaction How many times were compressed memory Since the memory space concurrent collector does not compress, organize, it will have a "debris" after a period of operation, so as to reduce the operating efficiency. This value is set to run how many times GC memory space after compression, finishing.
-XX:+CMSParallelRemarkEnabled Lower standstill mark
-XX+UseCMSCompactAtFullCollection In FULL GC when the compression of the older generation CMS is not removable memory, so this is very easy to generate debris, resulting in memory is not enough, therefore, the memory of this time compression will be enabled. Increase this parameter is a good habit. Performance may be affected, but you can remove debris
-XX:+UseCMSInitiatingOccupancyOnly Use manually define the initial definition of CMS began collecting Prohibit hostspot own trigger CMS GC
-XX:CMSInitiatingOccupancyFraction=70 Use cms as garbage collection, CMS began collecting after using 70% 92 In order to ensure that there is promotion failed (see below) error, set the value required to satisfy the following formula CMSInitiatingOccupancyFraction formula
-XX:CMSInitiatingPermOccupancyFraction Perm Gen set trigger number reached the usage ratio 92
-XX:+CMSIncrementalMode Set to incremental mode For single CPU case
-XX:+CMSClassUnloadingEnabled

Supplementary information

parameter name meaning Defaults
-XX:+PrintGC Output format: [GC 118250K-> 113543K (130112K), 0.0094143 secs] [Full GC 121376K-> 10414K (130112K), 0.0650971 secs]
-XX:+PrintGCDetails 输出形式: [GC [DefNew: 8614K->781K(9088K), 0.0123035 secs] 118250K->113543K(130112K), 0.0124633 secs][GC [DefNew: 8614K->8614K(9088K), 0.0000665 secs][Tenured: 112761K->10414K(121024K), 0.0433488 secs] 121376K->10414K(130112K), 0.0436268 secs]
-XX:+PrintGCTimeStamps
-XX:+PrintGC:PrintGCTimeStamps 可与-XX:+PrintGC -XX:+PrintGCDetails混合使用 输出形式:11.851: [GC 98328K->93620K(130112K), 0.0082960 secs]
-XX:+PrintGCApplicationStoppedTime 打印垃圾回收期间程序暂停的时间.可与上面混合使用 输出形式:Total time for which application threads were stopped: 0.0468229 seconds
-XX:+PrintGCApplicationConcurrentTime 打印每次垃圾回收前,程序未中断的执行时间.可与上面混合使用 输出形式:Application time: 0.5291524 seconds
-XX:+PrintHeapAtGC 打印GC前后的详细堆栈信息
-Xloggc:filename 把相关日志信息记录到文件以便分析. 与上面几个配合使用
-XX:+PrintClassHistogram garbage collects before printing the histogram.
-XX:+PrintTLAB 查看TLAB空间的使用情况
XX:+PrintTenuringDistribution 查看每次minor GC后新的存活周期的阈值 Desired survivor size 1048576 bytes, new threshold 7 (max 15)

new threshold 7即标识新的存活周期的阈值为7。

GC性能方面的考虑

对于GC的性能主要有2个方面的指标:吞吐量throughput(工作时间不算gc的时间占总的时间比)和暂停pause(gc发生时app对外显示的无法响应)

  1. Total Heap

    默认情况下,vm会增加/减少heap大小以维持free space在整个vm中占的比例,这个比例由MinHeapFreeRatio和MaxHeapFreeRatio指定。
    一般而言,server端的app会有以下规则:

    • 对vm分配尽可能多的memory;
    • 将Xms和Xmx设为一样的值。如果虚拟机启动时设置使用的内存比较小,这个时候又需要初始化很多对象,虚拟机就必须重复地增加内存。
    • 处理器核数增加,内存也跟着增大。
  2. The Young Generation

    另外一个对于app流畅性运行影响的因素是young generation的大小。young generation越大,minor collection越少;但是在固定heap size情况下,更大的young generation就意味着小的tenured generation,就意味着更多的major collection(major collection会引发minor collection)。
    NewRatio反映的是young和tenured generation的大小比例。NewSize和MaxNewSize反映的是young generation大小的下限和上限,将这两个值设为一样就固定了young generation的大小(同Xms和Xmx设为一样)。
    如果希望,SurvivorRatio也可以优化survivor的大小,不过这对于性能的影响不是很大。SurvivorRatio是eden和survior大小比例。
    一般而言,server端的app会有以下规则:

    • 首先决定能分配给vm的最大的heap size,然后设定最佳的young generation的大小;
    • 如果heap size固定后,增加young generation的大小意味着减小tenured generation大小。让tenured generation在任何时候够大,能够容纳所有live的data(留10%-20%的空余)。

经验&&规则

  • 年轻代大小选择

    • 响应时间优先的应用:尽可能设大,直到接近系统的最低响应时间限制(根据实际情况选择).在此种情况下,年轻代收集发生的频率也是最小的.同时,减少到达年老代的对象.
    • 吞吐量优先的应用:尽可能的设置大,可能到达Gbit的程度.因为对响应时间没有要求,垃圾收集可以并行进行,一般适合8CPU以上的应用.
    • 避免设置过小.当新生代设置过小时会导致:1.YGC次数更加频繁 2.可能导致YGC对象直接进入旧生代,如果此时旧生代满了,会触发FGC.
  • 年老代大小选择

    • 响应时间优先的应用:年老代使用并发收集器,所以其大小需要小心设置,一般要考虑并发会话率和会话持续时间等一些参数.如果堆设置小了,可以会造成内存碎 片,高回收频率以及应用暂停而使用传统的标记清除方式;如果堆大了,则需要较长的收集时间.最优化的方案,一般需要参考以下数据获得:
      并发垃圾收集信息、持久代并发收集次数、传统GC信息、花在年轻代和年老代回收上的时间比例。
    • 吞吐量优先的应用:一般吞吐量优先的应用都有一个很大的年轻代和一个较小的年老代.原因是,这样可以尽可能回收掉大部分短期对象,减少中期的对象,而年老代尽存放长期存活对象.
  • 较小堆引起的碎片问题

    因为年老代的并发收集器使用标记,清除算法,所以不会对堆进行压缩.当收集器回收时,他会把相邻的空间进行合并,这样可以分配给较大的对象.但是,当堆空间较小时,运行一段时间以后,就会出现”碎片”,如果并发收集器找不到足够的空间,那么并发收集器将会停止,然后使用传统的标记,清除方式进行回收.如果出现”碎片”,可能需要进行如下配置:
    -XX:+UseCMSCompactAtFullCollection:使用并发收集器时,开启对年老代的压缩.
    -XX:CMSFullGCsBeforeCompaction=0:上面配置开启的情况下,这里设置多少次Full GC后,对年老代进行压缩

  • 用64位操作系统,Linux下64位的jdk比32位jdk要慢一些,但是吃得内存更多,吞吐量更大

  • XMX和XMS设置一样大,MaxPermSize和MinPermSize设置一样大,这样可以减轻伸缩堆大小带来的压力

  • 使用CMS的好处是用尽量少的新生代,经验值是128M-256M, 然后老生代利用CMS并行收集, 这样能保证系统低延迟的吞吐效率。 实际上cms的收集停顿时间非常的短,2G的内存, 大约20-80ms的应用程序停顿时间

  • 系统停顿的时候可能是GC的问题也可能是程序的问题,多用jmap和jstack查看,或者killall -3 java,然后查看java控制台日志,能看出很多问题。(相关工具的使用方法将在后面的blog中介绍)

  • 仔细了解自己的应用,如果用了缓存,那么年老代应该大一些,缓存的HashMap不应该无限制长,建议采用LRU算法的Map做缓存,LRUMap的最大长度也要根据实际情况设定。

  • 采用并发回收时,年轻代小一点,年老代要大,因为年老大用的是并发回收,即使时间长点也不会影响其他程序继续运行,网站不会停顿

  • JVM参数的设置(特别是 –Xmx –Xms –Xmn -XX:SurvivorRatio -XX:MaxTenuringThreshold等参数的设置没有一个固定的公式,需要根据PV old区实际数据 YGC次数等多方面来衡量。为了避免promotion faild可能会导致xmn设置偏小,也意味着YGC的次数会增多,处理并发访问的能力下降等问题。每个参数的调整都需要经过详细的性能测试,才能找到特定应用的最佳配置。

promotion failed

垃圾回收时promotion failed是个很头痛的问题,一般可能是两种原因产生,第一个原因是救助空间不够,救助空间里的对象还不应该被移动到年老代,但年轻代又有很多对象需要放入救助空间;第二个原因是年老代没有足够的空间接纳来自年轻代的对象;这两种情况都会转向Full GC,网站停顿时间较长。

解决方方案一

第一个原因我的最终解决办法是去掉救助空间,设置-XX:SurvivorRatio=65536 -XX:MaxTenuringThreshold=0即可,第二个原因我的解决办法是设置CMSInitiatingOccupancyFraction为某个值(假设70),这样年老代空间到70%时就开始执行CMS,年老代有足够的空间接纳来自年轻代的对象。

解决方案一的改进方案

又有改进了,上面方法不太好,因为没有用到救助空间,所以年老代容易满,CMS执行会比较频繁。我改善了一下,还是用救助空间,但是把救助空间加大,这样也不会有promotion failed。具体操作上,32位Linux和64位Linux好像不一样,64位系统似乎只要配置MaxTenuringThreshold参数,CMS还是有暂停。为了解决暂停问题和promotion failed问题,最后我设置-XX:SurvivorRatio=1 ,并把MaxTenuringThreshold去掉,这样即没有暂停又不会有promotoin failed,而且更重要的是,年老代和永久代上升非常慢(因为好多对象到不了年老代就被回收了),所以CMS执行频率非常低,好几个小时才执行一次,这样,服务器都不用重启了。

-Xmx4000M -Xms4000M -Xmn600M -XX:PermSize=500M -XX:MaxPermSize=500M -Xss256K -XX:+DisableExplicitGC -XX:SurvivorRatio=1 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:CMSFullGCsBeforeCompaction=0 -XX:+CMSClassUnloadingEnabled -XX:LargePageSizeInBytes=128M -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=80 -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+PrintClassHistogram -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -Xloggc:log/gc.log

CMSInitiatingOccupancyFraction值与Xmn的关系公式

上面介绍了promontion faild产生的原因是EDEN空间不足的情况下将EDEN与From survivor中的存活对象存入To survivor区时,To survivor区的空间不足,再次晋升到old gen区,而old gen区内存也不够的情况下产生了promontion faild从而导致full gc.那可以推断出:eden+from survivor < old gen区剩余内存时,不会出现promontion faild的情况,即:
(Xmx-Xmn)*(1-CMSInitiatingOccupancyFraction/100)>=(Xmn-Xmn/(SurvivorRatior+2)) 进而推断出:

CMSInitiatingOccupancyFraction <=((Xmx-Xmn)-(Xmn-Xmn/(SurvivorRatior+2)))/(Xmx-Xmn)*100 

例如:

当xmx=128 xmn=36 SurvivorRatior=1时 CMSInitiatingOccupancyFraction<=((128.0-36)-(36-36/(1+2)))/(128-36)*100 =73.913 

当xmx=128 xmn=24 SurvivorRatior=1时 CMSInitiatingOccupancyFraction<=((128.0-24)-(24-24/(1+2)))/(128-24)*100=84.615… 

当xmx=3000 xmn=600 SurvivorRatior=1时  CMSInitiatingOccupancyFraction<=((3000.0-600)-(600-600/(1+2)))/(3000-600)*100=83.33

CMSInitiatingOccupancyFraction低于70% 需要调整xmn或SurvivorRatior值。

令:

网上一童鞋推断出的公式是::(Xmx-Xmn)*(100-CMSInitiatingOccupancyFraction)/100>=Xmn 这个公式个人认为不是很严谨,在内存小的时候会影响xmn的计算。

Guess you like

Origin blog.51cto.com/14309075/2415294