Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.outofmemoryerror android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)

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 :(

like image 918
Ahmad Arslan Avatar asked Jan 13 '15 06:01

Ahmad Arslan


3 Answers

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:

  1. Ask for a lower resolution image (fewer pixels). That may not be acceptable, but your team should be conscious of memory constraints.

  2. 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

  1. Increase the "heapSize" for your app. This is a LAST RESORT! Otherwise, you will likely run into the same error in the future and then have to resolve it using one of the above methods... and your QA really won't like you then! Here is a reference on 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.

like image 90
Jim Avatar answered Nov 08 '22 22:11

Jim


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

like image 39
pvn Avatar answered Nov 08 '22 21:11

pvn


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.

like image 30
mavarazy Avatar answered Nov 08 '22 22:11

mavarazy