Android-) how to avoid Memory leakage caused by Handler

Source: Internet
Author: User

Android-) how to avoid Memory leakage caused by Handler
Error Code

If a variable Runnable is defined in Activiy by using the internal class runnable,

final Runnable runnable = new Runnable() {    public void run() {        // ... do some work    }};handler.postDelayed(runnable, TimeUnit.SECONDS.toMillis(10)

Because Runnable is not of the static type,Implicit reference--- Activity. this.

If the Activity is finished before the runnable variable run (within 10 s), but the Activity. this still exists, the Activity object will not be recycled by GC, resulting inMemory leak.

Even if a static internal class is used, nothing can be done.

static class MyRunnable implements Runnable {    private View view;    public MyRunnable(View view) {        this.view = view;    }    public void run() {        // ... do something with the view    }}

It is assumed that the View is removed before runnable execution, but the member variable view is still referencing it, it will still causeMemory leak.

In the two examples above, the two usage causes memory leakage are:Implicit reference)AndExplicit reference).


The method for solving implicit reference is relatively simple, as long as internal non-static inner class orTop-level class(Variables defined in an independent java file) can be implicitly changed to explicit to avoid Memory leakage.

If you continue to use non-static internal classes, you need to manually end those pending tasks in onPause ).

Handler can refer toCanceling a pending RunnableAndCanceling pending Messages. HandlerThread can refer to this article.

Use to solve the second problemWeakReferenceIn short, WeakReference can be referenced as long as there are other stronger references.

static class MyRunnable implements Runnable {    private WeakReference>View< view;    public MyRunnable(View view) {        this.view = new WeakReference>View<(view);    }    public void run() {        View v = view.get();        if (v != null) {            // ... do something with the view        }    }}

In this way, the problem is solved. The disadvantage in the United States is that the pointer should be short before each view is used. Another efficient method is to assign a value to the runnable view in onResume, and assign a value to null in onPause.

private static class MyHandler extends Handler {    private TextView view;    public void attach(TextView view) {        this.view = view;    }    public void detach() {        view = null;    }    @Override    public void handleMessage(Message msg) {        // ....    }

When inheriting Handler or HandlerThread,

Define a static or top-level class as much as possible. If the ui element is used, it must be released before the Activity lifecycle is exposed. Reference

Asynchronous Android-Steve Liles

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: 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.