This Handler class shocould be static or leaks might occur, handlerleaks

Source: Internet
Author: User

This Handler class shocould be static or leaks might occur, handlerleaks

In Android, if Handler is used, write the following code:

private Handler handler;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);handler = new Handler() {@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubsuper.handleMessage(msg);}};handler.sendMessageDelayed(Message.obtain(), 60000);// just finish this activityfinish();}

Then run the Android Lint tool with a memory leak warning:

This Handler class shocould be static or leaks might occur (com. example. ta. MainActivity.1)

Issue: Ensures that Handler classes do not hold on to a reference to an outer class
Id: HandlerLeak

In Android, Handler classes shoshould be static or leaks might occur. messages enqueued on the application thread's MessageQueue also retain their target Handler. if the Handler is an inner class, its outer class will be retained as well. to avoid leaking the outer class, declare the Handler as a static nested class with a WeakReference to its outer class.

The reason is:

  1. When the Android Application Starts, A logoff object of the main application thread will be created first. logoff implements a simple Message queue to process the Message objects one by one. The logoff object of the main thread exists throughout the application lifecycle.

  2. When Handler is initialized in the main thread, the Handler is associated with the logoff message queue. The Message sent to the Message Queue references the Handler object that sends the Message, so that the system can call Handler # handleMessage (Message) to distribute and process the Message.

  3. In Java, non-static (anonymous) Internal classes reference external class objects. Static internal classes do not reference external class objects.

  4. If the external class is Activity, it will cause Activity leakage.

After the Activity is finished, the delayed message will continue to exist in the main thread message queue for one minute, and then process the message. The message references the Handler object of the Activity, and then the Handler references the Activity. These referenced objects are retained until the message is processed. As a result, the Activity object cannot be recycled, leading to Activity leakage.

To modify this issue, you only need to define the Handler class as static as prompted by Lint, and then use WeakReference to keep external Activity objects.


Private static class WebViewHandler extends Handler {public static final int what_pullRefreshCompleted = 1; private final WeakReference <WebActivity> mActivity; public WebViewHandler (WebActivity mActivity) {super (); this. mActivity = new WeakReference <WebActivity> (mActivity) ;}@ Overridepublic void handleMessage (Message msg) {if (mActivity. get () = null) return; switch (msg. what) {case what_pullRefreshCompleted : If (mActivity. get (). getmPullRefreshWebView ()! = Null) // because the Handler is static, we call the method to obtain the external class attribute mActivity. get (). getmPullRefreshWebView (). refreshComplete (); break;} super. handleMessage (msg );}}

Therefore, when using an internal class in an Activity, you must always consider whether you can control the lifecycle of the internal class. If not, you 'd better define it as a static internal class.



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.