- Static instances of non-static inner classes
Because the inner class holds a reference to an external class by default, the static instance belongs to the class. Therefore, when an external class is destroyed, the inner class still holds a reference to the external class, causing the outer class to not be recycled by the GC. Thus causing a memory leak.
The static variables of the class hold big Data Objects
Static variables maintain references to large data objects for a long time, preventing garbage collection.
Resource object not closed
Resource objects such as cursor, Stream, Socket,bitmap, should be closed in time after use. Failure to close in finally results in an exception where the resource object is not released.
Registration object is not anti-registered
We often write a lot of listener, not anti-registration will cause the Observer list to maintain the object's reference, prevent garbage collection.
Handler temporary memory leaks
Handler interacts with the main thread by sending a message, and the message is stored in MessageQueue after it is sent, and some messages are not processed immediately.
- Internal thread leaks new thread () {}.start (); one feature of thread in Java is that they are directly referenced by GC root, which means that the Dalvik virtual machine has strong references to all activated threads, causing the GC to never reclaim these thread objects. Unless the thread is manually stopped and set to null or the user directly killed the process operation. See this believe you should also have the answer in mind: I have created a thread in each mainactivity, this thread will hold the mainactivity reference, even if the current thread exits the activity because it is directly referenced by GC root so it will not be recycled. Causes Mainactivity to be recycled by GC as well. So when using a thread, be sure to consider stopping the thread and releasing it in time when the activity exits
- Context leaks
What happens to an Android memory leak