-XX:PretenureSizeThreshold的默认值和作用浅析

一、背景

《深入理解Java虚拟机》第93页,3.6.2 大对象直接进入老年代。

讲到大对象主要指字符串和数组,虚拟机提供了一个-XX:PretenureSizeThreshold参数,大于这个值的参数直接在老年代分配。

这样做的目的是避免在Eden区和两个Survivor区之间发生大量的内存复制(新生代采用复制算法)。

但是这里没讲清楚默认值是多少,默认会不会“大”对象直接进入老年代。

二、解析

2.1 参考文章

找到了一篇相关问题的文章《Frequently Asked Questions about Garbage Collection in the HotspotTM JavaTM Virtual Machine》

第29条:Do objects ever get allocated directly into the old generation?

In 1.4.1 there two situations where allocation may occur directly into the old generation.

有两种情况,对象会直接分配到老年代。
If an allocation fails in the young generation and the object is a large array that does not contain any references to objects, it can be allocated directly into the old generation. In some select instances, this strategy was intended to avoid a collection of the young generation by allocating from the old generation.

如果在新生代分配失败且对象是一个不含任何对象引用的大数组,可被直接分配到老年代。

通过在老年代的分配避免新生代的一次垃圾回收。


There is a flag (available in 1.4.2 and later) l-XX:PretenureSizeThreshold=<byte size> that can be set to limit the size of allocations in the young generation. Any allocation larger than this will not be attempted in the young generation and so will be allocated out of the old generation.

XX:PretenureSizeThreshold=<字节大小>可以设分配到新生代对象的大小限制。

任何比这个大的对象都不会尝试在新生代分配,将在老年代分配内存。


The threshold size for 1) is 64k words. The default size for PretenureSizeThreshold is 0 which says that any size can be allocated in the young generation.

PretenureSizeThreshold 默认值是0,意味着任何对象都会现在新生代分配内存。

2.2 实验解析

设置虚拟机参数

-Xms2048m
-Xmx2048m
-Xmn1024m
-XX:+UseConcMarkSweepGC

-XX:SurvivorRatio=8

-Xms表示初始化堆内存

-Xmx 表示最大堆内存

-Xmn表示新生代的内存

-XX:SurvivorRatio=8表示新生代的Eden占8/10,S1和S2各占1/10.

因此Eden的内存大小为:0.8*1024*1024*1024字节 约为819**1024*1024

上代码

public class Test {

    public static void main(String[] args) throws Exception {

        byte[] array = new byte[700 * 1024 * 1024];//734003216

        for (MemoryPoolMXBean memoryPoolMXBean : ManagementFactory.getMemoryPoolMXBeans()) {

            System.out.println(memoryPoolMXBean.getName() + "  总量:" + memoryPoolMXBean.getUsage().getCommitted() + "   使用的内存:" + memoryPoolMXBean.getUsage().getUsed());
        }

    }
}

可以看到应该被分配到了新生代的Eden区

Code Cache  总量:2555904   使用的内存:1206528
Metaspace  总量:4980736   使用的内存:3426872
Compressed Class Space  总量:524288   使用的内存:371280
Par Eden Space  总量:859045888   使用的内存:785546016
Par Survivor Space  总量:107347968   使用的内存:0
CMS Old Gen  总量:1073741824   使用的内存:0

 

将数组对象增加到大于Eden区内存大小

byte[] array = new byte[900 * 1024 * 1024];

会导致新生代分配失败,直接进入老年代

Code Cache  总量:2555904   使用的内存:1197824
Metaspace  总量:4980736   使用的内存:3426024
Compressed Class Space  总量:524288   使用的内存:371280
Par Eden Space  总量:859045888   使用的内存:34361864
Par Survivor Space  总量:107347968   使用的内存:0
CMS Old Gen  总量:1073741824   使用的内存:943718416

且此时通过jstat -gcutil vmpid命令查看垃圾回收状态,发现并没有YGC.

 

然后我们将字节数组改回 小于Eden去内存

byte[] array = new byte[700 * 1024 * 1024];

并添加启动参数-XX:PretenureSizeThreshold=100000000

Code Cache  总量:2555904   使用的内存:1172160
Metaspace  总量:4980736   使用的内存:3412552
Compressed Class Space  总量:524288   使用的内存:371280
Par Eden Space  总量:859045888   使用的内存:51542800
Par Survivor Space  总量:107347968   使用的内存:0
CMS Old Gen  总量:1073741824   使用的内存:734003216

发现即使新生代足够分配,大于这个值的大对象也直接在老年代分配。

从而印证了上面文档的说法。

三、总结

多查权威参考文档,参考价值更大。

多试验看效果,实践印象才更加深刻。

 

思路参考自:https://www.jianshu.com/p/f7cde625d849

 

  • 24
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

明明如月学长

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值