Big summary of Android development performance optimization

Source: Internet
Author: User

I. Android-related

1. With hardware acceleration, application Add android:hardwareaccelerated= "true" in Androidmanifest.xml. But this needs to be used on Android 3.0. android4.0 This option is turned on by default.
2. Set cache properties in view. Setdrawingcache is true.
3. Optimize your layout. Use the layoutopt command in the Tools directory in the Android SDK to see if your layout needs to be optimized.
4. Load view dynamically. Use Viewstub to avoid some infrequently held views for long-term hold of references.
5. Set the background image of window in acitivity to empty. GetWindow (). setbackgrounddrawable (null); The default background for Android is not empty.
6. Use <merge> to optimize the layout layer number. Use <include > to share layouts.
7. View the size of the heap
8. Use TraceView to view trace function calls. Targeted optimization.
9. Use of the cursor. However, be careful to manage the cursor, do not open the cursor every time. It is very time-consuming to turn the cursor on. The cursor.require is used to brush the cursor.
10. Use ring buffer (can be implemented using linked list data structure). You can set an upper limit on the length of a list, and constantly update the contents of the ring buffer according to the gesture changes.
11. Use Surfaceview to refresh the UI in a child thread, avoiding gesture processing and drawing on the same UI thread (normal view does).
12. Use JNI to place time-consuming processing on a C/C + + layer.
13. Some can use file operations, as far as possible to use file operations, file operations faster than the database operation is about 10 times times faster.
14. Lazy Loading and caching mechanisms. The time-consuming operation to access the network starts a new thread to do instead of the UI thread.
15. Avoid creating unnecessary objects
16. If the method does not use a member variable, the method can be declared as static, the performance will be increased to 15% to 20%
17. Avoid using Getter/setter to access field, declare field as public, direct access
Variables for static If no modifications are required, the static final modifier should be used to define the constant
19. Using the enhanced for loop
such as:set<object> Set = new hashset<object> ();
For loop traversal:
for (Object Obj:set) {
if (obj instanceof Integer) {
int aa= (Integer) obj;
}else if (obj instanceof String) {
String AA = (string) obj

} ........
}
Disadvantage: The collection itself cannot be manipulated during the traversal of the collection

for (String Str:set) {
Set.remove (str);//Error!
}
20. Private inner class to access the field or method of an external class, its member variables do not use private, because Setter/getter is generated at compile time, affecting performance. You can declare a field or method of an external class as a package access permission

21. Make reasonable use of floating point number, floating point number is twice times slower than integer type;

22. Performance optimizations for the ListView

Item minimizes the level of controls and layouts used, the background color sets the same color as the Cachecolorhint, and the layout of item in the ListView is critical, and you must minimize the layout of the controls you use. Relativelayout is an absolute tool through which you can reduce the level of layout. Reuse the control as much as possible, which reduces the memory usage of the ListView and reduces the number of GC times when sliding. The background color of the ListView is set to the same color as the cachecolorhint, which improves rendering performance when sliding. The GetView in the ListView is the key to performance, which should be optimized as much as possible. In the GetView method, it is not possible to do complicated logical computation in the View;getview method, especially the database operation, otherwise it will seriously affect the performance of sliding.

Two. Java-related

1 Create an instance of the class without the new keyword, and when you create an instance of the class with the new keyword, all constructors in the constructor chain are automatically called. But if an object implements the Cloneable interface, we can call its clone () method. The Clone () method does not call any class constructors.

When using the design pattern, creating a new object instance using the Clone () method is straightforward if you create the object in Factory mode. For example, here is a typical implementation of the factory pattern:
public static Credit Getnewcredit () {
return new credit ();
}
The improved code uses the Clone () method, as follows:
private static Credit Basecredit = new Credits ();
public static Credit Getnewcredit () {
return (Credit) Basecredit.clone ();
}
The idea above is also useful for array processing.
2 Using non-blocking I/O
A lower version of the JDK does not support the nonblocking I/O API. To avoid I/O blocking, some applications use a method of creating a large number of threads (in better cases, a buffer pool is used). This technology can be seen in many applications that must support concurrent I/O flows, such as Web servers, quotes, and auction applications. However, creating a Java thread requires considerable overhead.
JDK 1.4 introduces a non-blocking I/O library (Java.nio). If your application requires an earlier version of the JDK, there is a package that supports non-blocking I/O.
3 Caution with exceptions
Exceptions are bad for performance. Throwing an exception begins with creating a new object. The constructor of the Throwable interface calls the local (Native) method named Fillinstacktrace (), the Fillinstacktrace () method examines the stack, and collects the call trace information. Whenever an exception is thrown, the VM must adjust the call stack because a new object is created during processing.
Exceptions can only be used for error handling and should not be used for control procedures.
4 Do not repeat initialization of variables
By default, when invoking a class's constructor, Java initializes the variable to a certain value: All objects are set to NULL, integer variables (byte, short, int, long) are set to 0,float and double to pomelo trade? 0, the logical value is set to False. This is especially important when a class is derived from another class, because all constructors in the constructor chain are called automatically when an object is created with the new keyword.
5 Specify the final modifier of the class as much as possible
A class with a final modifier is not derived. In the Java Core API, there are many examples of final applications, such as java.lang.String. Specifying final for the string class prevents people from overwriting the length () method.
In addition, if you specify a class to be final, all methods of that class are final. The Java compiler looks for the opportunity Inline (inline) for all final methods (this is related to the specific compiler implementation). This will increase the performance by an average of 50%.
6 use local variables as much as possible
The parameters passed when the method is called and the temporary variables created in the call are saved in the stack, which is faster. Other variables, such as static variables, instance variables, and so on, are created in the heap and are slower. Also, depending on the compiler/JVM, local variables may be further optimized. See Use stack variables whenever possible.
7 Multiplication and division
Consider the following code:
for (val = 0; Val < 100000; Val +=5) {Alterx = val * 8; myresult = val * 2;}
Replacing multiplication operations with shift operations can greatly improve performance. The following is the modified code:
for (val = 0; Val < 100000; val + = 5) {Alterx = Val << 3; myresult = Val << 1;}

8. Do not arbitrarily use the STINGA=STRINGB+STRINGC, there are a large number of splicing operations in place with StringBuilder instead.

Finally: Impact of performance differences
Android phone customization is too high, the price from 600 to 5000 blocks, so there must be a difference in performance, both in terms of GPS or memory.
Different resolution of the adaptation
There are different problems in the interactive design of large data volume, so to make the network difference optimization, it is necessary to be able to use the lower price of the Android phone.
Targeted Abandon animation interaction
It's easy to do interactive design on the iOS platform, but the crash is considered on Android, so on Android we give up some animation interactions.
Different processing of data interactions
Each piece of data is big and small, and if it runs for a long time, the data is a little bit bigger. When interacting on Android, the reaction may be slow and may crash at any time, so we will process the segmented data and read the detailed data when we click on each piece of data.
Optimization of network differences
Network differentiation is mainly for offline and 2g/3g network, network differences will affect the user experience, users in the use of 2G networks, reduce some of the number of interactions, in the speed of processing more ease, in the operating rate and efficiency is guaranteed, all with user experience as the core.

The problems facing
Android version:
GPS hardware differences, Android phone models are numerous, resulting in uneven GPS hardware, positioning speed, accuracy has a large deviation;
Functional differences resulting in operational fluency problems, insufficient memory to cause crashes;
Android system diversification, software operation compatibility is not strong;
The resolution of the non-uniform, resulting in increased workload;
iOS version:
New features brought by system version upgrades;
The impact of the model replacement, resolution, size, performance, etc.;

Http://www.linuxidc.com/Linux/2013-07/87359.htm

Big summary of Android development performance optimization

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.