OOM exception

OOM exception is one of the more common exceptions in Android, so how does OOM exception occur? How is it solved? What are the common scenarios that generate OOM exceptions in Android?

1. First of all, understand an analysis of the OOM exception, which is mainly interpreted from three aspects.

1) Memory overflow: It means that when the program applies for memory, there is not enough space for it to use (this is the most important);

2) Memory leak: means that the memory allocated by the program is no longer used and cannot be recycled;

3) Memory jitter: refers to the objects that the program creates and then recycles a large number of objects in a short period of time.


2. Then analyze the common scenarios that cause the above three memory problems in Android (the following why these problems caused by these scenarios are not explained, you can find "why" in some of the previous articles in the Android series).

1) Memory overflow: such as loading large images, it is easy to cause problems (this is the most important);

2) Memory leak: such as using Handler, it is easy to cause problems;

3) Memory jitter: such as when the Button button is clicked multiple times, it is easy to cause problems;


3. How to solve the problems caused by the scenarios I exemplified?

1) Memory overflow caused by loading large pictures: compress pictures, release memory in time, and display pictures (when listView is sliding, it does not make network requests for pictures, it is necessary to reuse convertView and use Lru caching mechanism). It is recommended to use some good image loading frameworks, such as Glide, Picasso, etc. (if it is an APP that specializes in loading pictures, it is recommended to use the Fresco picture loading framework).

2) For the memory leak when using Handler, you can check the correct use and analysis of the Android performance optimization handler in the previous articles

3. The memory jitter caused by clicking the button multiple times can be found in the article RxBinding2.0 in the Android Rx2.0 series. How to solve it.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325509585&siteId=291194637