Introduction
What is an internal class? What is a memory leak? Why is Android's internal class prone to memory leaks? How to solve?
What is an internal class?
What is an internal class? What is an external class, an anonymous class, a local class, a top-level class, a nested class? You can refer to my article, and then look up some information, first find out what is the internal class and the characteristics of the inner class and then look down.
You will often encounter Android programs that use handler:
Public classsomeactivity {// ...... PrivateHandler Mhandler =NewHandler () {@Override Public voidhandlemessage (Message msg) {Switch(msg.what) { Case0: //Do something Break; Case1: //Do something Break; default: Break; } } }; Private voidSomeMethod () {Mhandler.sendemptymessage (0); }}
In the preceding code, the Mhandler field points to an anonymous handler class. is an anonymous class an inner class? Does an anonymous class hold an object of an external class? The answer is: The anonymous class is an inner class, but it is a special inner class, and if you put an anonymous class in a static method, it will not hold an instance of the external class. In the above code, this mhandler holds a reference to an external class (someactivity) instance because it is in the context of an object, not in the type context.
What is "holding references to external class instances"? You can understand this:
Public class innerclass { private outerclass outer; Public innerclass (Outerclass outer) { this. Outer = outer; }}
That is, when you create a Innerclass object, you must pass in a Outerclass object that assigns a field outer to Innerclass, which is a reference to the Outerclass object. Recall the GC principle, if the Innerclass object is not marked as a garbage object, then the Outerclass object that outer points to can be marked as a garbage object? The answer is: the Innerclass object and the GC root have a reference path, the Innerclass object also references the Outerclass object, then the Outerclass object to the GC root also has a reference path, so, Outerclass cannot be garbage object.
Why is there a memory leak?
As can be seen above: When Mhandler is not recycled, its peripheral activity objects cannot be recycled. When activity is closed by the user (finish), and the Mhandler is not yet recycled, the activity object is not recycled, causing activity memory leaks.
The crux of the problem has shifted to the question: why is the activity being mhandler and not being recycled?
We used this function when sending a message: the Mhandler.sendemptymessage (0) function. By looking at the source tracking call relationship, we found that we came to:
The message object has a Target field, which is a handler type that references the current handler object. The bottom line is that you hold a reference to the handler object by handler the message object that is destined for messages queue. If the message object has been disposed of in the queue without being disposed of, your handler object will not be freed and your activity will not be released.
This is a common phenomenon, when Message Queuing contains a large amount of message waiting to be processed, you send a message to wait a few seconds to be processed, and when you close the activity, you will cause a memory leak. If you send some delay messages frequently, even if the message queue is not busy, shutting down the activity before the delay arrives can also cause a memory leak.
What are the solutions?
Scenario #: When you close activity (in functions such as finish/onstop), the message that is still queued is canceled:
Mhandler.removecallbacksandmessages (NULL);
Scenario # #: Truncate strongreference with WeakReference. The crux of the problem since it is an inner class that holds a reference to an external class object, I do not have to use the inner class, directly using the static member class. But Mhandler also needs to interact with the activity object, then a weakreference, pointing to the external activity object.
Public classsomeactivity {PrivateHandler Mhandler =NewMyHandler ( This); Private Static classMyHandlerextendsHandler {PrivateWeakreference<someactivity>ref; PublicMyHandler (someactivity activity) {if(Activity! =NULL) {ref=NewWeakreference<someactivity>(activity); }} @Override Public voidhandlemessage (Message msg) {if(ref = =NULL) { return; } someactivity v=Ref.get (); if(v = =NULL) { return; } //Handle Message } }}
When the activity wants to close the destruction, the Mhandler has no effect on its weak references, and the destruction is destroyed; when Mhandler through WeakReference, the activity is destroyed, it is not processed. Equivalent to discarding the message.
In addition, when you use handler to have memory leaks, Android Studio Lint will have the following tips:
(Originally published inside the company)
Memory leaks caused by internal classes in Android