Himi original, you are welcome to reprint, please note clearly! Thank you.
Address: http://blog.csdn.net/xiaominghimi/archive/2010/12/29/6105212.aspx
As I am currently working in a professional online game company, I need to use some methods to streamline and optimize the online game code currently in operation, you need to use an excellent viewing tool-Android TraceView provided in the Android sdk. The following describes the implementation steps and notes!
What is TraceView? Let's take a look at Baidu's explanation:
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.
Traceview usage
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:
Java code
// Start tracing to "/sdcard/yourActivityTrace. trace"
Debug. startMethodTracing ("yourActivityTrace ");
//... // Stop tracing Debug. stopMethodTracing ();
// Start tracing to "/sdcard/yourActivityTrace. trace" Debug. startMethodTracing ("yourActivityTrace ");
//... // Stop tracing Debug. stopMethodTracing ();
Google Dev Guide says 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.
Before running the program, make sure that our AVD is an AVD with an SD card so that the trace file can be saved to/sdcard. You can perform any operation after the operation, and then click the home Key. Here, we can see a trace file under the/sdcard/directory through DDMS file explore. Now we copy this file to the specified directory on the computer, assuming it is under the C:/tracefile directory.
You can run traceview through the command line. after entering the tools directory, execute
Traceview C:/tracefile/yourActivityTrace. trace
Then you can see the graph. The next step is to analyze the graph according to the explanation in the Google Dev Guide.
The following describes how to implement and what you need to pay attention:
The implementation steps are divided into three steps: 1. You must first create an sdCard in our simulator; 2. embed our debugging code into the project; 3. Use TraceView to observe and analyze the Code;
1. Write two methods for creating the sdCard of the simulator:
First: When you create an avd in eclipse, you can select an Sd Card option under the api. The first option is the size of the created sdcard.
Type 2: cmd command! Open cmd and cd to the android sdk tool Path. (Alternatively, configure the sdk tool Path in the environment variable Path and re-open cmd)
Create a 1 GB sdcard using mksdcard-l mycard 1024 m f:/mysdcard. img;
Use emulator-avd my_android-sdcard F:/mysdcard. img to activate sdcard!
Finally, add-sdcard F:/mysdcard. img to eclipse Preferences --> Android --> Launch (this step is to add sdcard support to the first creation method)
Note 1:
If the space allocated by sdcard is too small, the program Tracing file is recorded until the sd debit card capacity is slow. Therefore, it is important to generate an appropriate SD memory card for the program before debugging, the longer the program runs, the larger the Tracing file.
Note 2;
(Emulator: ERROR: the user data image is used by another emulator. aborting, disable the simulator, or enter the Directory:/Documents and Settings/user /. android/AVD/* Device */(for example, my directory is C:/Documents ents and Settings/Administrator /. android/avd/android2.0.avd)
Then delete. the folder at the end of lock (I will explain why to delete these files, in fact. lock is a lock. If the program crashes or other causes, it cannot be cleared. this problem occurs in the folder at the end of lock, that is, the avd lock is not released, causing avd manager to think that the avd is in use .))
2. embed our debugging code into the project
As we explained by Baidu, add Debug at the beginning of the program running. startMethodTracing ("yourActivityTrace"); then call Debug in onPause. stopMethodTracing (); why do you want to write the end in onPause () instead of onStop ()? If you look at the api, you will see that the Api introduces onPause () it will be triggered after you return and click the home button, while onStop () is generally triggered by the system. When the program is in the background, and when the memory is tight, it may be called, but it may never be called!
Note: Do not run the project immediately after adding the debugging code to the project. Instead, define a "Write SD card permission" in AndroidMainfest. xml. The code for adding permissions is as follows:
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"> </uses-permission>
Because our debugging code will generate a trace file in the SD card, that is, writing data to the SD card, we need to declare a permission. Note the following!
• 10 • 20 • 30 • 40 ••• • 50 • 60 • 70 • 80 ••••••• • 90 • 100 • 110 • 120 ••••• • 140 • 150
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. himi"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
<Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">
<Activity android: name = ". MainActivity"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
</Application>
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"> </uses-permission>
<Uses-sdk android: minSdkVersion = "4"/>
</Manifest>
3. Run the project and exit the project to get the Tracing file. Use TraceView to analyze the code running status:
When the project runs normally and you click "return" or "home", A. trace file is generated in sdcard. The sdcard directory is under eclipse. CLICK:
Windows-show view-other-android-File explorer
There are two arrows in the upper right corner. The first one is to export files from the simulator sdcard, the second one is to import files from the PC to the sdcard, and the second one is to delete the files .....
Then we run the generated trace file traceview C:/name in cmd and place the path of the trace file on disk C, I cannot open traceview normally on disks other than drive C. I don't know why. Name indicates the generated. trace file. You do not need to enter the suffix ". trace" during cmd. The TraceView analysis window appears;
[Cmd command! Open cmd and cd to the android sdk tools Path. (Alternatively, configure the sdk tool Path in the environment variable Path and re-open cmd )]
Note 1: If a memory overflow problem occurs;
Solution: Find the traceview. bat file under tools under the SDK, right-click the file, and choose edit (or open notepad). Replace the last line with the following:
Call java-Xms128m-Xmx512m-Djava. ext. dirs = % javaextdirs %-jar % jarpath % *
NOTE 2: If the path is incorrect:
For example, if my himi. trace is on drive c, my cmd command is traceview c:/himi and press Enter!
But be careful here, because/h may be considered as an escape character !!! To avoid using h, n, r, t, and so on as much as possible, this problem can be completely avoided, for example, my C drive himi. trace file, which can be written as traceview c: // himi when writing the cmd command ~ Pay attention to the details.
The following is the running TranceView:
The upper-right corner indicates the total time used by the running program. From the traceview screen, we can see various colors. Each color represents different functions and steps. The larger the area of the same color, the longer the running time of this step, or the following statistical table, we can see that apart from the series 0 1 is a system function, 2. 3. function takes a long time, so sequence 4 is a custom function named "hot", which occupies almost the same time as the main draw of the main thread, so there must be a problem. Of course, this method is intentionally written to demonstrate traceview. The code for this hot function is as follows:
• 10 • 20 • 30 • 40 ••• • 50 • 60 • 70 • 80 ••••••• • 90 • 100 • 110 • 120 ••••• • 140 • 150
/**
* @ Author Himi
* @ Param canvas
*/
Public void hot (Canvas canvas ){
For (int I = 1; I <100; I ++ ){
Bitmap bmp = BitmapFactory. decodeResource (getResources (),
R. drawable. icon );
Canvas. drawBitmap (bmp, I + = 2, I + = 2, paint );
}
}
Obviously, I am deliberately consuming memory and time.
In the right-hand statistical field of traceview:
Exclusive: time when the same-level function itself runs
The time when the function itself runs is counted plus the time when the sub-function is called.
Name: Lists All call items. The preceding number is the number. Expand to show that the Parent and Children subitems are called and called.
Incl: percentage of the time taken to calculate the total time.
Excl: percentage of the total execution time.
Cballs + Recur cballs/Total: number of Calls and repeated Calls
Time/Call: the total Time. MS)
Therefore, traceview is a very good program monitoring tool. It can help you find out the function when the program runs slowly, so that our code can be continuously improved and improved!