高等圣契刷新地点:Minor GC 中 MaxTenuringThreshold 和 TargetSurvivorRatio 参数说明

来源:百度文库 编辑:九乡新闻网 时间:2024/05/03 21:40:13
Minor GC 中 MaxTenuringThreshold 和 TargetSurvivorRatio 参数说明
分类: java2011-06-13 21:44 62人阅读 评论(0) 收藏 举报
inor GC 中 MaxTenuringThreshold 和 TargetSurvivorRatio 参数说明
-XX:MaxTenuringThreshold
在新生代中对象存活次数(经过Minor GC的次数)后仍然存活,就会晋升到旧生代。
**关于MaxTenuringThreshold(简称MTT)的相关资料:
The MaxTenuringThreshold fora Hotspot JVM
Each object has an "age" field in its header whichis incremented every time an object is copied within the young generation. Whenthe age field reaches the value of MTT, the object is promoted to the oldgeneration (I’ve left out some detail here…). The parameter -XX:+NeverTenuretells the GC never to tenure objects willingly (they will be promoted only whenthe target survivor space is full). (out of curiosity: does anyone actually use-XX:+NeverTenure?) Originally, in theHotSpot JVM, we had 5 bits per object for the age field (for a max value of 31,so values of MTT would make sense if they were <= 31). A couple of years ago(since 5u6 IIRC), the age field "lost" one bit and it now only has 4(for a max value of 15).
So, basically, when I set MTT=80, the JVM would haveactually “never tenured” the objects inthe Young Generation until the Survivor Spaces were full.
(补充:,设置MTT=任意大于15的值,实际上每次计算得到的TenuringThreshold= 16 = Max(MTT)+1,这一点可以通过-XX:+PrintTenuringDistribution参数看出,对象年龄永远不会达到这个数值,这时就等于设置了永远不依据对象年龄进行晋升 )
说说MaxTenuringThreshold这个参数
-XX:TargetSurvivorRatio
一个计算期望存活大小Desired survivor size的参数.
计算公式: (survivor_capacity * TargetSurvivorRatio) / 100 * sizeof(a pointer):survivor_capacity(一个survivor space的大小)乘以TargetSurvivorRatio,
表明若某个age上的survivor space对象的大小如果超过Desired survivor size,则重新计算tenuring threshold,以age和MaxTenuringThreshold的最小值为准,否则以MaxTenuringThreshold(>15取16)为准.
view plain
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
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");
}
}
view plain
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
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次数还存活的对象。代码如下:
view plain
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
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)
参考:
http://cr.openjdk.java.net/~andrew/jdk6-hs14-merge/webrev.01/hotspot/src/share  /vm/gc_implementation/shared/ageTable.cpp.frames.html
http://blog.bluedavy.com/?p=70
http://java.sun.com/performance/reference/whitepapers/tuning.html
http://java.sun.com/docs/hotspot/gc1.4.2/faq.html
http://www.javaworld.com/javaworld/jw-01-2002/jw-0111-hotspotgc.html