-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空間等寓意,但是單純看這個變量名確實很難直觀的明白這個參數的用處,真是到處坑。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章