-XX:SoftRefLRUPolicyMSPerMB从名字看不出什么意思?【官文解读】

虚拟机参数:SoftRefLRUPolicyMSPerMB

-XX:SoftRefLRUPolicyMSPerMB从名字看不出什么意思?【官文解读】

  • SoftRefLRUPolicyMSPerMB 该参数得官方解释:

What determines when softly referenced objects are flushed?Starting with 1.3.1, softly reachable objects will remain alive for some amount of time after the last time they were referenced.

The default value is one second of lifetime per free megabyte in the heap. This value can be adjusted using the

-XX:SoftRefLRUPolicyMSPerMB  flag, which accepts integer values representing milliseconds.

For example, to change the value from one second to 2.5 seconds, use this flag:-XX:SoftRefLRUPolicyMSPerMB=2500

The Java HotSpot Server VM uses the maximum possible heap size (as set with the -Xmx option) to calculate free space remaining.The Java Hotspot Client VM uses the current heap size to calculate the free space.This means that the general tendency is for the Server VM to grow the heap rather than flush soft references, and -Xmx therefore has a significant effect on when soft references are garbage collected.

On the other hand, the Client VM will have a greater tendency to flush soft references rather than grow the heap.The behavior described above is true for 1.3.1 through Java SE 6 versions of the Java HotSpot VMs. This behavior is not part of the VM specification, however, and is subject to change in future releases.

Likewise the -XX:SoftRefLRUPolicyMSPerMB flag is not guaranteed to be present in any given release.

比较好的一个解释:

当gc执行时,决定SoftReference回收有两个因素:
[list]
[]SoftReference的timestamp
[
]有多少空闲空间
[/list]
在server模式下,会用-Xmx参数得到空闲空间大小。
在client模式下,会用当前heap最大空闲空间大小。
简单来说,server模式下会优先扩大heap大小,client模式下会优先回收垃圾。

SoftReference类中,有一个timestamp:

public class SoftReference<T> extends Reference<T> {

  /* Timestamp clock, updated by the garbage collector
    */
   static private long clock;

  /* Timestamp updated by each invocation of the get >method.  The VM may use
   * this field when selecting soft references to be >cleared, but it is not
    * required to do so.
   */
  private long timestamp;

  ......
}

在新建SoftReference对象和调用SoftReference.get时都会使>timestamp更新为clock的值。而clock代表的是上次gc的时间。

SoftRefLRUPolicyMSPerMB默认为1000,即1s。代表每1MB>空闲空间大小SoftReference保留1s。

是否回收的条件:
clock - timestamp <= freespace * >SoftRefLRUPolicyMSPerMB

举例来说,clock为1000,timestamp为300,空闲空间为1MB。

1000 - 300 <= 1000 * 1

所以不会被回收。【注:当上面条件满足则不会被回收】

一个值得注意的地方是,SoftReference会至少经历1次gc而不被回收。

参考http://jeremymanson.blogspot.com/2009/07/how-hotspot-decides-to->clear_07.html

解释: 我们知道软引用,实在空间不足的情况下才会被回收,当然这个只是一个比较简单的解释。实际上软引用的回收机制复杂得多,需要SoftRefLRUPolicyMSPerMB的意思,就先明白soft-reference在代码逻辑上需要满足的条件是什么,如下:

clock - timestamp <= freespace * SoftRefLRUPolicyMSPerMB

clock记录是上一次GC的时间戳,timestamp则是最近一次读取soft-reference引用对象(即最近调用get())的时间戳。他们的差【clock - timestamp】表示了soft-reference有多久没用了,越大表示越久没用。如果他们的差为负数,表示刚刚用过。而【freespace * SoftRefLRUPolicyMSPerMB】表示能够VM的忍耐度,VM能够忍耐软引用对象多久没有被回收,而VM的忍耐度从公式可以知道是由VM计算得出的空闲空间大小和用户指定的忍耐度SoftRefLRUPolicyMSPerMB来决定的。

也就是说,如果软引用上次被get()的时间离最近一次GC的时间不会太久远的话就可以不被当前GC回收。

题外话:

不得不吐槽一下SoftRefLRUPolicyMSPerMB这个参数的命名:虽然从名字中我们可以看到LRU算法,也可以看到它与SoftRef的GC有关,还有freespace由多少M空间等寓意,但是单纯看这个变量名确实很难直观的明白这个参数的用处,真是到处坑。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
这是一个使用Java语言编写的应用程序的命令行运行指令。该程序是一个消息队列中间件的Broker,使用了RocketMQ框架实现。其中的参数含义如下: -server:使用JVM的server模式。在多核CPU上提高性能。 -Xms2g:JVM启动时堆内存的最小值为2G。 -Xmx2g:JVM堆内存的最大值为2G。 -XX:+UseG1GC:使用G1垃圾回收器。 -XX:G1HeapRegionSize=16m:G1垃圾回收器内存区域的大小为16M。 -XX:G1ReservePercent=25:预留25%的空间以避免太满引发的性能问题。 -XX:InitiatingHeapOccupancyPercent=30:G1在堆内存使用达到30%时会触发垃圾回收。 -XX:SoftRefLRUPolicyMSPerMB=0:清除软引用的时间间隔为0,即软引用的对象一旦没有被使用就会被立即清除。 -verbose:gc:打印GC日志。 -Xloggc:/dev/shm/rmq_srv_gc_%p_%t.log:将GC日志输出到/dev/shm/rmq_srv_gc_%p_%t.log文件中。 -XX:+PrintGCDetails:打印GC详细信息。 -XX:+PrintGCDateStamps:打印GC时间戳。 -XX:+PrintGCApplicationStoppedTime:打印应用程序停止时间。 -XX:+PrintAdaptiveSizePolicy:打印自适应策略的信息。 -XX:+UseGCLogFileRotation:启用GC日志文件轮换。 -XX:NumberOfGCLogFiles=5:GC日志文件轮换时保留的文件数目。 -XX:GCLogFileSize=30m:GC日志文件的大小为30M。 -XX:-OmitStackTraceInFastThrow:关闭快速抛出异常时的栈信息。 -XX:+AlwaysPreTouch:在JVM启动时预先分配堆内存。 -XX:MaxDirectMemorySize=15g:最大直接内存大小为15G。 -XX:-UseLargePages:不使用大页面。 -XX:-UseBiasedLocking:不使用偏向锁。 -Drocketmq.client.logUseSlf4j=true:使用SLF4J作为日志框架。 -c ../conf/broker.conf:指定Broker的配置文件路径。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值