top of page
  • Writer's pictureOscar Fraxedas

Memory profiling in Xamarin

I'm currently working in a mobile application developed in Xamarin Forms.

There were some signs of memory issues and I finally decided to try Xamarin Profiler.

You'll need to be a Visual Studio Enterprise subscriber to use it, even a trial version will work.

After spending 5 mins reading the documentation and 2 mins running the application with the Profiler I was able to spot an issue: I was creating too many byte arrays.

Locating my code in the stack trace was simple enough.

This is what the function looked like:

public async Task<ImageSource> Adjust(Bitmap bitmap){

var stream = new MemoryStream(); await bitmap.CompressAsync(Bitmap.CompressFormat.Png, 100, stream); var tempFile = Path.GetTempFileName(); var tempFileSteam = new FileStream(tempFile, FileMode.Create); await tempFileSteam.WriteAsync(stream.ToArray(), 0, stream.ToArray().Length); retrun ImageSource.FromFile(tempFile);


I created a memory stream to hold a bitmap, I converted it to a byte array twice, before sending it to a file stream, without disposing of it.

The fix was very simple: I used a file stream to hold the bitmap within a using statement

The new function looks like:

public async Task<ImageSource> Adjust(Bitmap bitmap){

var tempFile = Path.GetTempFileName(); using (var stream = new FileStream(tempFile, FileMode.Create))

await bitmap.CompressAsync(Bitmap.CompressFormat.Png, 100, stream);

return ImageSource.FromFile(tempFile);


A new execution of the Profiler revealed that the problem with the byte arrays is gone.

Being able to tackle this small issue in a couple of hours is encouraging.

If you want to learn more about Xamarin Profiler visit:



73 views0 comments

Recent Posts

See All


bottom of page