My app uses some bitmaps with a SurfaceView. For bitmaps that a recalled often I stored them as class fields, i.e. when my extension of SurfaceView instance is created I have
bitmap_puppyicon = BitmapFactory.decodeResource(getResources(),R.drawable.puppy);
Where bitmap_puppyicon is a class variable of my extended SurfaceView. WHich means, I think, it is on the heap.
For bitmaps that are used once or twice or so, I created them on the fly in the onDraw() method.
I checked out the heap in DDMS and felt I could afford to store more bitmaps as class variables instead of creating them in onDraw().
To my surprise the heap size went down a few percentage points after doing this. Have I misunderstood something? Why would the heap get smaller with more bitmaps stored?
It use skia under the hood, and heap is not the place where bitmaps live, In the heap you have links to bitmaps only
Related
In android app, images can be changed through run-time (e.g. layout-bg, ImageButton-src, Button-bg, ..etc), so what happens to the old image in the terms of memory occupation? is it removed or still occupy a portion from the memory? and if it's not removed, how can I clear the memory from this image?
UI part of android is mainly written in Java -- once all references to a bitmap is gone, the bitmap will be flagged for garbage collection. If you do not use a bitmap texture, and you have no reference to that bitmap anywhere in the code the memory will be freed immediately. Please be careful about memory leak in android. Read this:
http://android-developers.blogspot.ca/2009/01/avoiding-memory-leaks.html
I am making a painting application in which I have to add bitmaps of images in an Arraylist when drawing many lines. Unfortunately, I got an out of memory exception. I am unable to fix the issue, though I have increased the size of heap.
I am adding current bitmaps in an Arraylist like this:
linesbitmap.add(Bitmap.createBitmap(mBitmap));
Heap memory size is increases while adding bitmaps in arraylits in Android?
Of course. If you pour more water into a bucket, the bucket will contain more water. If you put more stuff in a bag, the bag will contain more stuff. If you allocate more memory, you will be using more memory. This has nothing much to do with Android -- this is the way computers have worked for the past ~50 years.
i am unable to handle it
Use less memory:
Have fewer bitmaps in the list
Have smaller bitmaps (fewer pixels, lower color depth) in the list
Remove and recycle() bitmaps in the list before adding more to the list
Move data out of memory and into files (e.g., persist your bitmaps, then remove them from the list)
Save data more efficiently (e.g., retain instructions on how to draw a line, instead of a bitmap representing the line)
And so on
i have also increase the size of heap
You cannot increase the size of the heap on most versions of Android.
I have many pictures in my #drawable directory which are connected to xml layout via background property:
i.e
<ImageButton android:background="#drawable/background1" ... />
I have many activities, so, when activity is destroyed (BACK is pressed), the heap isn't freed. So, the question is:
Does android load everything in the memory once and make heap free only when application is destroyed? How I can prevent in this case memory consumption ? Only through image compression or dynamically loading background and images?
I think Android has a its own garbage collector, but for proceed it its take sometimes instead of relevent momory as the object is not in use. And whenver the bitmap is created its memory occupied in native heap and heap garbage collector is to lazzy.
So if possible use dynamic Image loading instead of static, and try to use your own recycler or freed the memory of your bitmap.
EDIT:
The gc() does not handle so-called short lived objects as fast as we
would like.
Keep the number of view objects at a stable level*, and recycle them instead of destroying and creating new ones.
A nice post of Avoiding memory leaks by Roman Guys refer it.
If I am wrong please let me know. And please share some more information on this.
Thanks.
I recently added three small images to my apps GUI. They are around 40kb in size. Everything is fine until the screen locks on the device. When I unlock I get this error:
OutOfMemoryError: bitmap size exceeds VM budget
I've seen plenty of articles relating to the error but all seems to be for a different reason. I'm not doing anything fancy like downloading from the internet. Just local resources for the UI.
Any suggestions welcome
Thanks
Dann
If the images are too big, and you are creating many Bitmaps, you'll have either to properly scale the Bitmaps before usage in code, or the shrink your resource images, if they are included in your project.
Also you have to properly use bitmap.recycle() when you no longer use your Bitmaps. This means that setting an Object which contains an Bitmap to null or clearing an ArrayList of Bitmaps won't be enough. You'll first have to iterate over the Bitmaps and call recycle() and clear then the list.
I am trying to decode a bitmap with pixel size 1024*683 with the api
decodefile(filepath) but process runs out of memory while decoding the
image.
I need the bitmap object for this image at one go without any scaling
as i have to work with NDK reading the pixel values using this bitmap.
Therefore any scaling or sampling method can't be applied in my case.
I wonder how come this is possible that just decoding a file with
such size would need any in-around method. There is enough heap size
available for this process.
Your opinion and perspective in this matter will be appreciated.
Thanks
Nawab
I faced the same problem when I had memory leaks in one of my activities. There were not only view leaks, but bitmap leaks too. And memory consumed by bitmaps is not taken into account when DDMS shows free heap space.