JVM调优:GC 参数

参考:

  • Memory Management in the Java HotSpot™ Virtual Machine
  • Java SE 6 HotSpot[tm] Virtual Machine Garbage Collection Tuning
  • Garbage Collector Ergonomics

一、    理论基础

参见《[Java性能剖析]Sun JVM内存管理和垃圾回收
二、    配置说明(重要部分加粗)
1.    Memory配置
1)    Heap Space配置

  • –Xmsn:初始堆空间,譬如-Xms512M
  • –Xmxn:最大堆空间,譬如-Xmx1024M,一般可指定这两个参数一致以避免在系统运行期间进行堆空间的调整
  • –XX:MinHeapFreeRatio=minimum(默认40):当代空闲空间在代空间中比例大于maximum时,自动减少代空间以小于该值(将内存归还给操作系统)
  • –XX:MaxHeapFreeRatio=maximum  (默认70):当代空闲在代空间中比例小于minimum时,自动增长代空间以大于该值(向操作系统请求内存分配)
  • -XX:YoungGenerationSizeIncrement=:Young Gen分配新内存时的增长比例,默认是20%(指符合MaxHeapFreeRatio时需要空间增长)
  • -XX:TenuredGenerationSizeIncrement=:Tenured Gen空间分配新内存时的增长比例,默认是20%(指符合MaxHeapFreeRatio时需要空间增长)
  • -XX:AdaptiveSizeDecrementScaleFactor=:空间缩小比例,如果空间增长比例是X,那么缩小比例是X/D(指符合MinHeapFreeRatio时需要空间缩小)

2)    Perm Gen配置

  • –XX:PermSize=n:默认持久区大小,譬如–XX:PermSize=32M(server模式下默认是16M)
  • –XX:MaxPermSize=n:持久区最大空间,譬如–XX:MaxPermSize=128M,如果设置地过小,会导致OutOfMemory(PermSpace)错误(server模式下默认是64M)

3)    自适应目标设定(默认情况下JVM回自动调整Young Gen与Tenured Gen的比例、Eden Space与Suvivor的比例来达到性能目标)

  • -XX:GCTimeLimit=time-limit :花费在GC上的时间上限,默认是98,当超过上限时,会抛出OutOfMemory(HeapSpace)的异常
  • -XX:GCHeapFreeLimit=space-limit :Heap空闲空间的最低比例下限,默认是2,当超过下限时,会抛出OutOfMemory(HeapSpace)的异常
  • -XX:MaxGCPauseMillis=nnn :最长的GC暂停时间,如果时间过长,会相应调整空间的大小(单位是毫秒)
  • -XX:GCTimeRatio=nnn :最大的GC占总可用时间的比例,如果时间过长,会相应调整空间的大小(花费在GC上的时间比例不超过1 / (1 + nnn))

GC的处理优先级是MaxGCPauseMillis最高,GCTimeRatio次之,其他的空间大小配置优先级最低
4)    Young Gen/Eden Space/Suvivor Space配置

  • -XX:-UseAdaptiveSizePolicy :不使用自适应自动调整空间大小,要使用后面的配置,必须先配置这个选项(可以使用-XX:+UseAdaptiveSizePolicy启动自适应自动调整空间大小)
  • –XX:NewSize=n:默认Young Gen的大小,譬如–XX:NewSize=400M
  • -XX:MaxNewSize=n:Young Gen的最大大小,譬如–XX:MaxNewSize=400M
  • –XX:NewRatio=n(默认server模式是8,client模式是2):Young Gen与Tenured Gen的比例
  • –XX:SurvivorRatio=n(默认是32):Eden Space与Survivor Space的比例,譬如–XX:SurvivorRatio=7,则每个Survivor Space占整个Young Gen的1/9(注意,有两个Survivor区)

5)    Thread Stack Space

  • -XX:ThreadStackSize=或-Xssn:设置线程的栈大小(字节数)(0表示默认) [Sparc: 512, Solaris Intel: 256, Sparc 64bit: 1024 all others 0] GC配置,一般使用默认值即可

2.GC配置
1)    Parallel and Parallel Compacting Collectors配置

  • -XX:+UseParallelGC :启用Parallel Collector
  • -XX:+UseParallelOldGC :启用Parallel Compacting Collector
  • -XX:ParallelGCThreads= :并行垃圾回收线程数(默认不需调整,与服务器的内核数一致)

2)    CMS配置(这部分为了避免我的理解错误,直接给出E文说明)

  • -XX:+UseConcMarkSweepGC:启用CMS垃圾回收器
  • -XX:+CMSIncrementalMode :Enables incremental mode. Note that the concurrent collector must also be enabled (with -XX:+UseConcMarkSweepGC) for this option to work.(disabled)
  • -XX:+CMSIncrementalPacing :Enables automatic pacing. The incremental mode duty cycle is automatically adjusted based on statistics collected while the JVM is running. (enabled) (Enables automatic control of the amount of work the CMS collector is allowed to do before giving up the processor, based on application behavior.)
  • -XX:CMSIncrementalDutyCycle= The percentage (0-100) of time between minor collections that the concurrent collector is allowed to run. If CMSIncrementalPacing is enabled, then this is just the initial value. (default 10)
  • -XX:CMSIncrementalDutyCycleMin= The percentage (0-100) which is the lower bound on the duty cycle when CMSIncrementalPacing is enabled. 10 0 -XX:CMSIncrementalSafetyFactor= The percentage (0-100) used to add conservatism when computing the duty cycle. 10 10 -XX:CMSIncrementalOffset= The percentage (0-100) by which the incremental mode duty cycle is shifted to the right within the period between minor collections. (default 0)
  • -XX:CMSExpAvgFactor= The percentage (0-100) used to weight the current sample when computing exponential averages for the concurrent collection statistics. (default 25)

 

你可能感兴趣的:(调优)