dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.97k stars 1.71k forks source link

Memory Leak When updating <image /> Image Source from View model [Observable Property] using memory Stream. 60 fps Byte[] to the view #23574

Open PickleBurg opened 1 month ago

PickleBurg commented 1 month ago

Description

The issue.

While running my application, I get a memory Leak When updating Image Source from my Mvvm View model using the [Observable Property].

I use memory Stream. to create an Image Source for the Observable property and this image is updated roughly 60 times per second.

[ObservableProperty]
private ImageSource _cameraImage;

private void UpdateImage(object state)
{
    // Generate random image byte array
    byte[] imageData = ImageGenerator.GenerateRandomImage();

    // Update the Image control on the main thread
    _frameData = imageData; // Store the frame data

    // Create a new StreamImageSource from the frame data
    var streamImageSource = new StreamImageSource
    {
        Stream = token =>
        {
            // Create a MemoryStream from the frame data
            MemoryStream memoryStream = new MemoryStream(_frameData);
            _frameData = null; // Clear the frame data after creating the stream to release memory
            return Task.FromResult<Stream>(memoryStream);
        }
    };

    // Assign the new StreamImageSource to the CameraImage property to update the UI
    CameraImage = streamImageSource;
}

In the Below images, shows the Action<Object, Microsoft.Maui.Controls.Internals.ResourcesChangedEventArgs> which will continue to climb until the application runs out of memory.

image

image

I have Not been able to find a work around or a solution to Clean the memory out of the binding context of the page.

Steps to Reproduce

1: Clone The following Repo: https://github.com/PickleBurg/ImageSource-Memory-Leak

2: Run application

3: check Diagnostic tools and Process Memory usage.

Link to public reproduction project repository

https://github.com/PickleBurg/ImageSource-Memory-Leak

Version with bug

8.0.40 SR5

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

net8.0-windows10.0.19041.0

Did you find any workaround?

Unable to find a solution

Relevant log output

No response

github-actions[bot] commented 1 month ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

ninachen03 commented 1 month ago

This issue has been verified using Visual Studio 17.11.0 Preview 3 (8.0.70 & 8.0.21). Can repro it.