Android Note: Practice and Analysis of Several App Debugging commands

Source: Internet
Author: User
Tags high cpu usage

In Android application development, we will use various types of code debugging. In fact, after Android development, we may encounter some random problems, such as high cpu and memory leakage, we cannot perform simple code debugging. We need a system log and so on. Below I will explain some common commands and methods I have encountered in my work.

1. logcat command
This command is the simplest and most commonly used. You can view the help information. If you need to print the 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. It will contain detailed dumpsys, dumpstate and logcat information after startup, which is a complete log record. It provides a great reference for analyzing user behaviors, abnormal information, and system status. Generally, we export the bugreport to the computer for analysis.

Adb bugreport> xxx. log

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

3. dumpsys command
The system information can be viewed in many ways.

Copy codeThe Code is as follows: dumpsys [options]
Meminfo Display memory information
CPU info displays CPU Information
Account displays accounts information
Activity displays information about all activities.
Window displays the relationship between the keyboard, window, and them.
Wifi display wifi Information

For example, to view the memory information of a program:Copy codeThe Code is as follows: # view the memory usage of the application com. tianxia. test.
Adb shell dumpsys meminfo com. tianxia. test

As follows:

The information in it is very valuable, especially for analyzing memory leaks and memory overflow.

4. top Command
It is too convenient to view cpu information.

top -m 5 -t

Let's see, five processes are listed by cpu size.

The cpu usage of com. tianxia. test is too high, which causes the phone to burn. At the same time, this information can be used to monitor the use of the application cpu to adjust the optimization code.

5. Configuration File local. prop
Currently, the local. prop configuration is not found on the Internet. I have only used the following in my work:

log.tag.SQLiteStatements=VERBOSE log.tag.SQLiteTime=VERBOSE

Add the preceding text to/data/local. prop. If this file is not available, create it by yourself. Restart your phone to view the detailed SQL statement information of the database queried by each application. It is useful for debugging the database and analyzing and optimizing Database SQL exceptions.

6. Analysis of mobile phone fever
The following is an example of how to find out the problem when the mobile phone is too hot?
First, we write a program com. tianxia. test, which has an endless loop. The core code is as follows:

Copy codeThe Code is as follows: @ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
While (true ){
System. currentTimeMillis ();
}
}

After the application is opened, the system time will be obtained. Because in the main thread, it will certainly lead to the application ANR. It will also waste the system cpu and the mobile phone will heat up, and we will run it.
If we do not know the above Code, let's find this problem:
(1) Find the Hot application.
Use the top command:

top -m 5 -t

Com. tianxia. test occupies 85% of the cpu. It turns out this guy is playing tricks. The process ID is 644, which is useful later.
(2) analyze what the Hot application process is doing
The strace command in linux is required, but android does not integrate this command. for android:
Http://benno.id.au/android/strace
After the download is complete, upload it to your mobile phone:
When we upload the adb push strace/system/bin file to the/system/bin file on the simulator, an out of memory error is returned. You can also upload the file to the/data directory. If you do not have the execution permission, chmod 777 strace.
The strace command has many parameters. If you execute strace directly, the following instructions are displayed:


The-p parameter indicates the process number. In the first step, we find that the process ID of com. tianxia. test is 644. Let's see what the application is doing with such a high cpu usage?

strace -p 644

The output is as follows:


Its system call has always been gettimeofday. It has always output this. Obviously, it must have entered an endless loop, and it is an endless loop for obtaining the time. Then it is combined with logcat and code, locate this section of code (that is, the code we provided earlier) to solve this bug.

7. Collect the cpu running status of the mobile phone.
Sometimes it is difficult for us to obtain the desired information using logs. We may need to write some simple steps and put them in the mobile phone for execution.
For example, monitor the cpu usage record cpu_log.sh:

Copy codeThe Code is as follows :#! /System/bin/sh
# This is a rough step.
File =/sdcard/cpu/cpu_info.log
Rm $ file
Until [1-gt 10000]
Do
Echo-e "\ n -------------" >>$ file
Date> $ file
Top-m 5-n 1 >>$ file
Sleep 3
Done

The cpu information of the mobile phone is written to the cpu_info.log file under the cpu directory of the sdcard every 3 seconds for further analysis.
Ps: push to the data directory to grant the executable permission. Run the command in shell.

8. Collect memory data of an application
This practice is similar to the above script, but the command is not the same as I listed it separately, because it is sometimes 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:

Copy codeThe Code is as follows :#! /System/bin/sh
# This is a rough step.
File =/sdcard/cpu/mem_info.log
Rm $ file
Until [1-gt 10000]
Do
Echo-e "\ n -------------" >>$ file
Date> $ file
Dumpsys meminfo com. tianxia. test >>$ file
Sleep 3
Done

The same is true.

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.