What is traceview?
Traceview is a good performance analysis tool on the Android platform. It allows us to understand the performance of the program we want to track in a graphical way, and can be specific to the method.
Role of traceview
1. view the execution time of the tracing code and analyze the time-consuming operations.
2. It can be used to trace method calls, especially the method call relationship at the android framework layer.
How to Use traceview
You can perform the following three steps:
1. Select the tracing range to add the record code
2. Use the tool trace view in tools to open the. trace file.
3. Analyze the trace file
1. Select the tracing range to add the record code
First, you must add code to the program to generate a trace file. With this trace file, you can convert it into a graph.
The code to be added is as follows:
1 |
Debug.startMethodTracing(); // Start |
2 |
Debug.stopMethodTracing(); // End |
The wirelessqa parameter is the name of the trace file to be created, wirelessqa. Trace. The default path is/sdcard/wirelessqa. Trace. You can also Customize/data/log/wirelessqa to indicate that the file is stored in/data/log/wirelessqa. Trace.
Instance code reference:
Publicclass mainactivity extends activity {02 03 @ override04 protectedvoid oncreate (bundle savedinstancestate) {05 super. oncreate (savedinstancestate); 06 setcontentview (R. layout. activity_main); 07 settitle (this. getclass (). getname (); 08 view tologinview = findviewbyid (R. id. to_login); 09 // start to record sdcard/wirelessqa. trace file 10 debug. startmethodtracing ("wirelessqa"); 11 tologinview. setonclicklistener (new view. onclicklistener () {12 13 publicvoid onclick (view) {14 intent = new intent (getapplicationcontext (), loginactivity. class); 15 startactivity (intent); 16} 17}); 18} 19 20 @ override21 protectedvoid onstop () {22 super. onstop (); 23 debug. stopmethodtracing (); // end record wirelessqa. trace24} 25}
Note:
In the development documentation, you can add debug in oncreate () of the activity. startmethodtracing (), and add debug in ondestroy. stopmethodtracing (), but we found that this method is not useful in actual tests, because the ondestroy () of our activity is usually determined by the system when to call, therefore, the trace file may not be obtained for a long time.
Therefore, we decided to call Debug. stopmethodtracing () in onstop (). In this way, when we switch to another activity or click the Home Key, onstop () will be called, and we can get the complete trace file.
Do not forget to add the permission to access the SD card.
1 |
<uses-permissionandroid:name= "android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> |
2 |
<uses-permissionandroid:name= "android.permission.WRITE_EXTERNAL_STORAGE" /> |
2. Use the tool trace view in tools to open the. trace file.
3. Analyze the trace file