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.
Related
My application use a little memory, it's about 3.4MB, in some old android devices such as GT-I9001. But when i running it in HTC one, my application use very more memory.
Look, the allocated memory is 26.881MB, it's too big, and the free memory only have 2.940MB. Then i use MAT tool check the memory leak, i find the resource bitmap use mach memory.
I can't explain the reason. My application often out of memory. I think Maybe the problem is caused by high screen resolution. If someone also encountered this problem, please join the discussion, thanks!
I debuged the problem, and found some reason:
The onCreate() function in my start activity, and you can see the breakpoint. The application only use allocated memory 3.4MB before calling the setContentView(R.layout.welcome) to load layout xml. Then the application run to next step, it use allocated memory 19MB. So i think this problem must be caused by loading the layout xml.
I modifed the "welcome.xml" file, deleted all widgets, that only have a "RelativeLayout"
But the program also use 19MB memory. Finally, i deleted the backgroud of RelativeLayout and the program memory return to normal size, it only use 3MB.
The size of pictrue "loading_background.png" is only 21KB, i think that perhaps the high screen resolution of high-end device changed the picture size in memory, i will try to use 9.png picture. If you understand this part of the problem, please join the discussion, thanks!
It's not a memory leak if you use big image for background.
File size doesn't matter. When it is loaded into memory it takes width * height of the image * 4bytes.
Use small 9-patch images or shape drawables when possible.
This problem can be solved using drawable-nodpi, look this:
Android background image memory usage
Just try to make a bitmap from 3MB PNG file. You will get a 20MB picture. That's why it's beter to convert your images from PNG to JPG. The quality is not so much different actually, but you will profit a lot from memory side.
just add high resolution images to the xxhdpi folder in drawable. this prevent android scale up the image to the ultra sizes
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 cache big bitmaps for drawing on screen in Android. But now I am facing OutOfMemoryException, say that the bitmap allocation exceeds the VM budget.
I need to minimize the size of the bitmap but I cannot reduce the resolution. For my use case, I need to only save the shape of the bitmap and apply color later when actually drawing, so I am using ALPHA_8 as the bitmap configuration.
I want to know if there is a 1-bit pixel (either completely opaque or completely transparent) configuration in bitmap, or any similar ways to save memory?
Reducing the color depth from 8 bits to 1 would, of course, help a little. However, it doesn't really solve the problem but just postpones it. It only means that you'll get the OOME later but you'll still get it.
Consider moving your cache from RAM to disk and, optionally, add a smaller RAM-based cache on top of it to improve performance.
In my app users choose images and program lets users to make changes on images. Since there are a lot of different android devices out there my program crashes on some of devices which less heap size. I want to calculate the best fit dimensions for user's phone so it won't crash because of VM budget. I have added a screenshot from "Picsay Pro" which is making exactly what i am looking for. I do know about the "BitmapFactory.Options" my only problem is to find a way to decide image dimensions which won't let crash the app because of VM budget.
Calculate the Free Space Remaining on the phone:
long freeMemory = (Runtime.getRuntime().maxMemory()) - (Debug.getNativeHeapAllocatedSize());
where,
Runtime.getRuntime().maxMemory() - returns the Total Heap Limit size (in Bytes).
Debug.getNativeHeapAllocatedSize() - returns the Amount of Data already used by your application (in Bytes).
Calculate the size of the Bitmap you are using by this formula,
long Bytes_allocated = (height of the Image) * (width of the Image) * 4;
Compare freeMemory and Bytes_allocated to select the appropiate size for your application.
I actually ended up compressing images on the phone for two reasons. One was upload speeds and another one was heap problems. You can try doing something similar, or at least post the stack trace!
Android outofmemory error bitmap size exceeds vm budget in 2.3.3
i think this compression technique could help
So far I've not found any reliable way to deal with image size vs available memory. The problem is the memory becomes fragmented very quickly, so that you could have 10 MB free, but no contiguous space for a 2 MB image. What is really needed is the size of the largest free space, but there doesn't appear to be any way to get this. Better would be a way to defragment memory, but no such function exist for this either.
Still if your available memory is less than the image, you can be sure you're going to crash if you attempt to use it, so it has some merit to at least check before you use it.
With late-2012 tablets now having 1920x1280 resolution, we now need 20MB of continuous memory for a single background image! It appears some of these tablets are now allowing heaps up to 256MB, but while tossing more VM space seemingly solves it, we really need a memory defragmenter or a way to reserve space.
There is one trick that might help if the image size is not scaled or modified, and each changed image is the same size AND you are limiting your app to Android 3.0+:
Options opt2 = new BitmapFactory.Options();
opt2.inBitmap = mBitmap; // reuse mBitmap to reduce out of memory errors
mBitmap = BitmapFactory.decodeResource(getResources(), myDrawable, opt2);
This will reuse the same memory area mBitmap for the image.
How do I display an image of arbitrary size without OOMing or downscaling the image.
I dont want to downscale due to zooming and dragging of the picture?
If it isnt possible to display an image of arbitrary size, how do I determine the maximum possible size of the image to display, without causing a OOM?
Bitmap data is aloccated in the Native heap rather than the VM heap. see BitmapFactory OOM driving me nuts for details on how to monitor the Native heap and how big your bitmap can get without hitting an OOM.