In this post, we will see the ways of detecting memory leaks in Android and…
Use StrictMode In Android to Rescue from your Accidental Doings
How is it possible in Android app development to detect and correct the mistakes you do! If you have not heard about this before, then Yes, you can do it using StrictMode, a tool that comes with Android.
What really is StrictMode?
StrictMode is a kind of debugging tool used by Android developers while developing applictions. When this tool is being used it looks out for the mistakes you are doing by accident and notifies those to your consent thus helping you rectify the issues on the go.
Most usually StrictMode is used to trace accidental network access or disk access on the main GUI thread. Generally, what the GUI thread does is it takes care of the animations and clicks happening on it. There will be no lags on the GUI thread until no process disturb it, but when issues like disk and network access hit the thread, it will go lagged for a long time and end with a dialog box asking the user to force quit or wait. So, it needs to be checked that the main thread is responsive. Keeing the thread responsive also helps the app to avoid Application Not Responding errors.
Let us see how we can use the tool in simple ways.
We should enable this in our onCreate() method which is the first of the activity lifecycle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public void onCreate() { if (DEVELOPER_MODE) { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads() .detectDiskWrites() // .detectAll() // to detect all issues .detectNetwork() .penaltyLog() .build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() .detectLeakedSqlLiteObjects() .detectLeakedClosableObjects() .penaltyLog() .penaltyDeath() .build()); } super.onCreate(); } |
Let us see some of the methods.
penaltyLog(): This method is used to log all the detected violations.
penaltyDeath(): If this is used, it will kill the whole process when a violation is found.
penaltyDeathOnNetwork(): This method would kill the whole process when a network usage is detected.
penaltyDialog(): This method splashes the developer with some dialog whenever it detects a violation.
penaltyFlashScreen(): This methdod flash the screen when a violation is found.
Another option for debugging is to use the auto generated BuildConfig.DEBUG block. When you are developing an app and use an emulator or a device to test your app, the BuildConfig.DEBUG will be automatically set to true. When you try to export the signed or unsigned Apk, it will be set to false.
1 2 3 |
if (BuildConfig.DEBUG) { //In debug mode } |
If you are tired of StrictMode warnings, just learn to differentiate the less violent and serious violent issues, so that you won’t force yourself to fix all the warnings the StrictMode shows you. Try to fix the issues that affect the quality of the application.
You can use a number of tools like threads, Handlers, AsyncTask, etc,. Other than finding Network access or Disk access, you can use StrictMode to trace out the things you accidentally do.
There are many policies and methods in StrictMode, each having a set of rules. It is always advised to follow the policies of using StrictMode. They include few best practices for how to avoid specific problems ike the type coding problems and more related to debugging.
This Post Has 0 Comments