AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.43k stars 2.21k forks source link

Possible memory issue when drawing SKPicture directly on the canvas #13007

Closed BobLd closed 1 year ago

BobLd commented 1 year ago

Describe the bug There's a possible memory issue when drawing SKPicture directly on the canvas. After disposing of the SKPicture, it seems some (unmanaged) memory is not released.

Subsequent Loading/Unloading of the picture lead to growing unmanaged memory. This memory is never released it seems.

I'm using the control from https://github.com/wieslawsoltes/Svg.Skia to draw the SKPicture

To Reproduce Please find a minimal app to reproduce the issue here https://github.com/BobLd/IssueCanvas

Steps to reproduce the behavior:

  1. Open the minimal app
  2. Click the Load button
  3. Click the Unload button
  4. Click the Load button
  5. etc.

Expected behavior I would expect unmanaged memory to not grow over subsequent Load/Unload, and to be restored to baseline level.

Screenshots The screenshot below shows memory usage in dotMemory and the subsequent Load/Unload. As an FYI, the baseline (app started, but Load button never clicked) Unmanaged memory usage is around 70MB on my computer. We can see here that we end up at 250MB and stay there: image

Desktop (please complete the following information):

Additional context I'm drawing the picture that can be found here and you can use https://chrome-m93-debugger.skia.org/ to look into it

kekekeks commented 1 year ago

Does it grow indefinitely or stops at some point?

BobLd commented 1 year ago

@kekekeks It grows to roughly 250MB and stays there. Loading/Unloading does not increases unmanaged memory beyond this point. From what I see, it then stays at this level and never decreases.

After some time unloaded, I would expect unmanaged memory to go back to baseline level which does not seem to happen

kekekeks commented 1 year ago

You are saturating Skia's cache. You can try reducing said cache size.

BobLd commented 1 year ago

@kekekeks okay I did not know about that, thx a lot.

I understand now it's more a skiasharp question than an Avalonia one, but could I ask how you would reduce the skia cache in an Avalonia context?

BobLd commented 1 year ago

image

I've added the possibility to manually purge Skia's (non-gpu related) cache by doing SKGraphics.PurgeAllCaches(); (see doc here) and also added lease.GrContext.PurgeResources(); in the ICustomDrawOperation.

Doing so I managed to only reach ~210MB instead of the 250MB plateau, but the memory still increase as seen on the screenshot.

If anyone has an idea how to purge more of the skia cache, let me know

kekekeks commented 1 year ago

Are you running with integrated GPU? That might be memory pre-allocated by the driver.

timunie commented 1 year ago

@BobLd I honestly don't see a real memory leak here where memory increases infinite. For me it is more a question how to reduce memory in general. Correct me if I'm wrong.

BobLd commented 1 year ago

I did 2 other tests:

@timunie I don't think this is necessary a memory leak, I'm just puzzled as to why memory in not released when unloading the skPicture, and grows up to a certain level and stays there... Even more now that running this app on the GeForce card does not displays the same behaviour...

Not sure what to conclude, the behaviour seems to be limited to integrated GPU, and I don't know why it happens. Might indeed be 'pre-allocated memory', I was unaware of that - will look into it. (by the way, is there a way to force Avalonia to use one GPU in particular from the app itself, i.e. for it to use GPU 0 instead of GPU 1?).

Feel free to close this issue if you think this is in fact not an issue

timunie commented 1 year ago

I somehow feel like this is either an issue with the graphic card or with skia sharp, as we don't manage SKPicture. So probably you can ask SkiaSharp for help.

worth to notice that we have a PR open to test newer Skia-version here: #12729 You may want to try it out if it helps in your situation.

For now, I'll move this issue into a general discussion as I think the input is valueable for others, but not an Avalonia issue.