`
xussen
  • 浏览: 30282 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

内存分配策略

阅读更多
大对象直接进入老年代
大对象就是指需要大量连续空间的java对象,写程序时应该避免“短命的大对象”
可根据-XX:PretenureSizeThreshold来设置多大的对象直接进入老年代,但这东西只对Serial和ParNew收集器有效

长期存活的对象进入老年代
虚拟机为每个对象定义一个年龄计数器
在第一次Minor GC后仍然存活, 将对象移入Survivor空间年龄设为1。
今后每执行一次Minor GC,年龄加1。加到一定程度(-XX:MaxTenuringThreshold 默认15),晋升到老年代。
VM : -verbose:gc - Xms20M -Xmx20M -Xmn10M - XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=1 -XX:+PrintTenuringDistribution -XX:+PrintGCDetails
               byte[] allocation1, allocation2, allocation3;
              allocation1 = new byte [_1MB / 4];
              allocation2 = new byte [4 * _1MB];
              allocation3 = new byte [4 * _1MB];
              allocation3 = null;
              allocation3 = new byte [4 * _1MB];
[GC [DefNew
Desired survivor size 524288 bytes, new threshold 1 (max 1)
- age   1:     413416 bytes,     413416 total
: 4695K->403K(9216K), 0.0028899 secs] 4695K->4499K(19456K), 0.0029114 secs] [Times: user=0.02 sys=0.00, real=0.00 secs]
[GC [DefNew
Desired survivor size 524288 bytes, new threshold 1 (max 1)
: 4499K->0K(9216K), 0.0006270 secs] 8595K->4499K(19456K), 0.0006456 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
def new generation   total 9216K, used 4423K [0x040e0000, 0x04ae0000, 0x04ae0000)
  eden space 8192K,  54% used [0x040e0000, 0x04531fa8, 0x048e0000)
  from space 1024K,   0% used [0x048e0000, 0x048e0000, 0x049e0000)
  to   space 1024K,   0% used [0x049e0000, 0x049e0000, 0x04ae0000)
tenured generation   total 10240K, used 4499K [0x04ae0000, 0x054e0000, 0x054e0000)
   the space 10240K,  43% used [0x04ae0000, 0x04f44ef8, 0x04f45000, 0x054e0000)
compacting perm gen  total 12288K, used 2111K [0x054e0000, 0x060e0000, 0x094e0000)
   the space 12288K,  17% used [0x054e0000, 0x056effe0, 0x056f0000, 0x060e0000)
No shared spaces configured.
VM : - verbose:gc - Xms20M -Xmx20M - Xmn10M - XX:SurvivorRatio=8 - XX:MaxTenuringThreshold=15 -XX:+PrintTenuringDistribution - XX:+PrintGCDetails
[GC [DefNew
Desired survivor size 524288 bytes, new threshold 15 (max 15)
- age   1:     413416 bytes,     413416 total
: 4695K->403K(9216K), 0.0026816 secs] 4695K->4499K(19456K), 0.0027035 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   2:     413416 bytes,     413416 total
: 4499K->403K(9216K), 0.0005062 secs] 8595K->4499K(19456K), 0.0005261 secs] [Times: user=0.02 sys=0.00, real=0.00 secs]
Heap
def new generation   total 9216K, used 4827K [0x04130000, 0x04b30000, 0x04b30000)
  eden space 8192K,  54% used [0x04130000, 0x04581fa8, 0x04930000)
  from space 1024K,  39% used [0x04930000, 0x04994ee8, 0x04a30000)
  to   space 1024K,   0% used [0x04a30000, 0x04a30000, 0x04b30000)
tenured generation   total 10240K, used 4096K [0x04b30000, 0x05530000, 0x05530000)
   the space 10240K,  40% used [0x04b30000, 0x04f30010, 0x04f30200, 0x05530000)
compacting perm gen  total 12288K, used 2111K [0x05530000, 0x06130000, 0x09530000)
   the space 12288K,  17% used [0x05530000, 0x0573ffe0, 0x05740000, 0x06130000)
No shared spaces configured.

动态年龄判断,就是说相同年龄所有对象的大小相加的总和大于Survivor的一半,那年龄大于或等于这个年龄的对象就直接进入老年代
VM : - verbose:gc - Xms20M -Xmx20M - Xmn10M - XX:SurvivorRatio=8 - XX:MaxTenuringThreshold=15 -XX:+PrintTenuringDistribution - XX:+PrintGCDetails
               byte[] allocation1, allocation2, allocation3, allocation4;
              allocation1 = new byte [_1MB /6+12*1024];
              allocation2 = new byte [_1MB / 6+12*1024];
             
              allocation3 = new byte [4 * _1MB];
              allocation4 = new byte [4 * _1MB];
              allocation4 = null;
              allocation4 = new byte [4 * _1MB];
[GC [DefNew
Desired survivor size 524288 bytes, new threshold 1 (max 15)
- age   1:     525384 bytes,     525384 total
: 4805K->513K(9216K), 0.0026955 secs] 4805K->4609K(19456K), 0.0027157 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC [DefNew
Desired survivor size 524288 bytes, new threshold 15 (max 15)
: 4609K->0K(9216K), 0.0005254 secs] 8705K->4609K(19456K), 0.0005400 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
def new generation   total 9216K, used 4423K [0x04250000, 0x04c50000, 0x04c50000)
  eden space 8192K,  54% used [0x04250000, 0x046a1fa8, 0x04a50000)
  from space 1024K,   0% used [0x04a50000, 0x04a50000, 0x04b50000)
  to   space 1024K,   0% used [0x04b50000, 0x04b50000, 0x04c50000)
tenured generation   total 10240K, used 4609K [0x04c50000, 0x05650000, 0x05650000)
   the space 10240K,  45% used [0x04c50000, 0x050d0458, 0x050d0600, 0x05650000)
compacting perm gen  total 12288K, used 2112K [0x05650000, 0x06250000, 0x09650000)
   the space 12288K,  17% used [0x05650000, 0x05860010, 0x05860200, 0x06250000)
No shared spaces configured.

空间分配担保
在发生Minor GC虚拟机会统计进入老年代的平均大小是否大于剩余空间,如果大于,直接Full GC,如果小于,查看HandlePromotionFailure设置是否允许担保失败。
如果允许,进行Minor GC ,不允许,进行Full GC。
当HandlePromotionFailure失败,只好Full GC
建议HandlePromotionFailure打开,避免Full GC过于频繁



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics