Android Best performance Practice (i)--manage memory properly

Source: Internet
Author: User
<span id="Label3"></p><p><p>Reprint Please specify source: http://blog.csdn.net/guolin_blog/article/details/42238627</p></p><p><p>Memory (ram) is a very valuable resource for any software development environment, and for mobile operating systems, it will be more valuable, because the hardware of the mobile phone compared to the PC after all is relatively backward. While Android virtual machines have a mechanism to recycle garbage automatically, this does not mean that we can ignore when we should allocate and free memory.</p></p><p><p>In order for the garbage collector to free up the memory used by the program, it is important to avoid memory leaks when writing code (usually due to the fact that global member variables hold object references) and to release object references when Appropriate. For most applications, the rest of the application can be left to the garbage collector to complete, and if an object's reference is no longer held by another object, the system will reclaim the memory allocated by the Object.</p></p><p><p>We should take the memory problem into consideration when we develop the software, so that we can develop more high-performance software. And the memory problem is not irregular, the Android system gives us a lot of memory optimization tips, as long as these techniques to write programs, you can let our program In-store performance is very good, let's learn these techniques.</p></p>Use the service in moderation<p><p>If you need to use service to perform background tasks in your application, be sure to let the service run only when the task is Executing. In addition, when the task is finished to stop the service, be careful of the service stop failure resulting in a memory Leak.</p></p><p><p>When we start a service, the system tends to keep the process that the service relies on, which causes the process to become very memory-intensive. also, the number of processes that the system can cache in the LRU cache is reduced, resulting in more performance when switching applications. serious, It may even cause a crash, because the system may not be able to maintain the processes on which all running service depends when the memory is very tight.</p></p><p><p>To be able to control the service lifecycle, the best solution for Android is to use intentservice, the most important feature of which is that it stops automatically when the background task Executes. This avoids the possibility of service memory leaks to a great extent.</p></p><p><p><span style="line-height: 1.5;">Keeping a service running in the background, even if it doesn't do any work, is one of the worst ways to write Android Programs. So Android officials highly recommend that developers do not get too greedy and keep service running in the background, which not only may result in very low performance of mobile phones and programs, but also may cause our software to be uninstalled directly after being discovered by the user (i will do this personally).</span></p></p>Free memory when the interface is not visible<p><p>When the user opens another program and our program interface is no longer visible, we should release all resources that are related to the Interface. Releasing resources in this scenario significantly increases the ability of the system to cache the background process, thus making the user experience even Better.</p></p><p><p>So how do we know if the program interface is already invisible? In fact, it is very simple to rewrite the Ontrimmemory () method in the activity, and then in this method to listen to the level of trim_memory_ui_hidden, once triggered, it means that the user has left our program, The resource release operation is now available, as Follows:</p></p><strong><strong>[java]</strong></strong>View Plain<span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_168">Copy</span></span></span> <span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_169"> <span class=" Tracking-ad " data-mod=" popu_167 "><span class=" tracking-ad " data-mod=" popu_170 "> </span> </span> </span> </span></span> <ol class="dp-j" start="1"> <ol class="dp-j" start="1"> <li class="alt"><span class="annotation"> @Override    </span> </li> <li> <span class="keyword">public <span class="keyword">void ontrimmemory (<span class="keyword">int level)  {   </span> </span> </span> </li> <li class="alt">    <span class="keyword"> Super.ontrimmemory (level);    </span> </li> <li>     <span class="keyword">switch   (level)  {   </span> </li> <li class="alt">    <span class="keyword">case  trim_memory_ui_hidden:   </span> </li> <li>         <span class="comment">//  for resource release operations    </span> </li> <li class="alt">         <span class="keyword">break;   </span> </li> <li>     }   </li> <li class="alt">}   </li> </ol> </ol><p><p>Note that the Trim_memory_ui_hidden callback in the Ontrimmemory () method is triggered only when all the UI components in our program are not visible, which is quite different from the OnStop () method, because OnStop () The method is only called when an activity is completely invisible, such as when the user opens another activity in our Program. therefore, We can release some activity-related resources in the OnStop () method, such as canceling the network connection or unregistering the broadcast receiver, but the UI-related resources should wait until Ontrimmemory (trim_memory_ui_hidden) This callback is then released, which ensures that if the user only returns from one activity of our program to another activity, the interface-related resources do not need to be reloaded, thereby increasing the response Speed.</p></p><p><p></p></p>Freeing memory when memory is tight<p><p>In addition to the Trim_memory_ui_hidden this callback, the Ontrimmemory () method has many other types of callbacks that can be notified when the phone memory is Lowered. We should decide how to release the resources of the application based on the level passed in the Callback:</p></p> <ul> <ul> <li><strong>trim_memory_running_moderate</strong> indicates that the application is functioning properly and will not be killed. But now that the Phone's memory is a bit low, the system may start to kill the process according to the LRU cache Rules.</li> <li><strong>Trim_memory_running_low</strong> indicates that the application is functioning properly and will not be killed. But now that the Phone's memory is very low, we should release some unnecessary resources to improve the performance of the system, and this will directly affect the performance of our Application.</li> <li><strong>trim_memory_running_critical</strong> indicates that the application is still working, but the system has killed most of the cached processes according to the LRU cache Rules. At this time we should try to release any unnecessary resources, otherwise the system may continue to kill all the processes in the cache, and start to kill some of the processes that should be kept running, such as services running in the Background.</li> </ul> </ul><p><p>The above is the callback when our application is running, so if our program is currently cached, we will receive the following types of callbacks:</p></p> <ul> <ul> <li><strong>Trim_memory_background</strong> says the phone is currently low on memory and the system is ready to start cleaning up the process based on the LRU Cache. This time our program in the LRU cache list of the nearest location, is not likely to be cleaned out, but then to release some of the more easily recovered resources can make the Phone's memory more abundant, so that our program is kept in the cache for a longer time, so that when the user returned to our program will feel very smooth, Instead of going through a reboot process.</li> <li><strong>Trim_memory_moderate</strong> said the phone is currently low in memory, and our program is in the middle of the LRU cache list, if the phone memory is not further released, then our program has been killed by the system Risk.</li> <li><strong>Trim_memory_complete</strong> said the phone is currently low in memory, and our program is at the very edge of the LRU cache list, the system will give the highest priority to kill our application, at this time should be as much as possible to release everything that can be released.</li> </ul> </ul>Avoid wasting memory on bitmap<p><p>When we read a bitmap picture, It is important to note that you should never load the unwanted Resolution. Displaying a high-resolution image on a very small imageview does not bring any visual benefit, but it takes up quite a lot of our valuable memory. The only thing to remember is that the memory used to parse a picture into a bitmap object is not the size of the image on the hard disk, maybe a picture is only 100k you don't think it's big, but the memory is calculated by Pixel points, for example, This picture is 1500*1000 pixels, Using the argb_8888 color type, then each pixel will occupy 4 bytes, the total memory is 1500*1000*4 bytes, that is, 5.7M, this data looks more scary.</p></p>Using the optimized data collection<p><p>The Android API provides a number of optimized data collection tool classes, such as sparsearray,sparsebooleanarray, and longsparsearray, which can be used to make our programs more Efficient. The HashMap tool class provided in the traditional Java API is relatively inefficient because it requires an object entry for each key-value pair, and Sparsearray avoids the time when the base data type is converted to the object data type.</p></p>Know the cost of memory<p><p>We should also be aware of the memory expenditure and consumption of the language we use, and should take this information into account throughout the design and development of the Software. There may be some seemingly innocuous wording, which results in a large portion of the memory outlay, such as:</p></p> <ul> <ul> <li>Using enumerations is usually more than twice times more memory than using static constants, and in Android development we should try not to use enumerations as much as Possible.</li> <li>Any Java class, including internal classes, Anonymous classes, consumes approximately 500 bytes of memory space.</li> <li>An instance of any class consumes 12-16 bytes of memory overhead, so creating instances frequently can also affect memory on a Program.</li> <li>When using hashmap, even if you only set a key for a basic data type, such as int, it allocates memory according to the size of the object, which is about 32 bytes, not 4 Bytes. So the best way to do this is to use an optimized set of data, as mentioned Above.</li> </ul> </ul>Use abstract programming with caution<p><p>Many programmers like to use abstractions to program, thinking that this is a good programming habit. of course, This is undeniable, because the abstract programming approach is more object-oriented and will improve the maintenance and extensibility of the Code. however, using abstractions on Android brings additional memory overhead, because abstract programming methods require additional code that is not executed at all, but also mapped into memory, which not only takes up more memory, but also decreases execution efficiency. of course, I'm not advocating that you don't use abstract programming at all, but use abstract programming with caution, and don't think of it as a cool way to use it and use it only when you think It's necessary.</p></p>Try to avoid using the dependency injection framework<p><p>Many people now like to use dependency injection frameworks in Android projects, such as Guice or roboguice, because they can simplify some complex coding operations, such as the following code:</p></p><strong><strong>[java]</strong></strong>View Plain<span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_168">Copy</span></span></span><span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_169"> <span class="tracking-ad" data-mod="popu_167"><span class="tracking-ad" data-mod="popu_170"></span></span></span></span></span> <ol class="dp-j" start="1"> <li class="alt"><li class="alt"><span class="keyword">Class Androidway <span class="keyword">extends Activity {</span></span></li></li> <li><li>TextView name;</li></li> <li class="alt"><li class="alt">ImageView thumbnail;</li></li> <li><li>Locationmanager loc;</li></li> <li class="alt"><li class="alt">Drawable icon;</li></li> <li><li>String myName;</li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="keyword">public <span class="keyword">void OnCreate (Bundle Savedinstancestate) {</span> </span></li></li> <li class="alt"><li class="alt"><span class="keyword">super.oncreate (savedinstancestate); </span></li></li> <li><li>Setcontentview (r.layout.main);</li></li> <li class="alt"><li class="alt">Name = (TextView) Findviewbyid (r.id.name);</li></li> <li><li>thumbnail = (ImageView) Findviewbyid (r.id.thumbnail);</li></li> <li class="alt"><li class="alt">loc = (locationmanager) Getsystemservice (activity.location_service);</li></li> <li><li>icon = getresources (). getdrawable (r.drawable.icon);</li></li> <li class="alt"><li class="alt">MyName = getString (r.string.app_name);</li></li> <li><li>Name.settext ( <span class="string">"Hello," + myName); </span></li></li> <li class="alt"><li class="alt">}</li></li> <li><li>}</li></li> </ol><p><p>Simplify it into one of the following:</p></p><strong><strong>[java]</strong></strong>View Plain<span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_168">Copy</span></span></span><span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_169"> <span class="tracking-ad" data-mod="popu_167"><span class="tracking-ad" data-mod="popu_170"></span></span></span></span></span> <ol class="dp-j" start="1"> <li class="alt"><li class="alt"><span class="annotation">@ContentView (r.layout.main)</span></li></li> <li><li><span class="keyword">Class Roboway <span class="keyword">extends Roboactivity {</span></span></li></li> <li class="alt"><li class="alt"><span class="annotation">@InjectView (r.id.name) TextView name; </span></li></li> <li><li><span class="annotation">@InjectView (r.id.thumbnail) ImageView thumbnail; </span></li></li> <li class="alt"><li class="alt"><span class="annotation">@InjectResource (r.drawable.icon) drawable icon; </span></li></li> <li><li><span class="annotation">@InjectResource (r.string.app_name) String myName; </span></li></li> <li class="alt"><li class="alt"><span class="annotation">@Inject Locationmanager loc; </span></li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="keyword">public <span class="keyword">void OnCreate (Bundle Savedinstancestate) {</span> </span></li></li> <li><li><span class="keyword">super.oncreate (savedinstancestate); </span></li></li> <li class="alt"><li class="alt">Name.settext ( <span class="string">"Hello," + myName); </span></li></li> <li><li>}</li></li> <li class="alt"><li class="alt">}</li></li> </ol><p><p>It looks really tempting, and we can even omit all the tedious operations of Findviewbyid (). But these frameworks often need to go through a long initialization process to search for annotations in the code, and may also load objects that you can't use into memory. These objects will always occupy the memory space, it may take a long time before they will be released, compared to the number of lines of the seemingly cumbersome code is a better choice.</p></p><p><p></p></p>Simplifying code with Proguard<p><p>Proguard believe that everyone is not unfamiliar, many people will use this tool to confuse the code, but in addition to confusion, it also has the ability to compress and optimize the Code. Proguard will retrieve our code, remove some useless code, and rename classes, fields, methods, and so on, and rename the classes, fields, and method names to be much shorter than they were originally, so that the memory footprint becomes LESS.</p></p>Using multiple processes<p><p>This technique is not really recommended, but it is really an advanced technique that can help us save and manage memory. If you want to use it, be sure to use it sparingly, because most applications should not run in more than one process, and if used improperly, it will even add extra memory instead of saving memory for Us. This technique is more suitable for those who need to perform a separate task in the background, and the function of the foreground can be completely separated from the SCENE.</p></p><p><p>Here is a better way to use multi-process skills, such as we are working on a music player software, which plays the music function should be a separate function, it does not need to have any connection with the ui, even if the software is closed should be able to play the music properly. If we only use one process at this point, then even if the user shuts down the software and the service is completely controlling the music playback, the system will still retain many of the UI Memory. This scenario is ideal for use with two processes, one for UI presentation and the other for continuous music playback in the Background.</p></p><p><p>The ability to implement multi-process is also very simple, just need to declare a android:process attribute in the application component of the Androidmanifest file, for example, we want to play music service can run in a separate process, It can be written like this:</p></p><strong><strong>[java]</strong></strong>View Plain<span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_168">Copy</span></span></span> <span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_168"><span class="tracking-ad" data-mod="popu_169"> <span class="tracking-ad" data-mod="popu_167"><span class="tracking-ad" data-mod="popu_170"></span></span></span></span></span> <ol class="dp-j" start="1"> <ol class="dp-j" start="1"> <li class="alt"><service android:name=<span class="string">". Playbackservice "</span></li> <li>android:process=<span class="string">": background"/></span></li> </ol> </ol><p><p>The process name specified here is background, and you can change it to any name you like. Note that a colon should precede the process name to indicate that the process is a private process for the current Application.</p></p><p><p>Android Best performance Practice (i)--manage memory properly</p></p></span>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.