Android Best Practices: StrictMode

When developing Android applications that uses network and database access, you need to do performance tuning. This is where StrictMode comes in handy

StrictMode?

StrictMode is used to detect memory leaks. It’s has introduced in Gingerbread and can be used as a development tool.

The configuration below was the proposed in the reference documentation:

public void onCreate() {
     if (DEVELOPER_MODE) {
         StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                 .detectDiskReads()
                 .detectDiskWrites()
                 .detectNetwork()   // or .detectAll() for all detectable problems
                 .penaltyLog()
                 .build());
         StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                 .detectLeakedSqlLiteObjects()
                 .detectLeakedClosableObjects()
                 .penaltyLog()
                 .penaltyDeath()
                 .build());
     }
     super.onCreate();
 }

Update If you’re looking on how to get StrictMode working for all Android platform versions, have a look at this article.

Leave a Reply

*

captcha *