android memory management (Heap Size) - android

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.

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!

Optimization of Android Application

Hi I making a chat application . In that application i use a lots of bitmap. This application takes nearabout 55 mb space in memory. can anyone tell me how i optimize my application so that it takes less space in memory. Some friends suggest me for unbinding Bitmap but i have no idea about it. Can anyone help me . Thanks in advance.
Have you had a look at the 'Displaying Bitmaps Efficiently' Training class? http://developer.android.com/training/displaying-bitmaps/
Manually unbinding won't really help with your memory usage unless you have a memory leak somewhere. The call you may want to look at is Bitmap.recycle().
In that application i use a lots of bitmap
do you use lots of bitmaps in the same time?
do you holding references to bitmap objects when they are not actually needed anymore? if yes - don't do that!
do you decoding the bitmaps to the height and width that you actually need to display them? (the actual imageView size that will show them) if not - you should do that!
if you will take my suggestions and still consume so much native memory - you can do the "unbinding" as you called it, by invoke the Bitmap.recycle() method on bitmap object that don't needed anymore. this method frees the pixels array resides in the native memory associated with the bitmap.
the system frees this memory by itself if no one holds the bitmap reference anymore, but calling the recycle() method will cause the system to free the memory when you tell it. this can be useful if you are using lot's of bitmaps in a short amount of time, which the system usually did not triggering the memory freeing in that time.

Avoid OutOfMemoryError in android

Loading images onto ImageView in xml takes more memory?
I Have an Activity with around 7 imageviews(just like the app tray in any android phone.)
I am loading the images for each of the imageviews in the xml. Does this cause OutOfMemoryError?
Is there a way to recycle these?
Yes it is possible to attach drawable images to ImageView and you can remove them when you need.
imageview.setBackgroundResource(R.drawable.image);
or
imageview.setImageDrawable(drawable);
It does not cause any out of memory issue. Link to Doc
I have had this problem when i was loading Images. Sometimes this error can only be seen in Android emulator, sometimes this crashes the app in the mobile device too.
I solved this problem by following two steps:
Re-size the image if the resolution is too high.
Not having too many image objects open at the same time.
You can help the garbage collector out too, by assigning the image object to NULL when it is no longer necessary.
Unless necessary i would prevent opening too many images at the same time.
The factor by which you have to re-size, will depend on the actual size of the image.
Few Tips to address memory issues in Android:
1). Never use local variables to store large data / bitmaps specially in loops, As it will create un-necessary objects. I prefer class/instance variables in such situations. Try not to persist data unless it is required.
2). Avoid making calls to View.findViewById(..) wherever you can. Romain Guy has clearly stated in Google I/O 2010 that this method is costly (takes some KBs of memory to run), so call this method smartly like using ViewHolder which will consume less memory.
3) Use String pool effectively and use StringBuilder/StringBuffer when you are doing heavy operations with strings. Make sure that you are aware when you have to use StringBuffer as it is synchronized and again costly in comparison to StringBuilder.

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

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