Analysis of Android traceview efficiency tool and two sdcard creation methods

Source: Internet
Author: User

From http://android.group.iteye.com/group/wiki/3041-himi-xiaominghimi-traceview-sdcard

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, we need to use a good viewing tool-android traceview provided in the android SDK. The following describes how to use this tool: Then we will explain the implementation steps andPlease pay special attention to this!

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)

UseMksdcard-l MyCard 1024 m f: \ mysdcard. imgCreates a 1 GB sdcard;

UseEmulator-AVD my_android-sdcard F: \ mysdcard. imgActivate sdcard!

Finally, add the following content in eclipse preferences --> Android --> launch.-Sdcard F: \ mysdcard. IMG (this step is to add sdcard support in 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!

Java code

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Package = "com. himi"
  4. Android: versioncode = "1"
  5. Android: versionname = "1.0" type = "codeph" text = "/codeph">
  6. <Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">
  7. <Activity Android: Name = ". mainactivity"
  8. Android: Label = "@ string/app_name">
  9. <Intent-filter>
  10. <Action Android: Name = "android. Intent. Action. Main"/>
  11. <Category Android: Name = "android. Intent. Category. launcher"/>
  12. </Intent-filter>
  13. </Activity>
  14. </Application>
  15. <Uses-Permission Android: Name = "android. Permission. write_external_storage"> </uses-Permission>
  16. <Uses-SDK Android: minsdkversion = "4"/>
  17. </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 Tracing file through cmd.Traceview c: \ NameThe path of the Tracing file is on drive C. If it is on another disk other than drive C, I cannot open traceview normally. 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:

Java code

  1. /**
  2. * @ Author himi
  3. * @ Param canvas
  4. */
  5. Public void hot (canvas ){
  6. For (INT I = 1; I <100; I ++ ){
  7. Bitmap BMP = bitmapfactory. decoderesource (getresources (),
  8. R. drawable. Icon );
  9. Canvas. drawbitmap (BMP, I + = 2, I + = 2, paint );
  10. }
  11. }

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!

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.