Memory leaks are common errors

Context improper use of memory leaks: Do not keep for a long life cycle Activity Context reference. Try to be replaced in all applications can be used instead of Context ApplicationContext place.

When constructing Adapter, do not use cache convertView

Bitmap object calls recycle when not in use () to release memory

Static Examples of nonstatic inner classes is likely to leak memory: that is, a class if you can not control its life cycle in which an internal class (such as in an Activity some special Handler, etc.), as far as possible using static class and the weak references processes ( For example ViewRoot implementation).

Vigilance thread memory leaks caused by not terminated; for example, is associated with a life cycle of more than Thread Activity in Activity, remember the end of the thread upon exiting Activity. A typical example is the HandlerThread run method is an infinite loop, it will not end his thread over the life cycle of the Activity life cycle, we must manually destruction methods Activity in the transporting thread.getLooper (). Quit () ; it will not leak.

Object registration and anti-registration is not paired memory leak caused; broadcast receivers such as registration, registered observers (such as a typical monitor database) and so on.

Creating and closing does not occur caused by leaks in pairs; for example Cursor resources must be manually shut down, WebView must manually destroyed, flow and other objects must be manually shut down and so on.

Do not create a method or a high frequency of execution cycles objects (such onMeasure), and the like may be used to create a set of objects HashTable container those objects taken from the container, but not every new release.

Design pattern to avoid code errors caused by memory leak; circular reference example, A hold B, B hold C, C holders A, this design is not released anyone.

Reproduced in: https: //www.jianshu.com/p/cbe66d1dec4b

Guess you like

Origin blog.csdn.net/weixin_34195364/article/details/91290640