In Android application development, we will use a variety of code debugging, in fact, after the development of Android, we may encounter some random problems, such as high CPU, memory leaks, etc., we can not easily debug code, we need a system log and so on, Below I grasp a few common commands and methods encountered in the work to show you practice.
1.logcat command
This command is easiest to use and can be viewed with help, I don't say much, if you need to print time, add parameter-V
2.bugreport command
This command is also very simple, but is very useful in practical applications, there will be a detailed dumpsys,dumpstate and logcat information from the boot, is a complete log record. The analysis of user behavior, abnormal information, System State has a great reference role. In general we will export the bugreport to the computer for analysis.
1 |
adb bugreport > xxx. log |
I emphasize again that bugreport contains rich system and user information, which is a record of the results of many other command outputs and is very useful.
3.dumpsys command
This view system information, use or more.
1234567 |
dumpsys [options]
meminfo 显示内存信息
cpuinfo 显示CPU信息
account 显示accounts信息
activity 显示所有的activities的信息
window 显示键盘,窗口和它们的关系
wifi 显示wifi信息
|
For example, to view a program's memory information:
12 |
#查看应用com.tianxia.test的内存使用情况 adb shell dumpsys meminfo com.tianxia.test |
As follows:
The information inside is valuable, especially when it comes to analyzing memory leaks, which can have a significant impact on memory overflow.
4.top command
This is too convenient to view the CPU information.
Let's take a look at the list of 5 processes that are listed by CPU size.
Com.tianxia.test CPU is too high, will cause the mobile phone hot. This information can also be used to monitor the use of the application CPU to adjust the optimization code.
5. configuration file Local.prop
Currently not found on the network Local.prop configuration use, work I only used the following:
1 |
log .tag.SQLiteStatements=VERBOSE log .tag.SQLiteTime=VERBOSE |
Add the above text to the/data/local.prop, if it is not created by the file itself. Then restart the phone, you can see each application detailed query database SQL statement information, for debugging the database, analysis and Optimization database SQL exception is very useful.
6. Analysis of mobile phone hair perm
Let's practice an example, the mobile phone is too hot, how to find the problem?
First we write a program com.tianxia.test, the Dead loop, the core code is as follows:
12345678 |
@Override public void oncreate (Bundle savedinstancestate) {      super      setcontentview (r.layout.main);      while ( true               } } |
This app will always get the system time when it is opened, because in the main thread, it will definitely lead to the application of ANR, and it'll always waste the system CPU, the phone is hot, we run it.
Assuming we don't know the code above, we'll find the problem:
(1). Find a perm application.
Use the top command:
A look is com.tianxia.test occupies 85% of the CPU, the original is this guy in mischief. The process ID is 644, which we'll use later.
(2). Analysis of the application process of hair perm What's up?
Need to use the Strace command under Linux, but Android is not integrated with this command, Android version:
Http://benno.id.au/android/strace
When the download is complete, upload it to your phone:
Our adb push Strace/system/bin, on the simulator is uploaded to the/system/bin will report out of the memory error, we can also upload to/data directory, if not execute permissions, also need chmod 777 strace.
Strace command has many parameters, the direct execution of Strace will display the instructions for use:
Where the-p parameter is the process number, and in the first step we find the process ID of com.tianxia.test is 644, so let's see what the application is doing with this high CPU.
The output is as follows:
Its system call has been gettimeofday, has been output this, obviously where must go into the dead loop, and is to get the time of the dead loop, and then combined with Logcat and code, positioning the code (that is, the code we gave earlier) to solve the bug.
7. Collect the CPU operation of the mobile phone.
Sometimes it's hard to get the information we want by using a log, and we may need to write some of the simplest steps to put on the phone.
For example, monitor CPU usage record cpu_log.sh:
1234567891011 |
#!/system/bin/sh< /code> #这个脚步比较粗糙, that's what this means file=/sdcard/cpu/cpu_info. log rm $file until [1-GT 10000] do echo-e "\n\n\n\n\n---------------" >> $file date >> $file top-m 5-n 1 >> $file sleep 3 done |
Every 3s will be the phone's CPU information written to the SDcard CPU directory in the Cpu_info.log file, convenient for us to follow up analysis.
PS: The use of the method is to push to the data directory, give executable permissions, execute under the shell.
8. Capturing memory data for an application
This practice is similar to the above script, but the command is not the same as I listed separately, because this is sometimes useful.
For example, we want to collect com.tianxia.test memory usage, analyze whether it will be memory leaks, footsteps similar:
1234567891011 |
#!/system/bin/sh< /code> #这个脚步比较粗糙, that's what this means file=/sdcard/cpu/mem_info. log rm $file until [1-GT 10000] do echo-e "\n\n\n\n\n---------------" >> $file date >> $file dumpsys meminfo com.tianxia.test >> $file sleep 3 done |
The same approach is used.
9. Summary
Bits and pieces have not time to tidy up, there are commonly used also have not commonly used, is some small skill, feel online this aspect of share less, and sometimes talk about these debugging methods with friends, especially Dragon elder brother, just ask me to write today to share with you, had to caught dead, perhaps to solve some incurable diseases have miraculous effect, hehe, Think of what to write, think of a few write a few, but also a learning record.
[Turn] Android Learning Series (--APP) Several command practices for commissioning