Knowledge Association from the Android handler internal class to the WeakReference

Source: Internet
Author: User

Handler: General usage:

The handler is used to process and get a message from the queue MessageQueue. In general, we want to rewrite Handler's handlemessage (Message msg) {} method to handle the following code:

public class Mainactivity extends Activity {    private TextView TextView;    Handler Normalhandler = new Handler () {@Overridepublic void Handlemessage (Message msg) {switch (msg.what) {case 1:LOG.I ("t Est ", textview.gettext (). toString ()); break;default:break;}};};}



Problem:

This time Handler will be checked by the Android SDK Lint tool to warn you (the yellow bulb + exclamation mark on the left): ThisHandler class should is static or leaks might occur.

Reason:

This Handler class should is static:

(Knowledge point one) why does the static inner class solve this problem? Or what is the difference between a static inner class and a non-static inner class?

Example: Class A{int A; static int B class b{} static Class c{}} (A is an outer class, B non-static inner class, C static inner class, A normal field, b static field)

1) b Non-static inner class:

You can access A.A and a.b, that is, the external properties can be azimuth. Because b implicitly holds a reference to a class object , which is equivalent to the property of a

2) c Static inner class:

C can only access a.b, can not be azimuth a.a. Why? Because C does not contain a reference to a, it is the same level as Class A, except that it is written to the inside of Class A.

This example causes:

Handler anonymous inner class implicitly holds references to external class activity (This is why you can invoke properties in Handlemessage () mainactivity TextView, etc.). ---> and later tune

Message message = Normalhandler.obtainmessage ();
Normalhandler.sendmessageattime (message, 100*1000);

The resulting message also contains a reference to the handler (see source).

After 100 seconds, the message is executed, during which the message is placed in MessageQueue, MessageQueue in Looper, and Looper is the local variable of the thread.

In other words, mainactivity will not be garbage collected even if the life cycle is finished. because Java's garbage collection mechanism is to see if an object has been referenced (starting from the main object in the thread, the reference between objects forms a network structure, if the object of the class is not on this web, it proves that it is not referenced.) This is the data structure of the graph traversal, what connected sub-graph, non-connected sub-graph. In this article, a mainactivity is handler hold a reference, handler by the message holding a reference, the message is MessageQueue hold a reference, MessageQueue is Looper hold a reference, Looper is a thread-local variable and the thread is not destroyed, it is not destroyed.

So even if the user has switched and exited to another activity,mainactivity the occupied memory will still not be released.


Solution:

Break Quote chain:

1.Message is processed after 100 seconds, then the message is recycled and the mainactivity is recycled. (So in fact, you won't have a problem if you don't send a message for a long time)

2. Make handler not hold the Mainactivity reference, use the weak reference weakreference: (Simply speaking, is only the object that the WeakReference refers to, garbage collection will reclaim the object, and then write another article of reference)


Normal code:

MyHandler handler = new MyHandler (this);p ublic static class MyHandler extends handler {private Weakreference<mainactiv Ity> reference;public MyHandler (mainactivity activity) {reference = new weakreference<mainactivity> (activity );} @Overridepublic void Handlemessage (Message msg) {switch (msg.what) {case 1:log.i ("Test", Textview.gettext (). ToString () ); break;default:break;}}}



Knowledge Association from the Android handler internal class to the WeakReference

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.