{"id":869,"date":"2011-10-26T23:05:37","date_gmt":"2011-10-27T06:05:37","guid":{"rendered":"http:\/\/wiebe-elsinga.com\/blog\/?p=869"},"modified":"2011-11-15T22:53:35","modified_gmt":"2011-11-16T05:53:35","slug":"869","status":"publish","type":"post","link":"http:\/\/wiebe-elsinga.com\/blog\/869\/","title":{"rendered":"Android Best Practices: StrictMode"},"content":{"rendered":"<div class=\"wpsso-pinterest-pin-it-image\" style=\"display:none !important;\">\n<\/div><!-- .wpsso-pinterest-pin-it-image -->\n\n<p>When developing Android applications that uses network and database access, you need to do performance tuning. This is where StrictMode comes in handy<\/p>\n<h3>StrictMode?<\/h3>\n<p>StrictMode is used to detect memory leaks. It&#8217;s has introduced in Gingerbread and can be used as a development tool.<\/p>\n<p>The configuration below was the proposed in the <a href=\"http:\/\/developer.android.com\/reference\/android\/os\/StrictMode.html\" target=\"_blank\">reference<\/a> documentation:<\/p>\n<div class=\"codecolorer-container java default\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><div class=\"java codecolorer\"><span class=\"kw1\">public<\/span> <span class=\"kw4\">void<\/span> onCreate<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp;<span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>DEVELOPER_MODE<span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;StrictMode.<span class=\"me1\">setThreadPolicy<\/span><span class=\"br0\">&#40;<\/span><span class=\"kw1\">new<\/span> StrictMode.<span class=\"me1\">ThreadPolicy<\/span>.<span class=\"me1\">Builder<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span class=\"me1\">detectDiskReads<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span class=\"me1\">detectDiskWrites<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span class=\"me1\">detectNetwork<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> &nbsp; <span class=\"co1\">\/\/ or .detectAll() for all detectable problems<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span class=\"me1\">penaltyLog<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span class=\"me1\">build<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;StrictMode.<span class=\"me1\">setVmPolicy<\/span><span class=\"br0\">&#40;<\/span><span class=\"kw1\">new<\/span> StrictMode.<span class=\"me1\">VmPolicy<\/span>.<span class=\"me1\">Builder<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span class=\"me1\">detectLeakedSqlLiteObjects<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span class=\"me1\">detectLeakedClosableObjects<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span class=\"me1\">penaltyLog<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span class=\"me1\">penaltyDeath<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span class=\"me1\">build<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp;<span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; &nbsp;<span class=\"kw1\">super<\/span>.<span class=\"me1\">onCreate<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp;<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p><strong>Update<\/strong> If you&#8217;re looking on how to get StrictMode working for all Android platform versions, have a look at <a href=\"http:\/\/www.simpligility.com\/2011\/01\/android-strictmode-for-all-platform-versions\/\" target=\"_blank\">this<\/a> article.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;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&#40;&#41; &#123; &nbsp; &nbsp; &nbsp;if &#40;DEVELOPER_MODE&#41; &#123; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;StrictMode.setThreadPolicy&#40;new StrictMode.ThreadPolicy.Builder&#40;&#41; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.detectDiskReads&#40;&#41; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.detectDiskWrites&#40;&#41; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.detectNetwork&#40;&#41; &nbsp; \/\/ or .detectAll() for all detectable problems &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.penaltyLog&#40;&#41; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.build&#40;&#41;&#41;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;StrictMode.setVmPolicy&#40;new StrictMode.VmPolicy.Builder&#40;&#41; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.detectLeakedSqlLiteObjects&#40;&#41; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.detectLeakedClosableObjects&#40;&#41; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.penaltyLog&#40;&#41; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.penaltyDeath&#40;&#41; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.build&#40;&#41;&#41;; &nbsp; &nbsp; &nbsp;&#125; &nbsp; &nbsp; &nbsp;super.onCreate&#40;&#41;; &nbsp;&#125; Update If you&#8217;re looking on how to get StrictMode working for all Android platform versions, have a look at this article.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[154,98,97],"_links":{"self":[{"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/posts\/869"}],"collection":[{"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/comments?post=869"}],"version-history":[{"count":0,"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/posts\/869\/revisions"}],"wp:attachment":[{"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/media?parent=869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/categories?post=869"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/tags?post=869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}