user1788084
user1788084

Reputation:

Android OutOfMemory error & LruCache

First things first, I know there's plenty of related posts, I've read lots of them, none of them helped and I'm out of ideas :)

So i'm developing an android app (sdk version 14+) which uses lots of images (for buttons, logos, displaying lot's of images and so on). Average image size is 120kb +- 100kb

As title says I'm gettin OutOfMemory error. At first i added image source to ImageViews in xml or programmatically using setImageResource. Soon app started to crash due to OutOfMemory error. So I've read this ( http://developer.android.com/reference/android/util/LruCache.html ) tutorial and implemented LruCache as it says. I added bitmaps to lrucache and then used the get method to set bitmap to imageview.

In activity's onDestroy method I call evictAll method on lrucache and I set lrucache to null. So I presume, the memory is freed then.

However problem still persists, when I go through couple of activities, the app crashes.

Help much appreciated :)

Upvotes: 5

Views: 5547

Answers (3)

user1788084
user1788084

Reputation:

Thanks guys you helped me a lot!

So if anyone surfs to this question with same problem, here are some guidelines:

  • call .recycle() on bitmaps when you don't need them anymore.
  • use Eclipse Memory Analyzer to find your problems
  • (must) see this video
  • here's how to view bitmaps from your .hprof file

Upvotes: 4

Raghunandan
Raghunandan

Reputation: 133560

According to the documentation exceeding max available VM memory will throw an OutOfMemory exception. Seems like caching bitmaps exceeds the virtual memory available.

Recycle bitmaps when not in use.

 bitmap.recycle();

When should I recycle a bitmap using LRUCache?. Have a look at the accepted answer by commonsware.

I believe that when the LRUCache evicts an image to make room for another one, the memory is not being freed.(Asked by user in the above link)

It won't be, until the Bitmap is recycled or garbage-collected.(answer by commomsware).

http://www.youtube.com/watch?v=_CruQY55HOk. Talks about memory management, memory leaks and using MAT Analyzer.

Upvotes: 3

Furqi
Furqi

Reputation: 2403

Are you storing all your images in the heap? If so, you should cache them in the sd card or the internal storage, and keep just a bunch of them in the heap (a nice approach is to use a LRU cache).

If you're already doing this... you can download the memory analyzer tool and debug your app's memory usage.

http://www.eclipse.org/mat/

Dealing with OutOfMemoryError can be really painful.

Good luck!

Upvotes: 1

Related Questions