Android Learning Series (29)-several command practices for app debugging

Source: Internet
Author: User
Tags high cpu usage

In Android application development, we will use variousCodeDebugging: in fact, after Android development, we may encounter some random problems, such as high CPU utilization and Memory leakage. We cannot perform Code debugging simply, 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.

Dumpsys [Options] meminfo Display memory information cpuinfo Display CPU information account display accounts information activity display all activities information window display keyboard, window and Their Relationship WiFi display WiFi Information

For exampleProgramMemory information:

 
# 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:

 
@ 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:

#! /System/bin/sh # This step is rough, it means file =/sdcard/CPU/cpu_info.logrm $ fileuntil [1-GT 10000] doecho-e "\ n ---------------" >>> $ filedate >>$ filetop-M 5-N 1 >>$ filesleep 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:

 
#! /System/bin/sh # This step is rough, it means file =/sdcard/CPU/mem_info.logrm $ fileuntil [1-GT 10000] doecho-e "\ n ---------------" >>> $ filedate >>$ filedumpsys meminfo COM. tianxia. test> $ filesleep 3 done

The same is true.

9. conclusion
it is a bit of tips that you have never had time to organize and commonly used or not frequently used. I feel that there are few shares on the Internet, sometimes I want to talk to my friends about these debugging methods, especially long Ge, but I want to share them with you today. I have to make an ugly speech. Maybe it is amazing to solve some intractable diseases, writing a few times is also a Learning Record.

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.