CPU Usage profiling

Hi Alex,

you can do profiling using the method tracing functionality in ART.

You can add the following code snippet to the beginning of your app, and it will create a sampling based trace of the first 10 seconds of your app’s runtime. Obviously, you can put the start and stop calls where it makes the most sense for you.

        List<NSURL> l = (List<NSURL>) NSFileManager.defaultManager().
                URLsForDirectoryInDomains(
                        NSSearchPathDirectory.DocumentDirectory,
                        NSSearchPathDomainMask.UserDomainMask);
        NSURL docDirURL = l.get(0); // Error handling is for cowards :)
        String fsPath = docDirURL.fileSystemRepresentation();
        System.out.println("Trace log path: " + fsPath);
        Debug.startMethodTracingSampling(fsPath + "/MOE-sampling.trace", 0, 100);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                Debug.stopMethodTracing();
            }
        }, 10000);

The resulting trace file can be opened from the Android Device Monitor (included in Android Studio): https://developer.android.com/studio/profile/traceview.html

Best Regards,
Gergely

1 Like