Knowledge Association from the Android handler internal class to the WeakReference

Source: Internet
Author: User
Tags gettext

Handler: General usage:

The handler is used to process and get a message from the queue MessageQueue. Generally we want to rewrite Handler's handlemessage (Message msg) {} method to handle, for example, 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:

(Point one) why do static internal classes solve problems? 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:

Access to A.A and a.b, that is, the external properties can be azimuth.

Because b implicitly holds a reference to the Class A object . Properties equivalent to a

2) c Static inner class:

C can only access the A.B, can not be azimuth a.a. Why? Since C does not contain a reference to a. It is the same level as Class A. Just write 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 (able to see the source code).

After 100 seconds, the message is run, 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, assuming that 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.

One of the mainactivity in this article is referenced by handler. Handler is referenced by the message, the message is MessageQueue holding a reference, MessageQueue is Looper hold the reference, Looper is a thread-local variable, and the thread is not destroyed. It will not be destroyed.

So even if the user has switched and exited to another activity. The memory occupied by Mainactivity is still not released.


How to resolve:

Break Quote chain:

The 1.Message is processed after 100 seconds. The message is then recycled and then recycled mainactivity.

(So, in fact, you're not going to have a problem with a message that's only a very long time)

2. Make handler not hold the Mainactivity reference, use the weak reference weakreference: (Simply speaking, there is only the object of WeakReference reference.) Garbage collection will reclaim the object and write another referenced article later.)


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.