Analysis of Android Development Optimization: optimization skills from the perspective of code

Usually, we write programs under the pressure of project planning. At this time, the completed code can complete the specific business logic, but the performance is not necessarily optimized. Generally speaking, excellent programmers will constantly refactor the code after writing the code. Refactoring has many advantages, one of which is to optimize the code and improve the performance of the software. Let's learn about code optimization in Android development from several aspects.

1) Memory leak caused by static variables

In the process of code optimization, we need to pay special attention to the static variables in the code. Static variables are class related variables. Its life cycle is from the time the class is declared to the time when the class is completely recycled by the garbage collector. Therefore, in general, static variables occupy memory space from the beginning of the class where they are used until the program exits. If you don't pay attention, static variables refer to resources that occupy a lot of memory, which makes the garbage collector unable to recycle memory, which may cause a waste of memory.

Let's first look at a piece of code that defines an activity.

If the current activity is recreated (for example, if you switch between vertical and horizontal screens, the whole activity will be recreated by default). Since resources refer to the activity created for the first time, the activity created for the first time cannot be recycled by the garbage collector, so that all objects in the activity created for the first time cannot be recycled. At this time, part of the memory is wasted.

Experience sharing:

In actual projects, we often add some object references to the collection. If the collection is static, we need to pay special attention. When you don't need an object, be sure to clean up its references from the collection in time. Or you can provide an update strategy for the collection to update the whole collection in time, so as to ensure that the size of the collection does not exceed a certain value and avoid the waste of memory space.

2) Use context of application

In Android, the life cycle of application context is as long as that of an application, rather than depending on the life cycle of an activity. If you want to maintain a long-lived object and the object needs a context, you can use the application object. You can get the application object by calling the context. Getapplicationcontext () method or the activity. Getapplication () method.

Still take the above code as an example. You can modify the code to look like this.

3) Close resources in time

Cursor is a class that manages data sets after Android queries data. Normally, if we don't shut it down, the system will shut it down when recycling it, but this efficiency is particularly low. If the amount of data obtained from the query is small, it is OK. If the amount of data in cursor is very large, especially if there is blob information, memory problems may occur. So be sure to close cursor in time.

A general code fragment using cursor is given below.

Similarly, when using files, close them in time.

4) Use bitmap to call recycle() in time

As mentioned in the previous chapter, when you do not use a bitmap object, you need to call recycle () to free memory and set it to null. Although calling recycle () does not guarantee the immediate release of occupied memory, it can accelerate the release of bitmap memory.

In the process of code optimization, if a Activity is found to use a Bitmap object without explicitly calling recycle () to release the memory, it is necessary to analyze the code logic, increase the relevant code, and release the memory by calling recycle () after the Bitmap is no longer used.

5) Optimize the adapter

Next, take the baseadapter of listview as an example to illustrate how to optimize the adapter.

The following methods are provided in the baseadapter class:

To provide the listview with the required view object.

The following is a complete code example of the getview () method.

If the listview has only a few entries, this technique will not bring much performance improvement. However, if there are hundreds or even thousands of items, only a few convertviews and viewholders will be created using this technique (depending on the number of items that can be displayed in the current interface), and the performance will be very different.

6) Code "micro optimization"

Today's era has entered the "micro era". The "micro optimization" here refers to the detailed optimization at the code level, that is, it does not change the overall structure of the code and the original logic of the program. Although Android uses Dalvik virtual machine, traditional java code optimization techniques are also applicable in Android development.

Some are briefly listed below. Because most Java developers can understand it, they will no longer do specific code description.

Creating new objects requires additional memory space. Try to minimize the creation of new objects.

Change the visibility of classes, variables, methods, and so on to a minimum.

For string splicing, use StringBuffer instead of string.

Do not declare temporary variables in a loop, and do not catch exceptions in a loop.

If there is no requirement for thread safety, try to use thread unsafe collection objects.

With a collection object, if its size is known in advance, you can set the initial size in the construction method.

The file reading operation needs to use the cache class to close the file in time.

Use exceptions with caution. Using exceptions will lead to performance degradation.

If your program creates threads frequently, you can consider using thread pools.

Experience sharing:

There are many things to say about code micro optimization, ranging from the declaration of a variable to an algorithm. Especially in the process of code review, you may repeatedly review whether the code can be optimized. However, I think that the micro optimization of code is very time-consuming, and there is no need to optimize all the code from beginning to end. Developers should optimize a part of the code according to the specific business logic. For example, some methods in the application may be called repeatedly, so this part of the code is worthy of special optimization. For other codes, developers need to pay attention in the process of writing code.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>