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.
For enquiries call:
+1-469-442-0620
HomeBlogWeb DevelopmentXamarin 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).
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, ios, tvos, mac applications in mac and android, ios, tvos applications in windows.
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.
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.
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.
Visual Studio (Mac or Windows)
Enterprise license
Device or emulator
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
Step 3: Click on the checkbox to agree to the license agreement and click next.Step 4: Choose the destination folder where you want to install the xamarin profiler and click next again.
Step 5: Click on the install button and wait until the installation completes.
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.
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.
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.
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.
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.
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.
Let us see a few of the common memory leaks that we make while developing our applications.
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 a few approaches to reduce the application size.
Looking to enhance your skills? Join our programming certificate course and unlock endless opportunities. Start your journey today!
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.
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.
Name | Date | Fee | Know more |
---|