Android内存管理(3)缓存不要用SoftReference, 用android.util.LruCache

  

  A reference that is cleared when its referent is not strongly reachable and there is memory pressure.

Avoid Soft References for Caching

  In practice, soft references are inefficient for caching. The runtime doesn't have enough information on which references to clear and which to keep. Most fatally, it doesn't know what to do when given the choice between clearing a soft reference and growing the heap.

  The lack of information on the value to your application of each reference limits the usefulness of soft references. References that are cleared too early cause unnecessary work; those that are cleared too late waste memory.

  Most applications should use an android.util.LruCache instead of soft references. LruCache has an effective eviction policy and lets the user tune how much memory is allotted.

Garbage Collection of Soft References

When the garbage collector encounters an object obj that is softly-reachable, the following happens:

  • A set refs of references is determined. refs contains the following elements:
    • All soft references pointing to obj.
    • All soft references pointing to objects from which obj is strongly reachable.
  • All references in refs are atomically cleared.
  • At the same time or some time in the future, all references in refs will be enqueued with their corresponding reference queues, if any.

  The system may delay clearing and enqueueing soft references, yet all SoftReferences pointing to softly reachable objects will be cleared before the runtime throws an OutOfMemoryError.

  Unlike a WeakReference, a SoftReference will not be cleared and enqueued until the runtime must reclaim memory to satisfy an allocation.

 

转载于:https://www.cnblogs.com/sjjg/p/5281486.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值