SoftReference gets garbage collected too early

前端 未结 5 1769
温柔的废话
温柔的废话 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:00

    From Android Training site:

    http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

    In the past, a popular memory cache implementation was a SoftReference or WeakReference bitmap cache, however this is not recommended. Starting from Android 2.3 (API Level 9) the garbage collector is more aggressive with collecting soft/weak references which makes them fairly ineffective. In addition, prior to Android 3.0 (API Level 11), the backing data of a bitmap was stored in native memory which is not released in a predictable manner, potentially causing an application to briefly exceed its memory limits and crash.

    More information in link.

    We shoud use LruCache instead.

提交回复
热议问题