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
26.06k stars 2.25k forks source link

Memory leak in Vulkan Renderer #17363

Open iejeecee opened 4 weeks ago

iejeecee commented 4 weeks ago

Describe the bug

The Vulkan Command Buffers created in the function ImportedSemaphore.SubmitSemaphore in the file Avalonia.Vulkan.VulkanExternalObjectsFeature.cs are never released causing a memory leak.

https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Vulkan/VulkanExternalObjectsFeature.cs#L171

Adding a call to _pool.FreeFinishedCommandBuffers() at the beginning of the function fixes the leak, but I cannot oversee if this breaks something unintentionally

void SubmitSemaphore(VulkanSemaphore? wait, VulkanSemaphore? signal)
{
    _pool.FreeFinishedCommandBuffers();

    var buf = _pool.CreateCommandBuffer();
    buf.BeginRecording();
    _context.DeviceApi.CmdPipelineBarrier(
        buf.Handle,
        VkPipelineStageFlags.VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
        VkPipelineStageFlags.VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
        0,
        0,
        IntPtr.Zero,
        0,
        IntPtr.Zero,
        0,
        null);

    buf.EndRecording();
    buf.Submit(wait != null ? new[] { wait }.AsSpan() : default,
        new[] { VkPipelineStageFlags.VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT },
        signal != null ? new[] { signal }.AsSpan() : default);
}

To Reproduce

E.g running the GpuInterop sample (with a Vulkan backend) and continuously modifying the teapot causing the screen to update. You can observe the memory leak in Task Manager on Windows

Expected behavior

Expected behavior is that the amount of memory allocated doesn't keep increasing while moving the teapot around

Avalonia version

11.1.4

OS

Windows

Additional context

No response

Gillibald commented 4 weeks ago

@kekekeks

kekekeks commented 4 weeks ago

We probably want to do that once per frame rather than in random parts of the codebase (like we currently do with ImportImage).