Hi, I need to cache bitmaps of various sizes for my applications. I followed the guidelines posted here. http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html#memory-cache And per these guidelines created an LruCache sized as follows
// Get memory class of this device, exceeding this amount will throw an // OutOfMemory exception. final int memClass = ((ActivityManager) context.getSystemService( Context.ACTIVITY_SERVICE)).getMemoryClass(); // Use 1/8th of the available memory for this memory cache. final int cacheSize = 1024 * 1024 * memClass / 8; mMemoryCache = new LruCache(cacheSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { // The cache size will be measured in bytes rather than number of items. return bitmap.getByteCount(); } } The only differences between my code and the recommended guidelines are 1. My cache is 1/4th the size of memory per memory class. 2. I cannot use bitmap.getByteCount() since I support GingerBread devices too( <API level 12) and so I approximate the same using bitmap.getRowBytes() * bitmap.getHeight(); On most devices I saw no issues. However as I tested my application on an HTC T-Mobile G2 Android Version 2.3.4 I observed a consistently reproable crash on Bitmap alloc. The device memory class was 32. So my cache size was 8 MB. The RunTime.maxMemory was also 32 MB. I observed my heap grow all the way to around 13 MB total (6/7 MB) allocated at which point the next Bitmap alloc failed with an OutofMemoryException. adb logs said VM did not allow me to allocate 1.5MB for the bitmap. My question is this. Is there some other number that should be used as a guideline for what sizes the device can support in terms of Bitmap allocations? Is there another API I need to call to get this number? Something like max allowed native heap for bitmaps? Is 1/4th of device memory class too big a cache? Thanks Gaurav -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en