Reputation: 657
I have been going through the android developer site for Displaying Bitmaps Efficiently.
But I am confused with the below statement and need suggestions for a good memory cache technique. Below is the link.
http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
Note: 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.
So is LRU Cache , the best technique than weak reference/Soft reference and why is it so? Also how to manage same for the different android device versions?
Please help me and suggest the best technique for the same.
Upvotes: 1
Views: 704
Reputation: 7521
Even though the document does not mention this . we do not handle the overhead of cache or recycling bitmaps it is handeled by using third party[trusted and widely used] tools like picasso or glide or universal image loader. please read upon this its good to know but a lot of apps use these
Upvotes: 2
Reputation: 10083
About references
A SoftReference should be cleared and enqueued as late as possible, that is, in case the VM is in danger of running out of memory.
A WeakReference may be cleared and enqueued as soon as is known to be weakly-referenced.
Check this for more
Using LRU Cache: Why is it preferred, Simple answer is because it's part of android. It is prefered to use the LRU cache as a part of android best practices.
Check this Android best practices
About versioning: It doesn't depends on android versions (Slightly below 2.3). But it depends on devices(Size of LRU cache vary).
Use this calculation for finding the size iin each device. (Android officially suggest this)
int memoryClass= ( ( ActivityManager )context.getSystemService( Context.ACTIVITY_SERVICE ) ).getMemoryClass();
int cacheSize = 1024 * 1024 * memoryClass/ 8;
Upvotes: 1
Reputation: 7377
It depends what you are using in displaying the bitmaps ? if you are using libraries such volley , picasso, universal image loader and others then they handled casching in a good way . if your not using those libraries there is HttpResponseCache
. but honestly I have never tested for Bitmaps
Upvotes: 0