Application of WeakReference in Handler, weakhandler

Source: Internet
Author: User

Application of WeakReference in Handler, weakhandler

 

 1 public class AutoActivity extends Activity { 2  3     Handler handler = new Handler(){ 4         public void handleMessage(android.os.Message msg) { 5              6         }; 7     }; 8     @Override 9     protected void onCreate(Bundle savedInstanceState) {10         super.onCreate(savedInstanceState);11         setContentView(R.layout.activity_auto);12     }13 }

The above code generates a warning when the handler object is created: This Handler class shocould be static or leaks might occur. Handler

Class should be static type, otherwise it may cause memory leakage.

Why does this happen?

This situation is caused by the special mechanism of android: When an android main thread is created, a logoff object is also created, this loose object implements a MessageQueue (Message Queue). When we create a handler object, handler is used to put and retrieve messages from this message queue, every time we put a msg into the Message Queue through handler, This msg will hold a reference to the handler object. Therefore, after the Activity ends, the msg will survive before it is retrieved, but the msg holds the reference of handler, And the handler creates in the Activity, will hold the reference of the Activity, so when the Activity ends, the Activity object cannot be recycled by gc, So memory leakage occurs.

The root cause is that after the Activity is ended, MessageQueue will not end. If msg exists in the message queue, it will lead to reference of handler,

Because the Activity is ended, msg cannot be processed, resulting in permanent possession of the handler object and permanent possession of the Activity object, resulting in Memory leakage. But why is it static?

Will this problem be solved? In java, all non-static objects hold strong references of the current class, while static objects only hold weak references of the current class. When declared as static, handler will hold

There is a weak reference of the Activity, and the weak reference will be easily recycled by gc, so that it can solve the problem that gc cannot be recycled after the Activity ends.

 

There are several ways to solve this warning:

1. Declare the hanlder object as a static object.

Ii. use static internal classes to implement weak reference to Activity through WeakReference. For specific implementation, see the following code:

1 public class AutoActivity extends Activity {2 3 MyHandler handler = new MyHandler (this); 4 @ Override 5 protected void onCreate (Bundle savedInstanceState) {6 super. onCreate (savedInstanceState); 7 setContentView (R. layout. activity_auto); 8} 9 10 static class MyHandler extends Handler {11 WeakReference <AutoActivity> mactivity; 12 13 public MyHandler (AutoActivity activity) {14 mactivity = new WeakReference <AutoActivity> (activity); 15} 16 17 @ Override18 public void handleMessage (Message msg) {19 super. handleMessage (msg); 20 switch (msg. what) {21 case 100: 22 // here msg23 is processed // Through mactivity. get () get Activity reference (context) 24 break; 25 default: 26 break; 27} 28} 29} 30}

 

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.