Xamarin Profiler and Memory Management

Read it in 10 Mins

Last updated on
17th Mar, 2021
Published
03rd Nov, 2020
Views
8,843
Xamarin Profiler and Memory Management

App memory management has become one of the key priorities for any developer today. With new-gen energy-efficient processors launching every day &  operating systems shifting towards efficient power usage along with sleek UI experience, today’s developers face a challenge of developing apps that are sleek & efficient in memory management along with an intelligent tool to check memory leakage proactively. In this article, I will be covering two major topics i.e. xamarin profiler (Part 1) and memory management (Part 2). 

Part 1: Xamarin Profiler 

Introduction to Xamarin Profiler: 

Xamarin Profiler is a tool developed by Microsoft that helps the developers to analyze the app's behavior, and its memory allocations with the help of a visual studio. It is a graphical interface that supports doing profiling in android, iostvos, mac applications in mac and android, iostvos applications in windows.  

Why use a profiler: 

We all know that sometimes we don't properly dispose of the objects and therefore the garbage collector will not collect them and remove them from the memory.  

 In other words, with the help of the profiler, we can check memory leaks that are being held by objects which should ideally be disposed of.  Before that, let's understand how the operating system manages the memory. 

Xamarin Profiler and Memory Management

There are two types of memory, native memory and managed memory. All the ios and android related objects like views, example buttons, text views fall into the native memory and the dot net-related objects, for example, subscriptions, event handlers, threads go into managed memory. Whenever there is a process, the operating system gives a small piece of this memory chunk to xamarin mono. So in order to check the memory allocations of managed memory, we need a profiler.  

Garbage Collector:

 Before getting started with the profiler, let's understand what a garbage collector is. The garbage collector collects the disposed objects and frees up the memory.  As I discussed above there are two memory worlds in the operating system, native memory, and managed memory. Sometimes garbage collectors cannot do their job, because some of the dot net objects may be holding reference to native objects or vice versa. In such cases, it is difficult to understand where the memory leak is happening. So this is where the profiles come into the picture.  

Prerequisites: 

Visual Studio (Mac or Windows) 

Enterprise license 

Device or emulator 

Download And Installation: 

Step 1: Download and install the profiler for windows from the below link 

https://docs.microsoft.com/en-us/xamarin/tools/profiler/?tabs=macos#download-and-install 

Step 2: Click on the Next button on the window 

Xamarin Profiler and Memory Management
Step 3: Click on the checkbox to agree to the license agreement and click next.  

Xamarin Profiler and Memory Management

Step 4: Choose the destination folder where you want to install the xamarin profiler and click next again.  

Xamarin Profiler and Memory Management

Step 5: Click on the install button and wait until the installation completes.  

Xamarin Profiler and Memory Management

Xamarin Profiler and Memory Management

Now that we have successfully installed the profiler, we will have to launch it. But before that, we have to enable profiling in our app. Let's discuss that in the next section. 

Launching Profiler From Visual Studio: 

Select android project, right-click and select properties, 

Build ---> Android Debug---> Android options ---> Enable Developer Instrumentation 

Once the above step is completed, open visual studio and load your application and keep the app in default debugging mode. Now go to Analyze---> Xamarin Profiler. Once you click on xamarin profiler, the profiler starts profiling automatically.

Xamarin Profiler and Memory Management

In case you don't see the xamarin profiler option, check the troubleshooting guide in the below link. 

Xamarin Profiler Troubleshooting - Xamarin 

Deep Dive Into Profiler Instruments: 

Xamarin profiler consists of three main instruments for profiling and they are allocations, cycles and time profiler. Let us understand how each of them works. 

1. Allocations: Allocations chart is at the top of the profiler as shown in the below figure. Allocations represent the amount of memory being collected by the garbage collector at regular intervals of time during profiling. This instrument uses three data views that help the developer to understand how much of the memory is being used and released. 

  • Allocations: This represents the memory allocated by the classes and their associated methods. When you select a class, it shows the memory allocated to that particular class

Xamarin Profiler and Memory Management

  • Call Tree: This displays the call tree of the threads used in the application. In other words, it shows the memory allocated to each thread.  

Xamarin Profiler and Memory Management

  • Snapshots: This section displays what memory is retained and released. To generate this information, you need to click on the camera button on the toolbar as shown in the below figure. The snapshots information can be captured only during live profiling.  

Xamarin Profiler and Memory Management

2. Time Profiler: This instrument represents the amount of time taken by each method in the application. The application is paused at regular intervals and a stack trace is run on each thread. 

Xamarin Profiler and Memory Management

3. Cycles: We often never dispose of the objects as sometimes they will be holding a reference to other objects. The cycles instrument represents the cycle of these objects in the application.  

Xamarin Profiler and Memory Management

Part B: 

Memory Management: 

Introduction: When it comes to a mobile application or a website, it is always necessary to ensure proper management of objects and resources to avoid sluggishness when using the app. If not taken care of, it would lead to slow performance and crashes when used continuously. In this article, I will discuss how to prevent memory leaks and improve the performance of the app. 

Common Memory Leaks: Memory leaks can be very dangerous because they lead to crashes if allocated memory is full. They can show down the app performance and sometimes make it unresponsive. 

Xamarin Profiler and Memory Management

Let us see few of the common memory leaks that we make while developing our applications. 

  • Subscribing or registering to event handlers, delegates and messaging centers can cause memory leaks if not unsubscribed and unregistered at the right place.  
  • Using views, example buttons and images cause a memory leak because the garbage collector does not collect them until they are disposed of. 
  • Caching mechanisms to store data can cause out of memory exceptions because the memory keeps filling up and the app crashes. 
  • Threads should be used carefully because any reference objects will not be collected by the garbage collector.  
  • Using static variables, events are GC roots and are not collected by the garbage collector.  
  • Using bindings can cause memory leaks. So always bind to the dependency object or INotifyPropertyChanged to avoid memory leaks. Avoid using bindings in unnecessary places. 

Steps for preventing memory leaks: 

Below are a few approaches to avoid memory leaks. They not only improve the performance of the app, but also reduce the work done by the CPU. Let’s discuss them one by one.  

1. Use of Asynchronous operations: We can use asynchronous operations to many of our functions especially for long-running tasks. Asynchronous operations do not block the UI thread and ensure a smooth transition of user interactions. To enable asynchronous operations, dot net provides async and await keywords which can be used to call asynchronous APIs.  

2. Reducing the application size: In order to reduce the application size, we need to understand the compilation process of each platform.  

Android uses a just-in-time (JIT) compiler to convert to an intermediate language. 

Windows uses a built-in-time compiler to convert to an intermediate language. 

Ios uses an ahead-of-time compiler to convert to ARM assembly language.  

For all of these platforms, appropriate linker options should be selected in order to strip off the unused framework class. By default, in xamarin the linker is disabled. We need to enable the correct linker so that during the run time, the application determines which members, types are used and removes the unused types. 

To understand how to select an appropriate linker, go through the below link 

Cross-Platform Performance - Xamarin 

3. Optimizing image assets: Images are the most expensive resources an application can have as we use them at high resolutions and they occupy a lot of memory. We need to optimize images based on the phone resolution by having different resolutions and fit them based on the phone size. For example, use a small resolution image if you are using it in a listview rather than a big image that fits a full screen because there is a lot of CPU wastage to decode them to smaller sizes. Also, use the images only when required and release them when not in use. 

4. Profilers: Profiler is a great tool to analyze the application behavior and check for memory leaks. They can also be combined with native instruments to check memory allocations of native objects. I hope you already know how to use them as we have discussed xamarin profilers in PART- A. 

5. Test for memory leaks in the app: It is always best practice to test memory leaks for every function you do. Below is the sample code to test memory leaks. 

[Test] 
        void MemoryLeakTest() 
        { 
           var weakReference = new WeakReference(leakyObject) 
            GC.Collect(); 
            GC.WaitForPendingFinalizers(); 
            GC.Collect(); 
            Assert.IsFalse(weakReference.IsAlive); 
        }

6. Unsubscribe from events: It is always best practice to unsubscribe from events to dispose of them so that the garbage collector does its job. We often forget to do that and this leads to memory leaks as the object is still holding the reference and keeps it alive. This has to be taken care of whenever using threads, messaging center, event handlers, and delegates. Below is the sample code to unsubscribe from events and dispose of them 

public class EventDemo
    {
        public event EventHandler DemoEvent;
        public void OnMyEventFires()
        {
            if (DemoEvent != null)
            {
                DemoEvent(this, EventArgs.Empty);
            }
        }
    }
    public class SubscriberDemo : IDisposable
    {
        readonly EventDemo  eventDemo;
      public SubscriberDemo(EventDemo  _eventDemo)
       {
           eventDemo = _eventDemo;
            eventDemo.DemoEvent += OnDemoEventFires;
        }
        void OnDemoEventFires(object sender, EventArgs e)
        {
            Debug.WriteLine("The publisher notified the subscriber of an event");
        } 
       public void Dispose()
        {
            eventDemo.DemoEvent -= OnDemoEventFires;
       }
    }

Using Dispose Pattern: The dispose method is used to release unmanaged resources. Our garbage collector does not allocate or release unmanaged memory. The pattern for releasing these objects is called a dispose pattern. In order to use the dispose method, you need to inherit your class with IDisposableInterface.  Below is the code snippet showing how to use dispose method.  

 public void Dispose() 
    { 
        // Dispose of unmanaged resources. 
        Dispose(true); 
        // Suppress finalization. 
        GC.SuppressFinalize(this); 
    } 

Using Statement: Whenever working with files, steam readers, graphics in .Net framework, we create a lot of unmanaged code. By implementing the using statement, the code is transformed into a try/finally statement behind the scenes and the dispose method is called at the final clause. So ensure that you implement “using statement” whenever necessary.  

public void CreateFile() 
    { 
        using (var stream = new FileStream(@"\Document\YourFile.txt", 
                                           FileMode.OpenOrCreate)) 
        { 
            // do your stuff   
 
        }// stream.Dispose()  
    } 

Size Reduction Techniques: Below are few approaches to reduce the application size. 

  • Always generate the build in release mode. 
  • Make sure the LLVM compiler is used. 
  • Enable the appropriate linker in each project (Link all for ios projects and Link all assemblies for android projects) 
  • Reduce the number of architectures to avoid FAT binary being produced. 

Reducing Web Service Communication: We often make multiple calls over the web in order to transfer and receive data. This communication uses more network bandwidth and drains the phone's battery. As a result, there is a performance hit on our application. There are a few approaches that can help us here. 

  • The JSON format is always preferred as it uses lesser bandwidth while sending and receiving data over the network. 
  • Using data transfer objects (DTO) during data transfer between the mobile application and web service because more data can be transferred in a single remote call; thereby reducing the number of network calls. 
  • Once the web service call is made, it is preferred to cache the data locally instead of requesting the server multiple times. 
  • One more approach to reduce the network bandwidth is to compress data before transferring it over the network. However this might increase the CPU usage and drain the battery. Therefore this approach must be carefully evaluated before using it.  

Conclusion:

To sum up the things we have covered, we now know what causes memory leaks, how to prevent them and how to use xamarin profiler to find memory allocations, improve application performance and do code optimization. So let us adapt to a routine to bring out these practices while building mobile applications.  

I hope this article helps you and saves a lot of time. Please do share your feedback and let me know if you have any queries. 

Profile

Mounika Rajapuram

Module Lead

Mounika calls herself a dynamic professional with expertise in developing mobile applications for iOS and Android. With C# and Xamarin as primary areas of expertise, she also likes to dwell in doodle art in her pastime.