{"id":6080,"date":"2017-06-21T13:05:16","date_gmt":"2017-06-21T13:05:16","guid":{"rendered":"https:\/\/9series-blog.staging9.com\/?p=6080"},"modified":"2026-03-03T10:45:11","modified_gmt":"2026-03-03T10:45:11","slug":"memory-and-code-optimization-tips-for-android-developers","status":"publish","type":"post","link":"https:\/\/www.9series.com\/blog\/memory-and-code-optimization-tips-for-android-developers\/","title":{"rendered":"Memory &amp; Code Optimization Tips for Android Developers"},"content":{"rendered":"<p style=\"margin-top: 15px;margin-bottom: 0px\"><span style=\"font-weight: bold\">1. Memory Optimization :<\/span><\/p>\n<p>There are wide range of devices in android, and each application perform differently on each device. So basically performance is one of the problem for <a href=\"https:\/\/www.9series.com\/services\/mobile-app-development.html\" target=\"_blank\">Android Application Developers<\/a>.<\/p>\n<p><a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2017\/06\/Memory-Code-Optimization-Tips-for-Android-Developers.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2570\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2017\/06\/Memory-Code-Optimization-Tips-for-Android-Developers.jpg\" alt=\"Memory &amp; Code Optimization Tips for Android Developers\" width=\"800\" height=\"500\" \/><\/a><\/p>\n<p><span style=\"font-weight: bold\">1 ) Don\u2019t Allocate Memory to Object if it is not needed <\/span>&#8211; Initializing the object at expensive places can affect the application performance a lot. It will call garbage collector frequently.<\/p>\n<p><span style=\"font-weight: bold\">Ex.<\/span> Avoid creating object inside loops and methods like onDraw().<\/p>\n<p><span style=\"font-weight: bold\">2 ) Choose Data Structure Carefully <\/span>&#8211;<\/p>\n<p style=\"margin-bottom: 0px\"><span style=\"font-weight: bold\">Ex.<\/span> We have one HashMap like<\/p>\n<p><a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2017\/06\/11.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2571\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2017\/06\/11.jpg\" alt=\"1\" width=\"800\" height=\"132\" \/><\/a><\/p>\n<p>It will consider <span style=\"font-weight: bold\">\u20181\u2019int<\/span> value which is primitive type and will allocate memory to wrap int to Integer which is also know as a Boxing technique that can be expensive in terms of performance of application.<\/p>\n<p style=\"margin-bottom: 0px\">We can use <span style=\"font-weight: bold\">Sparse*Array<\/span> which will reduce unnecessary object creation.<\/p>\n<p><a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2017\/06\/21.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2572\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2017\/06\/21.jpg\" alt=\"2\" width=\"800\" height=\"276\" \/><\/a><\/p>\n<p><span style=\"font-weight: bold\">3 ) Working with Image Bitmaps <\/span>&#8211; Bitmaps take lot of memory in our application so it is suggested that we should not load image in its full size , we should load the image in particular resolution which we are using it in our application.<\/p>\n<p><span style=\"font-weight: bold\">Ex.<\/span> Suppose we are showing the imageView of size 100 * 100 dp so we should load the image in same size, there are some common methods to perform in developer documentation of android.<\/p>\n<p><span style=\"font-weight: bold\">4 ) Use Cache Memory <\/span>&#8211; Cache allows you to use expensive object without recreating them. We can store such objects like image which we can\u2019t afford to load again and again.We can store it in Cache memory and load them directly from Cache.<\/p>\n<p><span style=\"font-weight: bold\">Ex.<\/span> Android provides LRUCache Class , which will perform such operations for us. Class has a Storage Limit. When class exceeds its limit it will automatically removes the least used object from the Cache.<\/p>\n<p>On other hand, we can also use some third party library to Cache the memory and avoid to load it again and again. Glide and Picasso are such libraries to achieve that.<\/p>\n<p><span style=\"font-weight: bold\">5 ) Use DiffUtil in RecyclerView <\/span>&#8211; DiffUtil uses Eugene W. Myer\u2019s difference algorithm to calculate the minimal number of updates to convert one list into another. Instead of notifying all items just notify those items which are changed. For more information refer this <a href=\"https:\/\/developer.android.com\/reference\/android\/support\/v7\/util\/DiffUtil.html\" target=\"_blank\">url<\/a>.<\/p>\n<p><span style=\"font-weight: bold\">6 ) Don\u2019ts use android:text in design layout files. <\/span>&#8211; Most of the <a href=\"https:\/\/9series.com\/blog\/bluetooth-low-energy-ble\/\" target=\"_blank\">mobile developers<\/a> uses android:text for layout design purpose and overwrites respected value from java classes which will writes text one time from xml layout and another from java files. So to overcome this, use tools:text instead of android:text<\/p>\n<p>Android Studio provides performance tool know as a Lint which will show us our potential error in the code. We have one variable that is created in onDraw method lint that shows error for the kind of code.<\/p>\n<p style=\"margin-bottom: 0px\"><span style=\"font-weight: bold\">2. Code Optimization :<\/span><\/p>\n<p>Optimization of the code is directly related to size of our application.<\/p>\n<p><span style=\"font-weight: bold\">1 ) Remove unused Resource <\/span>&#8211; Resource like image can take much size in our apk.so we should remove unused resources from res folder.<\/p>\n<p><span style=\"font-weight: bold\">Ex.<\/span> &#8211; There are two ways to remove unused resource from apk.<\/p>\n<p><span style=\"font-weight: bold\">CTRL + ALT + SHIFT + i <\/span>&#8211; Type unused resources and Enter which will give list of resources (drawables,strings etc.) which are not used in our app currently. We can delete them.<\/p>\n<p style=\"margin-bottom: 0px\"><span style=\"font-weight: bold\">Shrink unused resources from gradle file.<\/span><\/p>\n<p><a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2017\/06\/31.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2573\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2017\/06\/31.jpg\" alt=\"3\" width=\"800\" height=\"245\" \/><\/a><\/p>\n<p>Which will remove unused resource from your release apk.<\/p>\n<p><span style=\"font-weight: bold\">2 ) Don\u2019t use Images for all Density <\/span>&#8211; Android supports different density with different devices like ldpi,mdpi,hdpi,xxhdpi,xxhdpi. Use only those density images which you think large number of users are going to use. It is already recommended that all devices should use at least xxhdpi images.<\/p>\n<p><span style=\"font-weight: bold\">3 ) Avoid Frame by Frame Animation <\/span>&#8211; Frame by frame animation can enlarge for application, which is having multiple images for different density. Try to achieve such things with GIF images.<\/p>\n<p><span style=\"font-weight: bold\">4 ) Reuse Images <\/span>&#8211; We can use same resources like we need an image with mirror effect. So we can use same image with rotation.<\/p>\n<p style=\"margin-bottom: 0px\"><span style=\"font-weight: bold\">EX.<\/span><\/p>\n<p><a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2017\/06\/41.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2574\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2017\/06\/41.jpg\" alt=\"4\" width=\"800\" height=\"217\" \/><\/a><\/p>\n<p><span style=\"font-weight: bold\">5 ) Remove unused Code <\/span>&#8211; Nowadays we use so many third party libraries to achieve some features easily in our app which is nothing wrong to do, but there are some unused code and classes that takes place in application.<\/p>\n<p style=\"margin-bottom: 0px\">Android provides facility of Proguard to remove such code from your application.<\/p>\n<p><a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2017\/06\/5.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2575\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2017\/06\/5.jpg\" alt=\"5\" width=\"800\" height=\"265\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Memory Optimization : There are wide range of devices in android, and each application perform differently on each device. So basically performance is one of the problem for Android&#8230;<\/p>\n","protected":false},"author":1,"featured_media":6081,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"dsgo_overlay_header":false,"dsgo_overlay_header_text_color":"","dsgo_overlay_skip_top_bar":false,"_designsetgo_exclude_llms":false,"footnotes":""},"categories":[1449,6],"tags":[1776,1690,1777,2071,2072,1713],"class_list":["post-6080","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android-app-development","category-mobile-app-development","tag-android-app-development","tag-android-application-developers","tag-android-developers","tag-android-tips","tag-memory-and-code-optimization","tag-mobile-app-development"],"_links":{"self":[{"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/posts\/6080","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/comments?post=6080"}],"version-history":[{"count":1,"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/posts\/6080\/revisions"}],"predecessor-version":[{"id":6087,"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/posts\/6080\/revisions\/6087"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/media\/6081"}],"wp:attachment":[{"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/media?parent=6080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/categories?post=6080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/tags?post=6080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}