JVM参数:MaxTenuringThreshold和TargetSurvivorRatio说明

-XX:MaxTenuringThreshold
在新生代中对象存活次数(经过Minor GC的次数)后仍然存活,就会晋升到旧生代。

-XX:TargetSurvivorRatio
一个计算期望存活大小Desired survivor size的参数。默认50

计算公式: (survivor_capacity * TargetSurvivorRatio) / 100 * sizeof(a pointer):survivor_capacity(一个survivor space的大小)乘以TargetSurvivorRatio,表明所有age的survivor space对象的大小如果超过Desired survivor size,则重新计算threshold,以age和MaxTenuringThreshold的最小值为准,否则以MaxTenuringThreshold为准.

public class GCTenuringThreshold {  
    public static void main(String[] args) throws Exception {  
        GCMemoryObject object1 = new GCMemoryObject(2);  
        GCMemoryObject object2 = new GCMemoryObject(8);  
        GCMemoryObject object3 = new GCMemoryObject(8);  
        GCMemoryObject object4 = new GCMemoryObject(8);  
        object2 = null;  
        object3 = null;  
        GCMemoryObject object5 = new GCMemoryObject(8);  
        Thread.sleep(4000);  
        object2 = new GCMemoryObject(8);  
        object3 = new GCMemoryObject(8);  
        object2 = null;  
        object3 = null;  
        object5 = null;  
        GCMemoryObject object6 = new GCMemoryObject(8);  
        Thread.sleep(5000);  
 
        GCMemoryObject object7 = new GCMemoryObject(8);  
        GCMemoryObject object8 = new GCMemoryObject(8);  
        GCMemoryObject object9 = new GCMemoryObject(8);  
        GCMemoryObject object10 = new GCMemoryObject(8);  
        //object6 = null;  
        Thread.sleep(5000);  
        System.out.println("ok");  
    }  
} 

 

class GCMemoryObject {  
    private byte[] bytes = null;  
    public GCMemoryObject(int multi) {  
 
        bytes = new byte[1024 * 256 * multi];  
 
 
    }  
} 

    以-Xms20M –Xmx20M –Xmn10M –XX:+UseSerialGC参数执行以上代码,
输出如下:

[GC [DefNew
Desired survivor size 524288 bytes, new threshold 1 (max 15)
- age   1:     677968 bytes,     677968 total
: 6999K->662K(9216K), 0.0028753 secs] 6999K->2710K(19456K), 0.0029048 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC [DefNew
Desired survivor size 524288 bytes, new threshold 15 (max 15)
- age   1:         16 bytes,         16 total
: 6891K->0K(9216K), 0.0025312 secs] 8939K->2709K(19456K), 0.0026029 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC [DefNew
Desired survivor size 524288 bytes, new threshold 15 (max 15)
- age   1:         48 bytes,         48 total
- age   2:         16 bytes,         64 total
: 6200K->0K(9216K), 0.0095025 secs] 8910K->8853K(19456K), 0.0095770 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
ok
Heap
 def new generation   total 9216K, used 4297K [0x32310000, 0x32d10000, 0x32d10000)
  eden space 8192K,  52% used [0x32310000, 0x32742508, 0x32b10000)
  from space 1024K,   0% used [0x32c10000, 0x32c10040, 0x32d10000)
  to   space 1024K,   0% used [0x32b10000, 0x32b10000, 0x32c10000)
 tenured generation   total 10240K, used 8853K [0x32d10000, 0x33710000, 0x33710000)
   the space 10240K,  86% used [0x32d10000, 0x335b57a8, 0x335b5800, 0x33710000)
 compacting perm gen  total 12288K, used 369K [0x33710000, 0x34310000, 0x37710000)
   the space 12288K,   3% used [0x33710000, 0x3376c438, 0x3376c600, 0x34310000)
    ro space 10240K,  51% used [0x37710000, 0x37c3b700, 0x37c3b800, 0x38110000)
    rw space 12288K,  54% used [0x38110000, 0x387a76c0, 0x387a7800, 0x38d10000)

    可以看出,每次都重新计算threshhold,或者说从survivor space晋升到旧生代后,重新计算threshold。
SurvivorRatio过大,都应该是age为准,因为触发gc的条件没有达到,但survivor space/2的条件优先达到。
所以调小SurvivorRatio,可以达到MaxTenuringThreshold次数还存活的对象。代码如下:

public class GCTenuringThreshold1 {  
 
    //private  GCMemoryObject1 ng = new GCMemoryObject1(15);  
 
    public static void main(String[] args) throws Exception {  
        GCMemoryObject1 object2 = new GCMemoryObject1(0.1f);  
        GCMemoryObject1 object1 = new GCMemoryObject1(2f);  
        Thread.sleep(4000);  
 
        object1 = null;  
        GCMemoryObject1 object4 = new GCMemoryObject1(0.1f);  
        GCMemoryObject1 object3 = new GCMemoryObject1(2f);  
 
        Thread.sleep(4000);  
        object3 = null;  
        GCMemoryObject1 object6 = new GCMemoryObject1(0.3f);  
        GCMemoryObject1 object5 = new GCMemoryObject1(2f);  
 
 
        Thread.sleep(4000);  
        object5 = null;  
        GCMemoryObject1 object8 = new GCMemoryObject1(0.3f);  
        GCMemoryObject1 object7 = new GCMemoryObject1(2f);  
 
 
 
        Thread.sleep(4000);  
        object7 = null;  
        GCMemoryObject1 object10 = new GCMemoryObject1(0.3f);  
        GCMemoryObject1 object9 = new GCMemoryObject1(2f);  
 
 
        System.out.println("ok");  
    }  
}  
 
class GCMemoryObject1 {  
    private byte[] bytes = null;  
    public GCMemoryObject1(float multi) {  
 
        bytes = new byte[(int)(1024 * 1024*multi)];  
 
 
    }  
} 

    以-Xms20M -Xmx20M -Xmn10M -XX:SurvivorRatio=1 -XX:+UseSerialGC -XX:MaxTenuringThreshold=3 -XX:+PrintTenuringDistribution -XX:+PrintGCDetails
运行上述代码。

[GC [DefNew
Desired survivor size 1736704 bytes, new threshold 3 (max 3)
- age   1:     363408 bytes,     363408 total
: 2529K->354K(6848K), 0.0017645 secs] 2529K->354K(17088K), 0.0018275 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC [DefNew
Desired survivor size 1736704 bytes, new threshold 3 (max 3)
- age   1:     314616 bytes,     314616 total
- age   2:     363176 bytes,     677792 total
: 2748K->661K(6848K), 0.0009114 secs] 2748K->661K(17088K), 0.0009399 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC [DefNew
Desired survivor size 1736704 bytes, new threshold 3 (max 3)
- age   1:     314616 bytes,     314616 total
- age   2:     314600 bytes,     629216 total
- age   3:     363176 bytes,     992392 total
: 3042K->969K(6848K), 0.0010587 secs] 3042K->969K(17088K), 0.0010872 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC [DefNew
Desired survivor size 1736704 bytes, new threshold 3 (max 3)
- age   1:     314616 bytes,     314616 total
- age   2:     314600 bytes,     629216 total
- age   3:     314600 bytes,     943816 total
: 3341K->921K(6848K), 0.0012151 secs] 3341K->1276K(17088K), 0.0012443 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
ok
Heap
 def new generation   total 6848K, used 3049K [0x32310000, 0x32d10000, 0x32d10000)
  eden space 3456K,  61% used [0x32310000, 0x32524128, 0x32670000)
  from space 3392K,  27% used [0x32670000, 0x327566c8, 0x329c0000)
  to   space 3392K,   0% used [0x329c0000, 0x329c0000, 0x32d10000)
 tenured generation   total 10240K, used 354K [0x32d10000, 0x33710000, 0x33710000)
   the space 10240K,   3% used [0x32d10000, 0x32d68aa8, 0x32d68c00, 0x33710000)
 compacting perm gen  total 12288K, used 369K [0x33710000, 0x34310000, 0x37710000)
   the space 12288K,   3% used [0x33710000, 0x3376c430, 0x3376c600, 0x34310000)
    ro space 10240K,  51% used [0x37710000, 0x37c3b700, 0x37c3b800, 0x38110000)
    rw space 12288K,  54% used [0x38110000, 0x387a76c0, 0x387a7800, 0x38d10000)

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值