Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Image caching - how?

I think it is asked and asked before, but still, there are things I can't quite understand.

I have tried two different approaches:

  1. Keep all the images in memory, when certain limit is started to exceed, start removing them
  2. Let Android fix this with SoftReferences

In 2. it's just cleaning them up sometimes the second I allocate them! And I don't allocate too much - 30-40 images 50x50 pixels.

So I am sticking to one. The question is what is the limit?

  1. Can I get some reliable info from the device of how much exactly bitmap memory do I have left? I have done some research, watch DDMS values, it's just taking up more and more space (if I don't clean up) until it explodes. One moment there are only 200K left, next the system provides 2M more...
  2. I am currently using some heuristic decision, based on device model, or screen size. I think this is dead-end in the long run. Got memory exceptions on some phones, completely free on other.
  3. Is there a third solution, the right one?
like image 649
Danail Avatar asked Jul 29 '11 16:07

Danail


People also ask

How does caching images work?

It reduces the time it takes for the user to view the images or Javascript or CSS files. This is because the user now accesses the file from his system instead of getting downloaded over the network. At the same time, caching also reduces the number of requests and data transfer from your servers.

How do I save pictures on my Android cache?

My android project layout xml file has three button. which are shows, events & home. when buttons click they download images from url & save them in cache. So now downloaded are completed once it is clicked.

How does Android store cached data?

Open Settings and select Storage. In the resulting list, tap the Apps entry (Other Apps on Android 11 and earlier). This will take you to a list of all the apps installed on your phone. Choose the app whose cache you want to clear.

Does cache store images?

A cache is a special storage space for temporary files that makes a device, browser, or app run faster and more efficiently. After opening an app or website for the first time, a cache stashes files, images, and other pertinent data on your device.


1 Answers

Most important question: Are you recycle()-ing your bitmaps? This is very important for pre-Gingerbread applications (maybe post-Gingerbread too).

Watching the Memory Management for Android Apps session from Google I/O 2011 helped me get a better understanding of the peculiarities of developing for Android.

That video mentions a tool called MAT - a Memory Analyzer - which is useful for determining if you have objects hanging around in memory that have been leaked. It's possibly you have more than the 30-40 you think you have.

For viewing and/or logging the current size of heap, etc., I'd suggest using the code in this answer about Android Out of Memory exceptions.

like image 164
dustmachine Avatar answered Oct 15 '22 15:10

dustmachine