0

I am trying to understand how to use android LRU cache so that I can use it in my app.

I have an app that is all about images. One activity displays a GridView of images, another displays a fullscreen image, and besides those two activities images are used in all sorts of other ways. Now about 85% of my images are coming from my server. So my question about LRU cache is this: do I load images directly from the server into LRU cache? Or do I load images from the server into my app's disk space and then when needed load those locally stored images into LRU cache?

The reason for my confusion is this. About 15% of my app's image usage comes from the user's local device, which the app gets through an intent

public void dispatchGalleryIntent(View view) {
  Intent gallery = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  gallery.setType("image/*");
  startActivityForResult(gallery, GALLERY_PHOTO_REQUEST_CODE);
}

Apparently I can load these images obtained from the local image folder into LRU cache. Before I read up on LRU cache, I was under the impression that these local images were practically already cached. Anyway, my plan is to only cache the images I get from the server. But I am wondering if I should store them on my app's internal storage and then pass them to LRU cache on demand or does LRU cache already handles storage for me. In other words, if my app is killed, does that cause LRU cache to empty? To restate my concern, I want images obtained from the server to survive crashes and the app being killed. So is LRU cached itself suitable for that or should I store my images on disk and then load LRU cache as needed?

learner
  • 11,490
  • 26
  • 97
  • 169

1 Answers1

0

The LRUCache is an in memory cache for objects. It has nothing to do with anything on the file system, and it is not persistent across sessions. If you want to download files from a server and have them stick around, you need to save them to the file system. Then if you're dealing with a lot of images you may want to put the Bitmap objects into an LRUCache.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Since your rating is so high I am not waiting for a contradictory response. But do you know of any authoritative references? I haven't seen that in the google training tutorial. – learner Mar 19 '14 at 02:28
  • Well, I've used it :) LRUCache was introduced as a replacement for the previous method of caching- using HashMaps of WeakReferences. The app I used it in downloaded thumbnails from a server, and stuck them in a ListView. The LRUCache was used to hold the bitmaps so we didn't go OOM when scrolling. – Gabe Sechan Mar 19 '14 at 02:30
  • If I am to load my images to the filesystem, do you know how to setup how many files I should load so that I don't get OOM error? – learner Mar 19 '14 at 03:00
  • 1
    When you create an LRUCache, you can overload the function that determines the size of the object. So there's 2 normal ways to do it. One is to count each object as 1, and specify how many bitmaps to cache. This works well if you have similar sizes. The other is to use the bitmap function to get the size of the bitmap in bytes, and to let the LRU cache a certain number of bytes. The LRUCache will then insure you don't go OOM, because it will forget the least recently used bitmap if it goes over than amount of memory. – Gabe Sechan Mar 19 '14 at 03:12
  • I have asked another question here: http://stackoverflow.com/questions/22495555/how-do-i-load-images-to-life-system-while-avoiding-out-of-memory-error . Can you help with that one too? :) It's specific to storing from server to the filesystem. – learner Mar 19 '14 at 03:22