Android notes: practice and analysis of several commands for app debugging

In Android application development, we will use various code debugging; In fact, after the development of Android, we may encounter some random problems, such as too high CPU, memory leakage, etc. we can't simply debug the code. We need a system log, etc. below, I will grasp several common commands and methods encountered in my work and demonstrate them to you.

1. Logcat command this command is the simplest and most commonly used. You can view the help. I won't say more. If you need to print time, add the parameter - V time ADB logcat - V time

2. Bugreport command this command is also very simple, but it is very useful in practical applications. There will be detailed dumpsys, dumpstate and logcat information after startup, which is a complete log record. It is a great reference for analyzing user behavior, abnormal information and system state. Generally, we will export the bug report to the computer for analysis. adb bugreport > xxx.log

I emphasize again that bugreport contains rich system and user information. It is a record of the output results of many other commands, which is very useful.

3. The dumpsys command is often used to view system information

The information inside is very valuable, especially for analyzing memory leakage and memory overflow.

4. The top command is very convenient to view CPU information.

Let's look at the rendering, which lists five processes by CPU size.

The CPU of com.tianxia.test is too high, which will cause the mobile phone to get hot. At the same time, using this information, you can monitor the use of application CPU to adjust and optimize the code.

5. The configuration file local.prop is not available on the Internet at present. I have only used the following in my work:

Add the above text to / data / local.prop. If you don't have this file, create it yourself. Then restart the mobile phone and you can see the detailed SQL statement information of each application querying the database. It is very useful for debugging the database, analyzing and optimizing database SQL exceptions.

6. Analysis of mobile phone perm. Let's practice an example. If the mobile phone is too hot, how to find out the problem? First, we write a program com.tianxia.test, which is an endless loop. The core code is as follows:

At first glance, com.tianxia.test occupies 85% of the CPU. It turns out that this guy is making trouble. The process ID is 644, which is useful later. (2) Analyze why hot application processes need the strace command under Linux, but Android does not integrate this command. Download address of Android version: http://benno.id.au/android/strace After downloading, upload it to the mobile phone: We ADB push strace / system / bin. If we upload it to / system / bin on the simulator, an out of memory error will be reported, We can also upload to the / data directory. If we don't have execution permission, we also need Chmod 777 strace. Strace command has many parameters. Directly executing strace will display the instructions:

The output is as follows:

7. Collect the CPU operation of the mobile phone. Sometimes it is difficult for us to obtain the information we want by using the log. We may need to write some simplest steps and execute them in the mobile phone. For example, record CPU occupied by monitoring CPU_ log.sh:

8. The practice of collecting memory data of an application is similar to the above script, but the command is different. I list it separately because it is sometimes very useful. For example, we need to collect the memory usage of com.tianxia.test and analyze whether it will leak memory. The steps are similar:

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