Android application OutOfMemory. Bitmap - android

I have an application for android 3.0 and higher where i have to show some large bitmaps.The bitmaps are already scaled to the size in which they have to be displayed. I have already used the largeheap=true in manifest file but still i am getting OutOfMemory error. I am not using LRU cache right now as mention in displaying bitmaps efficiently on developer.android. I have the following doubts.
Should i just try-catch the error and do the rest of the work?
Do i have to use bitmap.recycle() function. After android 3.0 it is not necessary to use bitmap.recycle() as the memory is not allocated in native and as mentioned this function frees the native object.
Should i use the LRU cache mechanism as mention in caching bitmaps ? Wont it increase the memory consumption of my application as i would be storing bitmaps in cache ?
Is there any proper example or architecture to display bitmaps that explain the LRUcache mechanism. Reading all the theories on the net is alot confusing.
Does the inBitmap field of BitmapFactory.Options also help in reducing memory ?
I have memory constraints on my application and i want it to use as less memory as possible. Please advise.

The best way to manage the memory and keep the quality is explained in this article on developer android Displaying Bitmaps Efficiently . Hope this answers your query.

Related

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 :)
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
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.
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!

Android Gallery with bitmap

Can someone please give me a example of android gallery using bitmap properties. I have created a gallery without it which is giving me out of memory error. I am new to android programming. Any help is appreciated.
Each application is assigned a memory limit by VM. It could be 16Mb, 32 Mb ... depending on the device.
So handling bitmaps is a very important thing. Recycling the bitmap's or using softreferences, LRU Cache, disk Cache are helpfull to avoid VM Budget error.
Refer to
http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
for more details on handling bitmaps.

Bitmap memory leaks

So, before honeycomb, the Bitmap obeject was just a pointer for a native heap
memory space( using malloc ), and i could clean that native memory calling .recycle() ; after honeycomb the memory for the Bitmap is allocated in the app heap, which gets gc calls.
My question is, my app need to support 2.2+ so what should I do? Check the version and call recyle? Dont call recycle at all? What is your advice for that. Because i have a BitmapCache which hold some Bitmap instances and i dont want them living in the memory for ever.
In any android version whether it is froyo,gingerbread or honeycomb. You have to check yourself for memory management. Yes, from 2.2+ you can adjust you application from sdcard, but keeping the bitmaps in heap memory, always will create problem for whether you use either version. If you want pure using of bitmaps, then why dont you follow their way, try this link. They have given you many ways to managing the bitmaps efficiently. Follow this link:
Displaying bitmaps efficiently
first, any android platform version like: android 2.2, or android 3.0 , or higher than android 3.0, the bitmap that you dont want use, you also need invoke bitmap.recycle()
although android >=3.0 , the bitmap save in the dalvik heap, not save in native heap. so as the java heap, if you have some references to refer the object, the object cann't be gc by system, and the bitmap memory can taken many memory, if you dont ensure the object references counter is zero, it will a big problem.
the Bitmap cache you said can save bitmap, so if you use WeakReference or SoftReference to save the bitmap, and using WeakReference.get() or SoftReference.get() to return the bitmap, the bitmap reference is in right control of system. Otherwise you need manage by yourself.
As you notice on developer pages they said that weak referencies are outdated solution.. Check all bitmap referenced tutorials from link I left here and try to use scaling down (when possible), caching and AsyncTask, as well as lazy bitmap loading from this example..
These things combined will be solution I am pretty sure..
btw.. I am currently on this topics too, cause just weak references are ok, but not perfect solution and I want to make my app lag-proof for future versions of AndroidOS..
Hope it's not too much links but when we get it, it will work like a charm ;)
Cheers

android memory management (Heap Size)

i am developing one application related to graphics, in this application i am using
lots of images and drawing on it.and all activities are running until my flow does not
get completed.so what should i do for assign large heap size to run smoothly my
application. Or any other way to run application smoothly .. i have no idea about
memory management right now . i am using only BitmapDrawable to display images and also
system.gc() to garbage collection . also use this
Runtime.getRuntime().runFinalizersOnExit(true);
Runtime.getRuntime().gc();
Runtime.getRuntime().freeMemory();
can anybody help me .. thanx in advance
Have you tried using Java weak references so that these images are automatically garbage collected? This blog post explains that well. Also, you can use Guava Collections CacheBuilder for the images.
It not possible to increase Heap Size.Its fixed.But you an do some calculation to remove some waste of memory.
1) If you are using Bitmap then recycle it using bitmap.recycle() when it use became over, this will be garbage collected.
2)Try to remove unnecessary reference from your object if there is no use of them.
3)Try to use Local Variable and avoid Static function and variable
Hope it help you :)
Android does not allow you to adjust your heap size for Java code—you’re stuck with a value of about 20MB (perhaps more on newer devices). Always remember to recycle your bitmaps.

Drawable advantage over bitmap for memory in android

This question is linked with the answers in the following question:
Error removing Bitmaps[Android]
Is there any advantage of using Drawable over Bitmap in Android in terms of memory de-allocation ?
I was looking at Romain Guy project Shelves and he uses SoftReference for images caches but I'm unable to search where is the code which is de-allocating these Drawables when SoftReference automatically reclaims the memory for Bitmap. As far as I know .recycle() has to be explicitly called on the Bitmap for it to be de-allocated.
In my understanding, Bitmaps are typically better for performance if you don't need to do much image manipulation. However, I have run into memory leaks when I don't manually recycle them. My solution was to write a class to help me manage my images that provides an easy way to recycle all my bitmaps at certain points in my application. It also provides an easy way to reuse already loaded resources (including Drawables).
You don't need to call Bitmap.reycle(). This will be done for you in its finalizer. Doing it in the finalizer means the allocation will be delayed until finalizers run, so when possible directly calling recycle() can help with memory management.
Acc. to this page, starting from API Level 11, the Bitmap pixel data is stored in the Dalvik Heap along with the associated Bitmap. Thus calling .recycle is actually not required, unless you want to reclaim the memory manually for further use. Be sure to de-reference the bitmap too, just as an added measure.
PS: This was the link which explains hackbod's answer.

Categories

Resources