Baidu Search: Xiao Qiang test brand
QQ Group: 138269539
Handlers
Similarly, defining an anonymous Runnable object and committing it to Handler can also lead to activity leaks. The Runnable object indirectly refers to the activity object that defines it, and it is committed to the MessageQueue of Handler, which causes the activity to leak if it is not processed when the activity is destroyed.
Threads
Similarly, using Thread and TimerTask can also lead to activity leaks
Timer Tasks
As long as they are created through anonymous classes, they also hold strong references to activity, which can lead to memory leaks, even though they are executed on separate threads
Sensor Manager
Finally, system services can be obtained through context.getsystemservice, which are responsible for performing some background tasks or providing interfaces for hardware access. If the context object wants to be notified when an event inside the service occurs, it needs to register itself in the listener for the service. However, this will allow the service to have a reference to the activity, and if the programmer forgets to unregister when the activity is destroyed, it will cause the activity to leak.
Now, we've shown eight scenarios where it's easy to inadvertently leak large amounts of memory. Keep in mind that in the worst case scenario, your APP might run out of memory due to a large memory leak, but it's not always like this. Instead, a memory leak consumes a lot of memory, but does not run out of memory, and the APP is garbage collected frequently due to insufficient memory allocation. Garbage collection is a very time-consuming operation that can lead to serious stuttering. Be careful when creating objects inside activity, and often test for memory leaks.
Eight common issues that cause APP memory leaks (bottom)