Many common phenomena such as singleton instances, static variables, cursor, stream are not closed caused by memory leaks I will not repeat, a large number of online search. However, I have found that many articles explain memory leaks caused by non-static inner classes inaccurately, so here is the correct understanding.

Said many articles of a static inner class foreign classes have an implicit reference, so lead to memory leaks, actually this interpretation is not accurate, because under normal circumstances, a static inner class instance and the external class really have reference to each other, but when the recovery of memory, such as recycling activity, if there are no other object reference internal and external class instance, Non-static inner class instances are reclaimed as a whole with no memory leaks.

The handler we commonly use can cause memory leaks because when an activity is recycled, the handler’s message queue contains unprocessed messages that refer to the handler, which in turn refers to the activity. The activity leaks. If you set handler to a static inner class, there will be no references to the activity. This can generally prevent leaks. However, if the contents of the message point directly to the activity, the activity may still leak. So the easiest way to do this is to clear the message in the handler when the activity is destroyed…