Android pre-3.0 memory question - android

I read that before 3.0 data like bitmaps is strored outside the heap. But where exactly is it stored? And how can I analize the amount of memory consumed by my bitmaps? That all about OOM exeption.

The are located in the native heap. For more info Bitmaps in Android. You can get runtime information about the native heap using these methods.
getNativeHeapAllocatedSize
getNativeHeapFreeSize
getNativeHeapSize

Related

Android application OutOfMemory. Bitmap

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.

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!

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.

Tips for saving memory when coding under android?

Im currently developing a software under android and im getting quite quickly some OutOfMemoryException.... I did modified some part of my code to use more static variables instead of making new allocation with the "new" operator but is there any things else to do ? or any other tips ? Any advices would be welcome.
Thanks.
You can find a lot of tips here: http://developer.android.com/guide/practices/design/performance.html
Especially look at the Avoid Creating Objects topic.
You can also watch some Google I/O conferences, like the ones about writing non-janky apps or how to make fast and efficient apps from 2009.
If you have some resources that can be reloaded onto memory anytime, consider using WeakReferences. The objects inside them will be cleared just before the (Java) VM throws OutOfMemoryException, so there will be some more memory available.
You may be accessing too many resources (such as images) before garbage collector can handle/recycle them. If you're accessing particularly large images, then use BitmapFactory to scale them down, as seen answered on SO. (Of course, for this you would see something like "bitmap size exceeds vm budget" in your logcat too.)
Any more details on the error?

Categories

Resources