当应用页面需要网络加载很多图片的时候,为了提高效率,需要缓存图片,前段时间写了一个需要加载很多网络图片的ListView页面,当时为了提高效率将下载的图片用一个软引用字段进行缓存,但是效果不尽人意,缓存的图片被回收的很快,基本达不到效果,后面看见官方文档有这样一句话“In the past, a popular memory cache implementation was aSoftReference or WeakReference bitmap cache, however this is notrecommended. Starting from Android 2.3 (API Level 9) the garbage collector ismore aggressive with collecting soft/weak references which makes them fairlyineffective. In addition, prior to Android 3.0 (API Level 11), the backing dataof a bitmap was stored in native memory which is not released in a predictablemanner, potentially causing an application to briefly exceed its memory limitsand crash.”大概意思就是:从Android2.3以后,GC机制将变得更加频繁,软/弱引用的效率低了很多,而在3.0以前,bitmap是存放在native内存中,是不可预知的被释放,可能会导致崩溃。因此bitmap缓存不能直接使用软引用,需要借助类似如LruCache内部使用强引用进行。
android笔记2——关于图片缓存的一些注意事项
最新推荐文章于 2024-09-20 16:43:30 发布