Android notes: The Practice and analysis of several commands for app debugging _android

Source: Internet
Author: User
Tags memory usage sleep system log cpu usage

In the 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 and so on, we can not easily debug the code, we need a system log, etc., Below I grasp the work encountered several commonly used commands and methods for everyone to demonstrate practice.

1.logcat command
This command is most commonly used, can view help, I do not say more, if you need to print time, add parameter-V times

ADB logcat-v Time

2.bugreport command
This command is also very simple, but very useful in practical applications, there will be from the power-on after the detailed dumpsys,dumpstate and logcat information, is a complete log record. The analysis of user behavior, abnormal information, System State has a great reference role. In general we will export Bugreport to the computer for analysis.

ADB bugreport > Xxx.log

Again, I emphasize 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, the use of a relatively many.

Copy Code code as follows:

Dumpsys [Options]
Meminfo Display Memory Information
Cpuinfo Display CPU Information
Account Display accounts information
The activity shows all the activities information
window displays the keyboard, the windows, and their relationships
WiFi Display WiFi information

For example, to view a program's memory information:
Copy Code code as follows:

#查看应用com. Tianxia.test Memory Usage
adb shell Dumpsys meminfo com.tianxia.test

The effect chart is as follows:

The information inside is very valuable, especially for the analysis of memory leaks, memory overflow has a great effect.

4.top command
This view of CPU information is too convenient.

Top-m 5-t

Let's take a look at the effect chart, which lists 5 processes by CPU size.

Com.tianxia.test the CPU is too high, will cause the cell phone hair hot. This information can also be used to monitor application CPU usage to adjust optimized code.

5. configuration file Local.prop
At present, the Internet did not find the configuration of the use of Local.prop, the work I have only used the following:

Log.tag.sqlitestatements=verbose Log.tag.sqlitetime=verbose

Add the above text to/data/local.prop if the file is not created by itself. Then restart the phone, you can see each application detailed query database SQL statement information, for debugging the database, analysis and optimization of database SQL exception is very useful.

6. Analysis of mobile phone hair hot
Here we practice an example, the 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:

Copy Code code as follows:

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
while (true) {
System.currenttimemillis ();
}
}

This application will always get the system time, because in the main thread, will certainly lead to the application of ANR, also would have been wasting system CPU, cell phone fever, we run it.
Assuming we don't know the code above, we'll find the problem:
(1). Find the application of hot hair.
Using the top command:

Top-m 5-t

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 use later.
(2). Analysis of the application process of hair hot what are you doing?
Need to use Linux under the Strace command, but Android is not integrated with this command, the Android version of the download address:
Http://benno.id.au/android/strace
When the download is complete, upload it to the phone:
We adb push Strace/system/bin, on the simulator is uploaded to/system/bin will report out of the memory error, we can upload to the/data directory, if not execute permissions, also need to chmod 777.
Strace command has many parameters, direct execution strace will display the instructions for use:


Where the-p parameter input is the process number, the first step we find Com.tianxia.test process ID is 644, we look at this application to occupy such a high CPU what?

Strace-p 644

The output is as follows:


Its system call has been gettimeofday, always output this, obviously where must go into the dead loop, but also to get the time of the dead loop, and then combined with Logcat and code, locate this code (that is, we gave the code before) to solve the bug.

7. Collect the mobile phone CPU operation.
Sometimes it's hard to get the information we want with the log, we might need to write some of the easiest steps to put on the phone.
For example, monitor CPU-occupied records cpu_log.sh:

Copy Code code as follows:

#!/system/bin/sh
#这个脚步比较粗糙, that's what it means.
File=/sdcard/cpu/cpu_info.log
RM $file
Until [1-GT 10000]
Todo
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 under the Cpu_info.log file, facilitate our follow-up analysis.
PS: The use of the method is push to the data directory, give executable permissions, under the Shell to execute.

8. Collect memory data for an application
This practice is similar to the script above, except that the commands are different. I'm separate, because this is sometimes useful.
For example, we want to collect com.tianxia.test memory usage, analyze whether it will leak memory, footsteps similar:

Copy Code code as follows:

#!/system/bin/sh
#这个脚步比较粗糙, that's what it means.
File=/sdcard/cpu/mem_info.log
RM $file
Until [1-GT 10000]
Todo
Echo-e "\n\n\n\n\n---------------" >> $file
Date >> $file
Dumpsys meminfo com.tianxia.test >> $file
Sleep 3
Done

The same method is used.

Related Article

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.