Java.lang.outofmemoryerror android.graphics.BitmapFactory.nativeDecodeAsset (native method)

when setting up the content view, I have this exception. I already tried to handle this exception:

try{ setContentView(R.layout.activity_main); }catch (OutOfMemoryError e) { e.printStackTrace(); } 

This exception was 5 out of 100 times in testing. but I can not solve this problem. Or else my QA team will kill me on this :(

+6
source share
3 answers

There are several ways to resolve this type of error. This is usually because your image resolution is too large on your layout. Here are your options:

  • Ask for a lower resolution image (fewer pixels). This may not be acceptable, but your team must be aware of memory limitations.

  • Remove images from the layout, but leave the ImageView elements, if any. IF this is a background, remove the background. Then use java to load images efficiently. Before downloading, you will need to measure the screen size and / or size of the elements you want for the images. Android docs have a great example:

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

  1. Increase the "heapSize" value for your application. This is the LAST RESORT! Otherwise, you will most likely encounter the same error in the future, and then solve it using one of the above methods ... and you really won't like your QA! Here is the link to heapSize :

How to increase the heap size of an Android application?

It is also possible that you are referring to images on another screen or otherwise to a memory leak, in which case you need to look at any / all of the code that was called before (including other actions) to find where these leaks are. The likely reasons are things like passing an Activity to a background thread or service instead of passing it. Only context, the creation of static links to images, and not the use of WeakReference to process large memory objects transferred to other threads / processes, or other unusual approaches to object references.

+9
source

As others have mentioned, this is because the application has a memory leak or your application is using too much memory.

First try reducing the number of layers in your layout file, this will most likely solve the problem. In case this does not install the MAT plugin using this, you can check the amount of memory used by your application at runtime. It is possible that the previous action, open before your activity frees up memory, or the views are not properly processed in the list / grid

PS: Reduce the number of layers in your layout file, which will probably work

+2
source

The code provided is not necessarily the cause of OutOfMemoryError. The main reason for OutOfMemory is that the JVM GC cannot free up enough memory for your operation.

Assuming that setContentView is really a tough operation and reduces the size of the image resolution, it will solve it for a while, this problem will reappear in another place on the system at another time.

You can indicate the reasons why the GC cannot allocate enough space, usually this means that you have some outdated link in the code that you need to clear to help your GC.

I would suggest using a profiler for your application to keep track of how space is allocated and cleaned up in your application. You have a default default with Android.

+1
source

Source: https://habr.com/ru/post/980910/


All Articles