StrictMode: Your Main Thread's Watchdog
StrictMode is a developer tool that acts like a strict supervisor for your app's main thread, catching slow operations like disk or network access. It helps you find performance issues before they cause "Application Not Responding" errors.
Why it exists
The Android main thread is responsible for handling UI events and drawing. If you perform long-running tasks like reading a file or making a network request on it, the UI freezes. This leads to a poor user experience and "Application Not Responding" (ANR) dialogs. StrictMode was created to help developers find these violations during development, before they affect users.
The mental model
Think of StrictMode as a "nanny" for your app's main thread. You tell it the rules, like "no disk access" or "no network calls," and if the main thread breaks those rules, the nanny will tattle. It reports violations by logging to the console, flashing the screen, or even intentionally crashing the app, forcing you to fix the problem.
How it works
You enable StrictMode programmatically, typically in your Application or Activity's onCreate method. You configure policies for two different areas. First, a ThreadPolicy defines rules for the main thread, such as detecting disk reads, disk writes, or network requests. Second, a VmPolicy defines rules for the virtual machine, like detecting leaked SQLite cursors or unclosed resources. For each violation, you can specify a penalty, such as logging the stack trace (penaltyLog()) or crashing the app (penaltyDeath()).
When to use it
Use StrictMode exclusively during development and testing. It's an invaluable tool for catching performance bottlenecks and potential sources of ANRs early in the development cycle. It helps enforce good practices by making sure long-running work is offloaded to background threads.
When not to use it
Never enable StrictMode in production builds. The penalties are designed to be disruptive for developers, not end-users. Even with logging-only penalties, it adds performance overhead that is unnecessary for a shipped application. Always wrap your StrictMode setup in a check like if (BuildConfig.DEBUG).
One canonical example
In your Application class's onCreate method, you might add the following for a debug build: if (BuildConfig.DEBUG) { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build()); }. This setup will watch for any disk or network operations on the main thread and print a detailed stack trace to Logcat when one occurs.
Interview question
What is the main reason developers use StrictMode in Android applications?
- a.To ensure all user interface updates are executed on the main thread.
- b.To automatically fix performance issues by moving operations to background threads.
- c.To monitor and improve the overall performance of the app in production environments.
- d.To identify and debug long-running operations that block the UI thread during development.Correct
Why? this is the answer
StrictMode is a developer tool specifically designed to detect and report performance violations, such as disk or network access, on the main thread during development. It does not automatically fix issues or monitor production performance; its purpose is to help developers find and resolve these problems early.
Just read this? Test yourself on what you have been reading.
Read the original → developer.android.com
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.
See open roles