SoftReference gets garbage collected too early

前端 未结 5 1771
温柔的废话
温柔的废话 2020-12-08 17:17

I\'m on my way with implementing a caching mechanism for my Android application.

I use SoftReference, like many examples I\'ve found. The problem is, w

5条回答
  •  抹茶落季
    2020-12-08 18:02

    Jvm follows this simple equation to determine if a soft reference should get cleared:

    interval <= free_heap * ms_per_mb

    interval is duration between last gc cycle timestamp and the last access timestamp of soft reference. free heap is heap space available at that moment. ms_per_mb is milliseconds allocated to every MB available in heap. (Constant default 1000 ms)

    If above equation is false, reference gets cleared.

    So, even if you have a lot of free memory, if your soft references have not been accessed for an ample amount of time, they will get cleared.

    -XX:SoftRefLRUPolicyMSPerMB= jvm arg can be used to tweak ms_per_mb constant.

提交回复
热议问题