Giter Site home page Giter Site logo

android-weak-handler's Introduction

Android Weak Handler

Memory safer implementation of android.os.Handler

Problem

Original implementation of Handler always keeps hard reference to handler in queue of execution. Any object in Message or Runnable posted to android.os.Handler will be hard referenced for some time. If you create anonymous Runnable and call to postDelayed with large timeout, that Runnable will be held in memory until timeout passes. Even if your Runnable seems small, it indirectly references owner class, which is usually something as big as Activity or Fragment.

You can read more on our blog post.

Solution

WeakHandler is trickier than android.os.Handler , it will keep WeakReferences to runnables and messages, and GC could collect them once WeakHandler instance is not referenced any more.

Screenshot

Usage

Add JitPack repository to your build.gradle:

repositories {
    maven { url 'https://jitpack.io' }
}

dependencies {
    implementation 'com.github.badoo:android-weak-handler:1.3'
}

Use WeakHandler as you normally would use Handler

import com.badoo.mobile.util.WeakHandler;

public class ExampleActivity extends Activity {

    private WeakHandler handler; // We still need at least one hard reference to WeakHandler

    protected void onCreate(Bundle savedInstanceState) {
        handler = new WeakHandler();
        ...
    }

    private void onClick(View view) {
        handler.postDelayed(new Runnable() {
            view.setVisibility(View.INVISIBLE);
        }, 5000);
    }
}

Credits

Weak Handler is brought to you by Badoo Trading Limited and it is released under the MIT License.

Blog

Read more on our tech blog or explore our other open source projects

android-weak-handler's People

Contributors

arkivanov avatar dmitry-voronkevich avatar lukaville avatar twlkyao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

android-weak-handler's Issues

optimize when msg.getCallback() not null

leak could still occur when used like bellow:
WeakHandler handler = new WeakHandler();
Message m = Message.obtain(handler.getExecHandler(), new Runnable() {
@OverRide
public void run() {
//do something
}
});
handler.sendMessageDelayed(m, delaytimes);

although postDelay() should be used here, but what if when we get message like this:
Message message = Message.obtain(m);
and m has a callback?

so maybe sendMessageXX() methods should be optimized:

public final boolean sendMessage(Message msg) {
    if (msg.getCallback() != null) {
        return mExec.post(msg.getCallback());
    }
    return mExec.sendMessage(msg);
}

public final boolean sendMessageAtFrontOfQueue(Message msg) {
    if (msg.getCallback() != null) {
        return postAtFrontOfQueue(msg.getCallback());
    }
    return mExec.sendMessageAtFrontOfQueue(msg);
}

public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
    if (msg.getCallback() != null) {
        return postDelayed(msg.getCallback(), uptimeMillis);
    }
    return mExec.sendMessageAtTime(msg, uptimeMillis);
}

public final boolean sendMessageDelayed(Message msg, long delayMillis) {
    if (msg.getCallback() != null) {
        return postDelayed(msg.getCallback(), delayMillis);
    }
    return mExec.sendMessageDelayed(msg, delayMillis);
}

Postdelay a runnable but does not run after activity recycled by GC

Some code in activity like this:

new WeakHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // do something
            }
        }, 10_000);
``
If the activity finished within 10 seconds, the runnable will not run.Is there any idea to run the runnable after activity destroyed?
P.S.
I found some reason in WeakHandler.java

final ChainedRef mRunnables = new ChainedRef(mLock, null);
//`mRunnables` recycled after activity destroyed so nothing keep reference to the following runnable

callback sometimes occurs null pointer

Just as #11 say:
callback sometimes occurs null pointer

I have an idea:
Check if tag of callback and tag of message-bundle are same by setting a tag when send message.

It can guarantee a one-to-one correspondence between callbacks and messages.

handleMessage method

the class has not the handlerMessage method.
when i send a Message,where get the mesage?

support handleMessage ???

Hi.it support handleMessage or not like this?

    Handler handlerButtonToggleAction = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    MediaArticle article = (MediaArticle) msg.obj;
                    if (article.isCached()) {
                        showArticleActionDialog(article);
                    } else {
                        toggleArticleCacheStatus(article);
                    }
                    break;
            }
        }
    };

In my project. i have a main handler in activity to update ui by message tag.
Thanks.

activity leaks

a non-mainlooper handler execute a long term runnable which get a refer to the current activity can still cause leaks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.