cell phone apps erni xamarin

Profiling memory leaks in Xamarin Forms

I remember one time I was working with a mobile project and it was already deployed to the client. It work out well from planning, development, testing to deployment but after a while the client was clamoring that the app is getting slower and slower when it was continuously being used up to the point where it will just crash suddenly. Well it is a given fact maybe not all but I’m guilty of it that we really love our code, so in that sense I was pointing the blame on maybe the device or something unusual that the client was doing with the Application. To make the long story short I’ve learned that Profilers are programmer’s best friend.

by Kirk Patrick Junsay

Introduction

In today’s article I would like to discuss on how to Profile your Xamarin Applications specifically detecting Memory leaks. Later on this article I will show to you the step by step process on how to use the Xamarin Profiler in detecting the memory leaks. For the sample mobile application you can download the application (link below) and can follow along.

 

Hello Garbage Collection

We all know that in the .NET world we have a very good friend called Garbage Collector. Basically it is responsible on freeing up the used memory. As simple as it sounds, whether you are in a Desktop, Web or especially mobile where there are two worlds one is Managed and the other one is the native part which at times Garbage Collection is not enough to reclaim those used memories. Remember the story that I told you in the beginning it may not be a shocker now but the cause of the slow down is due to Memory Leaks.

 

What are Memory Leaks?

Memory leaks in the mobile world is a result of incorrect memory management wherein an object is stored in memory but cannot be accessed by the running code. Here are some scenarios but not limited to wherein memory leaks are most likely to happen:

  • Using of Events, delegates or Messaging Centers
  • Using of Images (refer to the famous bitmap example)
  • Creation of custom controls
  • Static Objects or Classes
  • Use of threads

Memory leaks can be very hard to track down using only your instincts or experience so it is a good practice every now an then to perform profiling in your mobile application. In this way as early as development stage you can already detect as well as apply fixes for Memory leak problems.

 

Profilers to the rescue

Profiling is an important and always taken for granted step in development (I’m guilty of it). Based on Xamarin, profiling is under dynamic program analysis wherein simple terms it analyzes your application while it is running or being used.

Profilers are the tools that performs data mining in your application about the usage of particular method, execution time and the memory being allocated. It can help developers to dig deep and analyze the mined metrics to pinpoint the problem or improvement areas in your application.

Always remember that as mobile developers it is crucial that our codes must always be optimized because performance (app fluidity, response time) is much more noticeable on mobile platforms compared to desktop computers.

 

 

Xamarin Profiler

Xamarin Profiler is a great tool created by Microsoft wherein it gives developers ways to profile or collect telemetry with your Mobile Applications using Visual Studio. The main function of the profiler is to collect and displays information about the Mobile App wherein the Developer can pinpoint, analyze and optimize areas in their application for a smooth experience by end users. There are different ways the Xamarin Profiler can help us like statistical sampling and Memory profiling. Later will have a go on the Memory profiling using our demo Application. The only caveat that I can think of is that Xamarin Profiler is currently available in Enterprise edition only.

 

Demo Application

For our demo application will be using Xamarin Forms Android version where in it will be composed of four pages, we have the Main Page and 3 sub pages. The application is simple enough that we will only navigate on different pages then back to the Main page. The only thing is that those sub pages are currently subscribing to the MainPage in order for the Sub pages label will be Updated. If you can browse the code you will quickly see that we are expecting a memory leak in the form of Messaging Center due to the subpages are not unsubscribing.

Using Xamarin Profiler

Let’s open the Demo Project and in order to start your profiling go to tools and click the Xamarin Profiler as seen in figure 1.

 

profiler xamarin erni apps

Figure 1. Xamarin Profiler under Tools Tab

 

You can select on the specific device you want to deploy on or you can also use the built in Emulator as shown in Figure 2.

 

device selection erni apps

Figure 2. Device Selection

 

Select the available tools you want to use as shown in Figure 3. In this demo we are going to use the Allocations wherein it mainly concerns the Memory allocations of your application. You can take snapshots and analyze the different objects.

 

select tools erni apps

Figure 3. Select Tools

 

Once you have selected a Allocations tool you can also configure some options of how the memory profiling will proceed. You can either customize the frames, frequency or either select from the presets as shown in Figure 4.

 

level of detail erni apps

Figure 4. Presets for Allocation configuration.

 

Once you have clicked the Start Profiling button it will automatically launch your selected application on the selected device and you will be greeted with this window as shown in Figure 5.

 

main window xamarin profiler

Figure 5. Xamarin Profiler Main Window

 

There are so many tools or data that the Xamarin Profiler is showing so I will discuss a quick run through on the different parts or categories based on their usage.

  • Brown Area – Represents the Selected device and the Application that is currently running.
  • Green Area – Represents the actual graph of the Memory consumption with respect to time.
  • Red Area – Represents the different data categorized on your selected tab.
  • Orange Area – Represents the information (Object name, count, size) displayed based on the selected tab.
  • Yellow Area – Represent the real time display of information based on the current snapshot.

Let’s now try to capture two snapshots of the Demo application by capturing snapshot 0 upon start of the application then lets navigate back and forth from main page to other pages and capture our Snapshot 1 as shown in figure 6.

 

Figure 6. Comparing snapshots

 

One main thing to point out is that both of the Snapshot increased memory consumption but let’s dig deeper on Snapshot 1 and check the effects of the memory leak brought by the messaging center. Just double click the Snapshot 1 at will open a more detail window of the different objects in the snapshot as shown in figure 7.

snapshot details erni apps xamarin

Figure 7. Snapshot details.

 

At first glance you will be overwhelmed on the number of objects you can see, but one thing that I am doing is that I’m filtering it to only live objects plus to your namespace only for easy tracking. For this demo we already know that the messaging center is the cause of the leak so will just filter the MessagingCenter as shown in Figure 8.

 

filtering of objects xamarin

Figure 8. Filtering of Objects.

 

Based on the number of objects that has been created on the Subscription it is correct which is 10 because I navigated back and forth 10 times between Main page and sub pages. Now that we have the benchmark to perform the fix let’s modify the code by adding the unsubscribe code on the OnDisappearing method as shown in Figure 9.

 

ondissapearing xamarin

Figure 9. Unsubscribe from the Messaging Center.

 

Let’s try to profile again our application to see if there is no Memory Leak due to the Messaging Center. So it’s still the same process, but important thing is to rebuild the project deploy it again then after successful deploying in the device that’s the time you start the profiler. Capture a snapshot upon loading of the application then try to navigate 10 times again back and forth then capture again a snapshot of the memory as shown in Figure 10.

 

fixed messaging center xamarin erni apps

Figure 10. Fixed Messaging Center

 

As you can see after we applied the fix there is no living object under Messaging Center which is a good thing. Well the consumed memory was low in this demo but imagine the impact that it will bring to an Enterprise grade application.

Here is the URL for the Sample Application: https://github.com/KirkPatrickJunsay/MemoryLeakDemo 

 

Conclusion

To wrap things up, we discussed Profiling our Xamarin Applications focusing on Memory Leaks. We also have an actual demo on the step by step procedure on running Xamarin Profiler. Let me state that Profilers are not only for memory leaks but also can be used in code optimization. Finally moving forward maybe you can also adapt or put in your development routine the profiling of your mobile applications. You can also check other cool Xamarin blogs on the URL below as we continue to celebrate Xamarin’s Month for the whole month of February. A big kudos also for Luis Matos for organizing the Xamarin’s Month.

Here is the URL for Xamarin’s Month Blog Posts: https://luismts.com/blog/xamarin/xamarin-month-february-2019/

News from ERNI

In our newsroom, you find all our articles, blogs and series entries in one place.

> Load more

ERNI Schweiz

Casinoplatz 2

3011 Bern

Phone: +41 58 268 12 00

Email: [email protected]

better ask ERNI

ERNI Suisse

Bâtiment L

Route des Acacias 43

1227 Geneva

Phone: +41 58 268 11 03

Email: [email protected]erni.ch

better ask ERNI

ERNI Suisse

Voie du Chariot 3

1003 Lausanne

Phone: +41 58 268 11 03

Email: [email protected]

better ask ERNI

ERNI Schweiz

Brünigstrasse 18

6005 Lucerne

Phone: +41 58 268 12 00

Email: [email protected]

better ask ERNI

ERNI Schweiz

Löwenstrasse 11

8001 Zürich

Phone: +41 58 268 12 00

Email: [email protected]

better ask ERNI

ERNI Deutschland

Trakehner Str. 7-9

60487 Frankfurt am Main

Phone: +49 697 953 28 01

Email: [email protected]

better ask ERNI

ERNI Deutschland

Design Offices München – Arnulfpark

Luise-Ullrich-Str. 20

80636 München

Phone: +49 162 334 77 30

Email: [email protected]

better ask ERNI

ERNI Slovakia

Ševčenkova 34

851 01 Bratislava

Phone: +421 2 32 55 37 37

Email: [email protected]

better ask ERNI

ERNI España

Edificio El Triangle

Plaça Catalunya 1-4, 3º planta, Módulo A y B

08002 Barcelona

Phone: +34 93 667 77 76

Email: [email protected]

better ask ERNI

ERNI España

Carrer Pallars, 208, Bajos

08005 Barcelona

Phone: +34 93 667 77 76

Email: [email protected]

better ask ERNI

ERNI España

Calle de Alfonso XII 62

Oficina 3101

28014 Madrid

Phone: +34 901 848 787

Email: [email protected]

better ask ERNI

ERNI España

Sant Cugat ERNI Office

Plaça Xavier Cugat, 2 EDIF B Planta Baja

08174 Sant Cugat del Vallès

Phone: +34 93 667 77 76

Email: [email protected]

better ask ERNI

ERNI Romania

Calea Dorobantilor no. 98-100

3rd floor, in Olimpia Business Center

400609 Cluj-Napoca

Phone: +40 744 319 228

Email: [email protected]

better ask ERNI

ERNI Singapore

7 Straits View

Marina One East Tower #05-01

Singapore 018936

Phone: +65 9161 9863

Email: [email protected]

better ask ERNI

ERNI Philippines

9th Floor, 500 Shaw Zentrum Building

500 Shaw Boulevard

Mandaluyong City, Philippines 1555

Phone: +63 2 531 59 82

Email: [email protected]

better ask ERNI