Java GC 策略

2017/02/12 iteye

Sun Hotspot JVM的GC策略是分代:

  1. Yong Generation: Eden, Survior1, Survivor2  Scavenge GC, Copy算法,GC频繁  
  2. Old(Tenured) Generation  会引起Full GC, 尽可能减少Full GC次数.一般采用CMS(Concurrency-Mark-Sweep, C is not referening to Compact)算法  
  3. Permanent Generation     会引起Full GC, 尽可能减少Full GC次数     通过 -XX:PermSize= -XX:MaxPermSize=  设置    permanent Generation Space 保存Class,Method等MetaData信息,与Heap不同,Sun JVM默认64M。  

注:

  1. GC调优中平衡throughput和pause time两个指标, Server 和 Client JVM会有不同的考量(HotSpot中)
  2. new,old space 调优 new space一般占25~40%,如果Stateless对象比较多的话可以考虑多分配一点;相反如果stateful对象比较多,可以多分配一点old space    
  3. java 启动命令行中加入 -verbose:gc 来measure 当前GC 性能     [GC 325407K->83000K(776768K), 0.2300771 secs] [GC 325816K->83372K(776768K), 0.2454258 secs] [Full GC 267628K->83769K(776768K), 1.8479984 secs]     325407K和83000K是Minor GC前后Yong GenerationHeap大小   776768K  is the committed size of the heap: the amount of space usable for java objects without requesting more memory from the operating system. Note that this number does not include one of the survivor spaces, since only one can be used at any given time, and also does not include the permanent generation, which holds metadata used by the virtual machine. 我的理解是young 和old generation当前之和除去一个survivor区和permanent区的大小。当前young Generation的大小是逐步增大的,介于xms和xmx之间。  
  4. -XX:+PrintGCDetails 参数输出的GC log格式会有不同     [GC [DefNew: 64575K->959K(64576K), 0.0457646 secs] 196016K->133633K(261184K), 0.0459067 secs]   indicates that the minor collection recovered about 98% of the young generation, DefNew: 64575K->959K(64576K) and took 0.0457646 secs (about 45 milliseconds).   The usage of the entire heap was reduced to about 51% 196016K->133633K(261184K) and that there was some slight additional overhead for the collection (over and above the collection of the young generation) as indicated by the final time of 0.0459067 secs.    
  5. -XX:NewRatio=3 means that the ratio between the young and tenured generation is 1:3. In other words, the combined size of the eden and survivor spaces will be one fourth of the total heap size.   6. -XX:SurvivorRatio=6   sets the ratio between a survivor space and eden  to 1:6. In other words, each survivor space will be one sixth the size of eden, and thus one eighth the size of the young generation (not one seventh, because there are two survivor spaces).

Search

    Table of Contents