Icons memory leaks - Xamarin.Android

Kenny Ali 1 Reputation point
2021-04-19T16:43:59.627+00:00

I've been hunting (without any great success) my app's icons memory leaks.

Very close to the release date of the app I noticed that my app started to crash due to a ram error (Out of Memory Exception).

I start testing a lot of variations trying to figure out where the leak was, and so finally noticed that right after startup in the Splash screen I have a memory usage of 100%.

Then Finish the Splash and show the MainLayout and have a usage 92%. Trigger the "cause the GC" and got 82% (the lowest usage no matter what I do):

| ID: 1 | | Heap Size 89.016 MB | | Allocated 73.016 MB | | Free 16.000 MB | | Used 82.03% | | Objects 80,651|

And at that point it was actually doing nothing (I removed all the code), the app was just the skeleton (TextViews, empty RecyclerViews and the images / icons).

Made another test with any "src" into the ImageViews and finally some improvement, it drops to 67.57%:

| ID: 1 | | Heap Size 49.332 MB | | Allocated 33.332 MB | | Free 16.000 MB | | Used 67.57% | | Objects 79,165 |

So, sadly, I thought that a solution could be stop using icon images, but still i think that usage is way to high.

Any ideas?

Developer technologies | .NET | Xamarin
Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. JessieZhang-MSFT 7,716 Reputation points Microsoft External Staff
    2021-04-20T03:34:35.277+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    For this, you can check the offical document Improve Xamarin.Forms App Performance.

    Specially part Optimize image resources, from this document ,we know:

    Displaying image resources can greatly increase an application's memory footprint. Therefore, they should only be created when required and should be released as soon as the application no longer requires them. For example, if an application is displaying an image by reading its data from a stream, ensure that stream is created only when it's required, and ensure that the stream is released when it's no longer required. This can be achieved by creating the stream when the page is created, or when the Page.Appearing event fires, and then disposing of the stream when the Page.Disappearing event fires.

    When downloading an image for display with the ImageSource.FromUri method, cache the downloaded image by ensuring that the UriImageSource.CachingEnabled property is set to true.

    In addition, you can try to optimize your code by refering to document: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/deploy-test/performance .

    Best Regards,

    Jessie Zhang

    ---
    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.