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=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

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

  从而印证了文档的说法。

  三、总结

  多查权威参考文档。

  多试验看效果

猜你喜欢

转载自blog.csdn.net/qianfeng_dashuju/article/details/94456781
今日推荐