Android Performance Optimization code example (i)

Source: Internet
Author: User
<span id="Label3"></p><p><p>In the Android development process, often because the code is not standardized, the API is inappropriate, the Control's use of the scene is not comprehensive and user inappropriate operation, etc. can cause a series of performance problems, The following is some of the Android development process I have now to pay attention to the details, Just so-called a rat poo can break the porridge, details determine success or failure</p></p><p><p><strong>Here are some performance-optimized scenarios:</strong></p></p><p><p>1. Do not use enum in android, use static final instead of enum enum, because using enum consumes more memory space than using static</p></p><p><p>2. Use Getapplicationcontext () in toast to replace the context of the current activity, such as <code>Toast.makeText(getApplicationContext(),"",Toast.LENGTH_SHORT).show();</code><br>Because if the context of the current activity is used, when the user is improperly manipulated, such as by pressing the back key when the toast is displayed, and when the toast holds the context reference of the current activity, the memory leak is caused</p></p><p><p>3, the development of Android reasonable use of static, because the static life and application of the same length of life, if the variable can be then do not use static modification, so that it and actiity to maintain the same life cycle or to maintain the same life cycle</p></p><p><p>4, about the network request, if the use of very frequent will be the User's phone power and traffic caused relatively large losses, is so-called application development should put the user experience in the first place, so in the network request, should try to follow these two principles:<br>1, reduce the time and frequency of the mobile network activation (to Ensure that the application function is normal use of the Premise)<br>2, to some of the user interaction with the relatively large data (pictures, files, etc.), The data can be compressed</p></p><p><p>5, to prevent memory jitter, so-called memory jitter is to create a large number of objects in a short period of time, resulting in memory tension, and then trigger GC recovery, because when performing GC recovery, all the threads will be paused, including the main thread, only wait for the GC reclamation to complete other operations to continue, GC recycling is typically efficient, but if memory jitter is high, it can result in a less efficient GC Recovery. So memory jitter is likely to cause the interface to Stutter.<br>To prevent memory jitter:<br>1. You can use object pooling to manage objects, reduce the number of object creation times, and then manually release objects in the object pool after use is complete<br>2. Do not perform object creation in the for, while and other loop bodies<br>3. Avoid performing object creation in the OnDraw () method<br>4, using pre-allocation strategy to reduce the creation of a large number of data at once</p></p> <blockquote> <blockquote> <p><strong>pre-allocation:</strong> is to create some of the data that will be used in advance when the program is started, which provides a faster loading speed when you need to use this data, which is called pre-allocation</p> </blockquote> </blockquote><p><p>6. For Custom view, we</p></p> <ul> <ul> <li>We know that calling View.invalidate () will trigger a redraw of the view, with two principles to Follow:<br>1, only when the content of the view changes, only to trigger the Invalidate method<br>2, try to use Cliprect and other methods to improve the performance of the drawing</li> <li>Reduce the need to draw unnecessary drawing elements, for those invisible elements, we need to try to avoid redrawing</li> <li>Do not perform memory allocation operations in the OnDraw () method, such as: do not perform Paint mpaint = new paint () in the OnDraw () method;</li> <li>For elements that are not on the screen, you can use Canvas.quickreject to remove them and avoid wasting CPU Resources. In addition, the GPU is used to render the UI as much as possible, which can greatly improve the overall performance of the Program.</li> </ul> </ul> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced. Reprint Annotated Source: http://blog.csdn.net/u010687392</p></p> <p><p> Android Performance Optimization code example (i) </p> </p></span>

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.