Application analysis of strictmode, a sharp tool for Android performance tuning

As an Android Developer, I have to deal with performance problems more or less in my daily development work. For example, my android program runs slowly and jams, and there are often anr dialog boxes and so on. Since there is a performance problem, performance optimization is needed. As the saying goes, if you want to do well, you must sharpen your tools first. A good tool can help us find and locate problems, and then solve them with a clear aim. This paper mainly introduces the application and some problems of strictmode in Android application development.

What is strictmode

Strict mode means strict mode. It is a developer tool used to detect violations in programs. The most common scenario is to detect time-consuming operations such as local disk and network reading and writing in the main thread.

Where is Yan

Since it is called strict mode, where is it strict?

In Android, the main thread, that is, the UI thread, in addition to handling UI related operations, can also perform file reading or database reading and writing operations (since Android 4.0, network operations are prohibited from being executed in the main thread, otherwise networkonmainthreadexception will be thrown). Using strict mode, the system will respond to the violation of the main thread, such as log printing, pop-up dialog box or crash. In other words, strict mode will expose the details of application violations to developers for optimization and improvement.

What can be detected

Strict mode mainly detects two major problems, one is thread policy, that is, treadpolicy, and the other is VM policy, that is, vmpolicy.

ThreadPolicy

Thread policy detection includes

VmPolicy

The contents of virtual machine policy detection are

Activity disclosure uses detectactivityleaks() to open unclosed closeable objects. Disclosure uses detectleakedcloseableobjects() to open leaked SQLite objects() to open detectleakedsqlliteobjects() to detect the number of instances and setclassinstancelimit()

working principle

In fact, the implementation principle of strictmode is also relatively simple. Taking IO operation as an example, it is mainly monitored during open, read, write and close. The libcore.io.blockguardos file is the monitoring place. Take open as an example to monitor as follows.

The implementation code of onreadfromdisk () method is located in strictmode.java.

How to use

The most important thing about how to use strictmode is how to enable strict mode.

Where is it?

The strict mode can be enabled in the oncreate method of application or activity and other components. In order to better analyze the problems in the application, it is recommended to put it in the oncreate method of the application.

Simple enable

The following code enables all threadpolicy and vmpolicy violation detection

The strict mode needs to be enabled in the debug mode, not in the release version.

At the same time, strict mode has been introduced since API 9, and some API methods have also been introduced from API 11. You should pay attention to the API level when using.

If necessary, you can also turn on some strict mode.

View results

There are many forms of reporting violations in strict mode, but to analyze specific violations, you still need to check the log. Filter strictmode under the terminal to get the specific stacktrace information of violations.

Resolution of violations

for instance

Take the file writing in the main thread as an example, the code that causes the violation warning

The warning caused is

Because the above is an IO violation in the main thread, the solution is to put the write operation into the worker thread.

However, this is not perfect, because the outputstream.write method may throw IOException, resulting in the situation that the OutputStream object is not closed. Improvement is still needed to avoid the violation that the closable object is not closed. Improvements are as follows

Detect memory leaks

Usually, to detect memory leaks, we need to use mat to analyze the heap dump file. This operation is not difficult, but it is not easy. Using strict mode, memory leaks can be found only by filtering logs.

Take activity as an example. First, we need to start violation detection for detecting activity leakage. Use detectall or detectactivityleaks() above. Secondly, write a piece of code that can produce activity disclosure.

Partial implementation of sleakyactivities in myapplication

When we repeatedly enter leakyactivity and then exit, filtering strictmode will get such logs

According to the analysis log, there should be only one instance of leakyactivity, but now there are two, indicating that leakyactivity has a memory leak.

Strict mode can not only detect the memory leakage of activity, but also custom detect the instance leakage of class. Starting from API 11, this method provided by the system can meet our requirements.

For example, only one is allowed in a browser Search@R_ 404_ 2419 @ instance, we can set the detected Search@R_ 404_ 2419 @ instance leakage

noteSlowCall

Starting from API 11, strictmode allows developers to customize some time-consuming call violations. This customization is applicable to custom task execution classes. For example, we have a class for task processing, taskexecutor.

First, you need to track the time consumption of each task. If it is greater than 500 milliseconds, you need to prompt the developer. Noteslowcall can realize this function. Modify the code as follows

Perform a task that takes 2000 milliseconds

Note that ~ duration = 20 ms is not the execution time of the time-consuming task, but our custom information MSG = slowcall cost = 2000 contains the real time-consuming.

Other skills

In addition to viewing the log, we can also turn on the strict mode in the developer option. After it is turned on, if there is an operation that takes a long time in the main thread, the screen will flash. This is a more direct method.

Here comes the problem

Is the log time reliable

In the following filter log, we see that the following IO operation takes 31 milliseconds. Is this true

From the above stacktrace, we can see that the testreadcontentofile method contains file read IO operations. As for whether it is 31 milliseconds, we can calculate it by using the principle of stopwatch, that is, the following records are recorded at the place where the method is called

The above operations in the obtained log took 9 ms, not 31 Ms.

Note: generally, the time consumption given by strictmode is higher than the actual situation, and it is not the real time-consuming data.

be careful

summary

The above is the application analysis of strictmode, a sharp tool for Android performance tuning introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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
分享
二维码
< <上一篇
下一篇>>