x-ite's diary

覚え書きです。想定読者は俺

andoird Traceviewでプロファイリング

android 2.1以下
public class MyActivity extends Activity {
    @Override
    public void onCreate() {
        super.onCreate();
        Debug.startMethodTracing("MyProject");  
    }

    @Override void onDestroy() {
        Debug.stopMethodTracing();
        super.onDestroy();
    }

Android2.1以下ではSDCardへの書き込み権限が必要。
AndroidManifest.xmlに下記を追加する。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

本体で実行後、プロファイルを取得

$ adb pull /sdcard/MyProject.trace /path/to/profile

プロファイル開始

 traceview /tmp/MyProject
android 2.2以上

DDMSを利用すると動的にプロファイリングができる。
実機でプロセスを立ち上げてDDMSのDevicesタブにある該当のプロセスを選択する。

Start Method Profilingを実行

実機を操作して操作し終わったら、
Stop Method Profilingを実行する。

少しまつと自動的にEclipseのDebugが立ち上がりプロファイリング結果が見える。

Refference

Profiling with Traceview and dmtracedump
http://developer.android.com/guide/developing/debugging/debugging-tracing.html