In my app I have different list view that contains some thumbnails. Today I'm starting the refactoring and I want to implement the LRU Caching. I'm following the Android guide lines, but I'm wondering if is better to initialize only one LRU Cache for entire app, or is better initialize LRU cache for each list view. I'm afraid of outOfMemory. Thus I have the following questions that I can't answer with myself: - one LRU cache initialized with singleton pattern is a good idea ? - if the memory is low, does lead to outOfMemory situation the following initializion of the LRU Cache ?
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Get max available VM memory, exceeding this amount will throw an
// OutOfMemory exception. Stored in kilobytes as LruCache takes an
// int in its constructor.
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = maxMemory / 8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
// The cache size will be measured in kilobytes rather than
// number of items.
return bitmap.getByteCount() / 1024;
}
};
...
}
if memory is low, the LRU cache is automatically released? I'm wondering if the app will have problem to release memory when I use LRU Cache (app crash because out of memory ? )
Only one LRU Cache for entire app , can it be a problem ?
- More than one LRU Cache for entire app , can they be a problem ?