It will help united states with several low-level problems including memories management, program sort dependencies, and so forth.

However we however often get crashes with OutOfMemory. So where’s the rubbish collector?

I’m attending give attention to among the cases where huge objects in memory can’t end up being cleaned for an extended time frame. This example just isn’t eventually a memory drip — objects is going to be obtained at some point — therefore we occasionally push it aside. This is not better as it can sometimes induce OOM problems.

The scenario I’m explaining will be the Handler leak, in fact it is usually found as a caution by Lint.

Standard Sample

That is an extremely basic activity. Realize that this private Runnable might uploaded with the Handler with a long wait. We’ll manage they and turn the device number of era, subsequently dump memories and review they.

We’ve seven recreation in memory space now. This is not at all good. Let’s figure out why GC struggles to clean them.

The query we meant to bring a listing of all recreation staying in storage was developed in OQL (item Query code), that will be quite simple, but powerful.

As you can plainly see, one of many strategies are referenced by this$0 . This is certainly an indirect research through the private lessons with the holder lessons. This$0 is actually referenced by callback , basically then referenced by a chain of after that ’s of Message back to the key thread.

Any time you produce a non-static class in the manager course, Java brings a secondary regard to the master

After you post Runnable or content into Handler , it’s then stored in list of information commands referenced from LooperThread till the information was accomplished. Uploading delayed emails is actually a definite drip for at least the full time with the delay price. Posting immediately could cause a short-term problem too when the waiting line of messages try large.

Static Runnable Answer

Let’s just be sure to mastered a memory drip by getting rid of this$0 , by transforming the unknown class to fixed.

Run, rotate to get the memory dump.

Just what, once again? Let’s discover just who helps to keep talking about tasks .

Have a look at the base of the forest — task is actually held as a regard to mContext inside mTextView of our DoneRunnable lessons. Using fixed inner classes isn’t adequate to tackle memory space leakages, nonetheless. We need to would more.

Fixed Runnable With WeakReference

Let’s keep using iterative repairs and obtain rid of the mention of TextView, which keeps task from being ruined.

Remember that the audience is maintaining WeakReference to TextView, and let’s operated, rotate and dump mind.

Be cautious with WeakReferences. They may be null any kind of time minute, therefore fix all of them initially to a regional adjustable (hard guide) following check always to null before usage.

Hooray! Only 1 activity example. This eliminates our memories challenge.

Very because of this means we should:

  • Utilize fixed interior tuition (or outside classes)
  • Utilize WeakReference to all objects controlled from Handler / Runnable

Any time you contrast this signal to your original rule, you will probably find a huge difference in readability and laws clearance. The first rule is much shorter and much clearer, and you’ll observe that eventually, book in textView would be changed to ‘Done’. You should not see the signal to realize that.

Writing that much boilerplate laws is very monotonous, particularly if postDelayed is defined to a short while, such as for instance 50ms. You will find much better and clearer possibilities.

Cleaning All Communications onDestroy

Handler class keeps an appealing ability — removeCallbacksAndMessages – which might take null as debate. It will eliminate all Runnables and emails uploaded to a specific handler. Let’s utilize it in onDestroy .

Let’s run, turn and dispose of memory.

Good! Singular case.

This process is actually a lot better than the earlier one, whilst helps to keep rule obvious and clear. The actual only real cost will be take the time to clean all messages on task / fragment kill.

We have an additional option which, if you’re sluggish at all like me, you will fancy further. 🙂

Usage WeakHandler

The Badoo staff came up with the interesting concept of introducing WeakHandler – a category that behaves as Handler , but is means less dangerous.

It can take advantage of tough and weakened records to eliminate memory leakage. I will explain the theory in more detail slightly later on, but let’s go through the rule 1st:

Much like the initial laws in addition to one small improvement — in place of utilizing android.os.Handler , I’ve utilized WeakHandler . Let’s operate, rotate and dispose of memory:

Nice, isn’t it? The rule is actually cleaner than before, and memories are clean nicely! 🙂

To use they, just include dependency towards build.gradle:

And free Beard dating import they within coffee class:

Consult Badoo’s github web page, where you can fork it, or learning it’s origin rule

WeakHandler. The way it operates

The primary aim of WeakHandler is always to hold Runnables / information hard-referenced while WeakHandler can hard-referenced. As soon as it can be GC-ed, all communications is going out also.

Here is straightforward diagram that displays differences between utilizing typical Handler and WeakHandler to publish unknown runnables:

Studying the leading drawing, task keeps a regard to Handler , which content Runnable (puts it into waiting line of communications referenced from Thread). Everything is fine except the secondary resource from Runnable to task . While content is in the waiting line, all graphs can’t feel garbage-collected.

By comparison, into the bottom diagram task retains WeakHandler , which keeps Handler internally. Once we inquire they to publish Runnable , truly covered into WeakRunnable and posted. Therefore, the information waiting line keeps reference only to WeakRunnable . WeakRunnable helps to keep weak reference to the required Runnable , therefore, the Runnable are garbage-collected.

Another little secret usually WeakHandler still keeps a tough mention of the specified Runnable , to prevent it from being garbage-collected while WeakRunnable is effective.

The side-effect of employing WeakHandler is all information and runnables might not be accomplished if WeakHandler happens to be garbage-collected. To prevent that, simply hold a reference to they from task. When Activity is preparing to feel accumulated, all graphs with WeakHandler will compiled as well.


Making use of postDelayed in Android os calls for further efforts. To experience it we developed three different methods:

  • Need a fixed interior Runnable / Handler with WeakReference to owner lessons
  • Evident all emails from Handler in onDestroy of Activity / Fragment
  • Utilize WeakHandler from Badoo as a sterling silver round

It’s for you to decide to decide on your preferred strategy. The next looks very affordable, but demands a little extra jobs. The 3rd try my favourite, obviously, it require some attention as well — WeakHandler really should not be utilised without tough reference from outdoors. And thank-you for reading!