Troubleshooting and diagnosis

Profiling is the term used to describe the dynamic system analysis while the target application is running. Profilers generally collect data about metrics such as CPU utilization, framerate values, and most importantly data about memory allocations. Especially with Xamarin projects, since we are dealing with multiple platforms, profiling becomes an important part of testing and diagnostics.

There are numerous tools that one can use to profile memory usage on Xamarin projects, Xamarin Profiler being the only one that can be used both for Xamarin.iOS and Xamarin.Android applications.

Xamarin Profiler

Xamarin Profiler is the newest addition to the Xamarin Suite. This profiler has the advantage over other platform-specific applications since it can be run either on OS X or Windows targeting Xamarin.Android or Xamarin.iOS applications.

Figure 2: Xamarin Profiler

It was designed to give developers almost real time (depending on the sampling rate) information about the memory heaps for Xamarin applications. It can also save memory allocation snapshots which can later on be accessed and analyzed.

It can be started directly from Visual Studio or Xamarin Studio and can be used with both emulator and real device build/run configurations.

Currently there are two instruments you can select in the initial popup window.

Allocations instrument

The first instrument is the Allocations template which provides detailed information on the memory segments and allocations. In this view, developers can see a generalized list of allocations grouped by the class name under the Summary tab. The Call Tree tab gives a list of threads in the application and how they relate to the memory objects. Allocation list provides live data about the object allocations, and the Snapshots tab gives information about the memory snapshots stored.

Time Profiler

Time Profiler is the second instrument that can be used in Xamarin Profiler. It provides valuable information on how much time the application spent executing a certain method. Developers can see a whole stack trace on each method.

Device Monitor (Android only)

Android Device Monitor is hitherto the main diagnostic tool for Android development. And for Xamarin developers, when Android SDK is installed, device monitor can be accessed directly from a tool box item on Visual Studio and under the tools menu on Xamarin Studio.

On the main page of the device monitor there is a tree-view displaying each device or simulator that can be attached to with the device monitor.

Note

Only a single debugger can be attached to any device at a time, therefore other debuggers have to be detached before using the device monitor.

Once the device is selected, developers can get allocation information and the heap state using the graphical interface. It is also possible to trigger garbage collection cycles using the device monitor.

Figure 3: Android Device Monitor attached to Visual Studio Emulator

Instruments (iOS only)

Instruments is a valuable application that is installed together with the Xcode toolset. In this application developers are provided with a big set of diagnostic tools varying from energy consumption, graphic resources, to memory allocations.

The allocations instrument has a very similar interface to Xamarin Profiler, and gives almost real-time data about memory objects.

Figure 4: Instruments Profiling Xamarin Application

The Xcode Instruments tool can be used both together with an actual device or the iOS simulator. It can be started directly from Xamarin Studio. Once the application is started on the iOS simulator or on the actual device, it becomes available in the target selection window.

Figure 5: Instruments with iOS Simulator set as target

Note

If you are developing Xamarin.iOS applications on Microsoft Windows with an OS X build machine, you will not be able to access the Instruments directly from the development station. Once the application is either on the test device or the simulator, you can start the instruments on the build machine and choose the correct target to analyze.

Monotouch Profiler (iOS only)

Monotouch Profiler was the Xamarin tool used to diagnose memory issues with Xamarin.iOS applications before it was superseded by Xamarin Profiler. It can still be accessed using the Run with Mono HeapShot menu item under the Project menu in Xamarin Studio. While providing useful information about memory allocations and the heap, it currently does not go further than being a lightweight application to take memory snapshots.