while setting the content view I have got this exception. I have already tried to handle this exception by:
try{
setContentView(R.layout.activity_main);
}catch (OutOfMemoryError e) {
e.printStackTrace();
}
This exception came 5 out of 100 time approx while testing. but I am unable to resolve this issue. Or otherwise my QA team will kill me on this :(
There are several ways to resolve this type of error. Usually it is because your image resolution is too high on your layout. Here are your options:
Ask for a lower resolution image (fewer pixels). That may not be acceptable, but your team should be conscious of memory constraints.
Remove the image(s) from the layout, but leave the ImageView
elements, if there are any. IF it is a background, remove the background. Then use java to load the images efficiently. You will need to measure the screen size and/or size of the elements you want the images for before loading. Android docs have a great example here:
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
heapSize
:How to increase heap size of an android application?
It is also possible that you are referencing images on another screen or otherwise have a memory leak, in which case you need to review any/all code that is called before this (including other activities) to find where those leaks are. Likely causes are things like passing an Activity
to a background thread or service instead of passing it's Context only, creating static references to images, not using WeakReference
to handle large memory objects passed to other threads/processes, or other unusual approaches to references to objects.
As mentioned by others this is happening because the app is having memory leak or your app is using too much memory.
Try to reduce the number of layers in your layout file first, this would most probably resolve the issue. In case this doesn't install MAT plugin, using that you can check the amount of memory being used by your app at runtime. There may be a possibility that a previous activity opened before your activity is not releasing memory or views are not being properly recycled in list/grid
PS : Reduce the number of layers in your layout file that would work most probably
Provided code is not necessarily the cause of OutOfMemoryError. The primary cause for OutOfMemory is that JVM GC can't free enough memory for your operation to proceed.
Assuming setContentView is indeed heavy operation, and decreasing resolution size of the image, would solve it for a while, this problem would reappear in a different place in the system, in a different time.
You can google the reasons why GC can't allocate enough space, usually it means you have some stale reference in your code, which you need to clean, in order to help your GC.
I would suggest using a profiler for your application, to track how space is allocated and cleared in your application. You have standard one by default with android.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With