1.TraceView
1) Function: For hot spot analysis and performance optimization, analyze CPU time, number of calls, function call relationship, etc.
2) Method:
A) Add the tracking switch in the program code
Import Android.os.Debug;
......
Android.os.Debug.startMethodTracing ("/data/tmp/test");//Build/data/tmp directory First
...//The program segment being traced
Android.os.Debug.stopMethodTracing ();
b) compile, run, generate/data/tmp/test.trace file on device side
c) Copy the trace file to the PC side
$ adb pull/data/tmp/test.trace./
d) Analyze trace files using the Android self-bring tool
$ANDROID _src/out/host/linux-x86/bin/traceview Test.trace
At this point, you can see the number of times each function is called CPU utilization and other information
e) Analyze the build Call relationship class diagram using the Android Self-tool
$ apt-get Install graphviz# installation picture related software
$ANDROID _src/out/host/linux-x86/bin/dmtracedump-g test.png Test.trace
The class diagram is generated in this directory Test.png
3) Note
Trace file generation conflicts with the debug version of the LIBDVM module, so this method is only suitable for debugging non-debug version emulators, otherwise it will be error when parsing trace files
2.HProf (Heap profile)
1) Function:
Memory analysis for Java-level display of detailed memory footprint information indicating suspect memory leak objects
2) Method:
A) Add a dump action to the code
Import Android.os.Debug;
Import java.io.IOException;
......
try {
Android.os.Debug.dumpHprofData ("/data/tmp/input.hprof"); Build/data/tmp Directory First
} catch (IOException IoE) {
}
b) Copy the Hprof file to the PC side
$ adb pull/data/tmp/input.hprof./
c) Use the command Hprof-conv to turn hprof into the standard hprof for mat recognition
$ $ANDROID _src/out/host/linux-x86/bin/hprof-conv input.hprof output.hprof
d) Use the Mat tool to see hprof information
Download Mat tool: http://www.eclipse.org/mat/downloads.php
Open output.hprof with tools
3) Note: This tool can only display Java-level, not the C-tier memory consumption information
3.SamplingProfile (used on Android 2.0 version)
1) function
Samples the currently running function every n milliseconds and outputs it to log
2) Add sampling settings to the code
Import Dalvik.system.SamplingProfiler
......
Samplingprofile sp = Samplingprofiler.getinstance ();
Sp.start (n);//n Sets the number of samples per second
Sp.logsnapshot (Sp.snapshot ());
......
Sp.shutdown ();
It will start a thread monitor and print information in Logcat
4. Take the current stack condition and memory information in the form of a system signal
1) principle
Dalvik virtual machine to sigquit and SIGUSR1 signal Processing (DALVIK/VM/SIGNALCATCHER.C), respectively, to complete the current stack and take the current memory situation function
2) Usage
A) $ chmod 777/data/anr-r# Set the ANR directory permission to writable
$ rm/data/anr/traces.txt# Delete the trace information before
$ ps # Find process number
$ kill-3 Process Number # sends a sigquit signal to the process, at which point the trace information is generated
$ cat/data/anr/traces.txt
Feature implementation: Traverse the Thread list (Dalvik/vm/thread.c:dvmdumpallthreadex ()) and print the current function call relationship (Dalvik/vm/interp/stack.c:dumpframes ())
b) $ chmod 777/data/misc-r
$ ps # Find process number
$ kill-10 Process Number # sends Sigquit signaling to the process, which generates HPROF information at this time
$ ls/data/misc/*.hprof
Now generate the Hprf file, how to use this file, see Part Two (HProf)
Note: hprof files are very large, pay attention to delete immediately after use, so as not to occupy the full memory
5.logcat and Principle
1) Android.util.Log use println standard Java output words, and prefix i/v/d ....
2) Dalvik the use of pipelines and threading, first using DUP2 to redirect stdout and stderr to management (Vm/stdioconverter.c:dvmstdioconverterstartup), Then start a thread to read the content from the other end of the pipeline (DALVIK/VM/STDIOCONVERTER.C:STDIOCONVERTERTHREADST Art ()), using the log utility (System/core/liblog/logd_ WRITE.C: __android_log_print ()) output to/dev/log/*
3) Logcat by adding different parameters to see the different input information under/dev/log/
# logcat-b main displays information from the main buffer
# Logcat-b Radio Displays information in the wireless buffer
# Logcat-b Events Displays information in the event buffer
6.jdwp (Java Debug Wire Protocol) and principle
1) The virtual machine (device side) loaded the agent JDWP at startup, thus having the debugging function. The debugger side (PC side) connects to the device via the JDWP protocol, sending commands to get the state and control the execution of the Java program. JDWP is communicated by command and reply (reply).
2) The Debug tool jdb in the JDK is a debugger, and the DDMS also provides a debugger to connect to the device.
3) Dalvik provides two kinds of connection methods for JDWP: TCP and ADB, TCP can manually specify the port, the ADB is automatically set to 8700 port, usually using DDMS debugging is through the ADB mode
7.monkey
1) Monkey is a command-line tool that comes with Android. It sends a pseudo-random user event stream to the system, enabling stress testing of the application under development.
2) method
Open the setting interface on the device side
$ adb shell
# monkey-p Com.android.settings-v 500
You can see that the interface is constantly being switched
8. Other Gadgets
See the tools provided in Android.os.Debug for details
1) Take the nanosecond-level time to calculate the time
Threadcputimenanos ()
2) Statistics of memory allocation between two points
Startalloccounting ()
Stopalloccounting ()
Getglobalalloccount ()
Get .....
3) Print the current load class
Getloadedclasscount ()
Printloadedclasses () It needs to turn on the Ndebug function to open the log function in System/core/
9. Print Debug Information
$ adb bugreport
Android Debug Toolset (pretty full)