Skip to content

Memory leak when using PlatformImage class #31108

@Nikita-181

Description

@Nikita-181

Description

I need to get the height and width of an image. According to the example article on Microsoft Learn, I use the PlatformImage class. When loading a 2.40 MB photo 10 times (I need to know the sizes of several images), the application's RAM consumption grows to 400 MB! When I click again(grows to 900 MB), the memory is not cleared (despite the request for garbage collection and using), the memory occupied continues to increase and is not cleared.

On Android everything works very slowly.

Example image

Image

memory.leak.mp4
Image

Steps to Reproduce

1.Download sample image
2. Create MAUI project
3. Change MainPage.xaml.cs to:

using Microsoft.Maui.Graphics.Platform;

namespace MauiApp1
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void OnCounterClicked(object? sender, EventArgs e)
        {
            string path = @"PXL_20250503_113944303.jpg";// path to example image

            for (int i = 0; i < 10; i++)
            {
                using (FileStream picDataForMeta = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    using var img = PlatformImage.FromStream(picDataForMeta);
                }
            }
            GC.Collect();
        }
    }
}
  1. Set the path to the downloaded image to the variable "path"
  2. Run app and click button "Click me"

Link to public reproduction project repository

No response

Version with bug

9.0.30 SR3

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

No response

Affected platforms

Android, Windows

Affected platform versions

Android 13, Android 16, Windows 10

Did you find any workaround?

No response

Relevant log output

Metadata

Metadata

Assignees

No one assigned

    Labels

    perf/memory-leak 💦Memory usage grows / objects live forever (sub: perf)platform/windowss/no-reproCan no longer be reproduced on latests/triagedIssue has been reviewedt/bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions