Analyzing Xamarin app performances using Android Device Monitor

Application performances are important. When application doesn't perform fast enough or crashes often, users are unhappy and application is doomed to fail. This is especially true in mobile world, where everything should be fast and responsive.

If you are developing cross-platform application using Xamarin and you run into performance issues, you may be in a bigger trouble than initially expected. The reason is multiple levels of abstractions, different memory heaps on different levels, unmanaged code and memory… You need a good profiler.

First you may want to try Xamarin Profiler. It theory it has essential profiling features on Xamarin level. In practice it is not stable enough for real-world applications. Latest preview release has stability improvements, so you may get some value from it.

When it comes to Android, you should try using great tool called Android Device Monitor. Although it represents Android level information and you can't see Xamarin specific objects, analyzing trace or memory dump can still be very helpful.

Go to your Android folder and run Device Monitor.

On the left side of Device Monitor window you will see list of connected devices and their processes. Start your application and it's process should be visible in this list.

On top bar you can see available options: tracking memory heap, causing GC, tracking threads or making UI screenshots. Among them is Start Method Profiling.

Profiling Manually

  1. Select your process.
  2. Click Start Method Profiling
  3. Choose whether you want Sample based profiling or Trace based profiling
  4. Do the action in your application
  5. Click Stop Method Profiling

Trace file is generated, containing the trace information you want to analyze.

  • A timeline panel – describes when each thread and method started and stopped. We can zoom to specific time span and check what each thread did at that moment.
  • A profile panel – provides a summary of what happened inside a method. We can see which one took the most of CPU time or how many calls it had.

Methods are on Android level, so it may require some guessing which C# code corresponds to it, but it's still worth trying.

Profiling From Code

As Opposite from starting profiling manually, you can do it from code. Android.Debug class provides two methods: startMethodTracing() and stopMethodTracing(), to start and stop logging of trace information to disk. This option is very precise because you can specify exactly where to start and stop logging trace data in your code.

Luckily Xamarin.Android provides wrappers for these methods. Let's write our wrapper which is going to give specific name to trace files, which includes date and time.

[crayon lang=”csharp”]
public void StartMethodTracing(string traceName = “”)
var currentTime = DateTime.UtcNow.ToString(“yyyy-dd-M–HH-mm-ss”);
traceName = string.Format(“{0}-{1}”, traceName, currentTime);

public void StopMethodTracing()

Now let's choose applications action we want to analyze. In this example I am going to analyze 5 navigations between Home and PhoneBook pages.

Go back to Device Monitor and open File Explorer tab.

Each page navigation produce separate file. Select all files and click "Pull a file from device". Save them in desired folder on your machine. I like to put them in session folders as I want to compare them later with other sessions.

Go back to Android Device Monitor and click Open File. Open all files from folder:

Observe data:

From this trace file we can see that some Task.Run takes 4.5 seconds to execute. That signals something really heavy was happening at that time, but it doesn't happen every time. In this case it was Garbage Collection that blocking this Task from executing until it finishes.

Make changes to your code and repeat process. Analyze trace again until you find what was preventing your application to perform fast and responsive.


This Post Has 2 Comments

  1. Thanks for the article, but where do I find the tracefile? I start and stop profiling as described in Profiling Manually, but after stop nothing happens. How could I open the trace file?

    Many thanks!

    1. mm

      Trace file should be immediately loaded in central area of Android Device Monitor. It should be located at C:\Users\your_user\AddData\Local\Temp\ddms1234567890.trace. I am guessing that Method Profiling is failing for some reason on the device. Try using other emulator or device. 

Leave a Reply