[MUSIC PLAYING]
Recognize this?
It's the dreaded ANR, the application not responding
dialogue.
Android shows this dialogue when an app
has taken more than five seconds to return from an event
handler.
But why would that happen?
Well it's because the UI thread is blocked.
It's waiting for something.
And since the UI thread is the only thread
than can process input and draw to the screen, when it stops
responding, so does your app.
So what can cause the UI thread to just stop like that?
Well, the usual suspects are system calls
that can block indefinitely, like disk or network access.
These blocking calls are like time bombs in your application.
Under normal conditions, they might execute so fast
you barely notice them.
But with the right combination of hardware and software
and luck, they explode.
My name is Ian Ni-Lewis, and I'm going
to show you how to find blocking calls in your UI thread
before they blow up on you.
Now the tool that can help us here is called strict mode.
You can get to it by going to Developer Options
and selecting Strict Mode Enabled.
And when this option is enabled, anytime a blocking call
is made on the UI thread, the screen border will flash red.
Now that's the strict mode alarm.
It signals a potential performance problem.
For example, let's say your app suddenly
decides to, I don't know, load 90 bitmaps
during a scroll event.
With strict mode on, you'll know about that
because you'll see that red flash.
But there's more to strict mode than just a checkbox.
Strict mode is actually a flexible and fine-grained API
that gives you control over what every thread in your app
is allowed to do.
It's kind of like a run-time version of Lint.
Now using strict mode means defining a policy
for one or more of the threads in your app.
Now policy is an object that's composed of two components,
a detection mask and a penalty.
Now the detection mask is a set of flags
that defines the things that you want to test for like disk
reads or network access.
The penalty tells the system what
to do when any one of those flag conditions are met.
The API in strict mode gives you a whole set of penalty options,
from flashing the screen, like we saw earlier,
to writing to the system log, all the way up
to crashing your process.
You can also use strict mode to respond to custom conditions
that you detect.
Let's say, for instance, that you
know that one of your methods is too
slow to run on the UI thread.
To detect that with strict mode, you
call StrictMode.noteSlowCall at the beginning of the method.
And make sure you enable detectCustomSlowCalls
in the policy for the UI thread.
Now when detectCustomSlowCalls is enabled,
a call to noteSlowCall will raise an exception,
and then strict mode will apply whatever penalty that you've
defined for that thread.
Now if detectCustomSlowCalls is disabled,
then noteSlowCall is a NOOP.
Oh, here's a quick pro tip.
Call noteSlowCall whenever you acquire a lock.
Why?
Well, locks are kind of like stealth blocking calls.
They often return immediately, so fast
that you don't even notice they're there.
But if the lock is contended, your thread
will get switched out, with no guarantee
on when it gets to run again.
Now locks won't usually cause an ANR all by themselves,
but if you call a synchronized method
or acquire a lock on your UI thread,
sooner or later you're going to drop a frame.
Now strict mode also has an API to set the policy
for the virtual machine.
That's the VM policy.
Now where the thread policy has options for detecting slower
blocking methods, the VM policy gives you
options for detecting various kinds of memory leaks.
Now these two policies complement each other.
Remember, if you're low on memory,
even a normal allocation can block
for hundreds of milliseconds.
Now chances are there's at least one
blocking call on your app's UI thread,
just waiting to cause a glitch, a
dropped frame, or even an ANR.
Strict mode will help you find those time bombs.
In an upcoming video, we'll talk about how
to diffuse them, which is why you need to check out
the rest of our Android Performance Patterns
content for other ways to improve your performance.
And don't forget to join our Google community
for more excellent info.
So keep calm, profile your code, and always remember,
perf matters.
[MUSIC PLAYING]