3

我创建了一个测试 WPF 应用程序来测试 SixLabors.ImageSharp,以查看它的性能以及它是否满足某些要求等,然后再集成到生产应用程序中。

我的测试应用程序包含一个 Image 控件,用于显示将要操作的图像,以及几个按钮用于加载图像、旋转、应用旋转。

当应用程序启动时,它会消耗大约 30-40MB 的系统内存。

但是,一旦我使用以下代码对图像应用旋转并保存回磁盘,内存使用量就会从 ~25-40MB 变为 ~300-500MB。

如果我对图像应用第二次旋转,内存使用量会再次上升到大约 600-800MB 左右。

如果我对图像应用第三次旋转,内存使用量再次上升到大约 850-1,000MB 左右。

如果我对图像应用第四次旋转,内存使用量会下降到执行第一次旋转的位置,大约 300-500MB。

我指定应用程序使用 MinimalPooling 来尝试降低内存使用量。

Configuration.Default.MemoryAllocator = ArrayPoolMemoryAllocator.CreateWithMinimalPooling();

轮换代码

private void ApplyRotation()
{
    string output;

    using (SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(imageToProcess))
    {
        // Decide based on the angle which rotation to use/
        // Rotate the image in place.
        // 'x' signifies the current image processing context.

        switch (angle)
        {
            case 0:
            case 360:

                break;

            case 90:
                image.Mutate(x => x.Rotate(RotateMode.Rotate90));

                break;

            case 180:
                image.Mutate(x => x.Rotate(RotateMode.Rotate180));

                break;
            case 270:
                image.Mutate(x => x.Rotate(RotateMode.Rotate270));

                break;

        }

        // The library automatically picks an encoder based on the file extension then
        // encodes and write the data to disk.
        // You can optionally set the encoder to choose.
        output = imageToProcess;

        image.Save(output);
    }
}

查看ImageSharp 文档,它指出 ImageSharp 保留了大约 300-400MB 的内存,因为它们使用了 ArrayPools。这解释了第一次使用库时的第一次内存跳转,但是随后的内存使用跳转我不明白也不知道如何释放。

我用过

Configuration.Default.MemoryAllocator.ReleaseRetainedResources();

正如在他们的文档中发现的那样,我认为应该释放所持有的资源。情况似乎并非如此。如果我对我的图像应用 3 次旋转并且内存使用量约为 1GB,那么在运行的应用程序中任何时候都不会减少到 300MB 以下,即使控件正在另一个窗口上使用然后随后关闭也是如此。

有谁知道如何减少这种内存使用或处理所有东西,以便内存使用恢复正常。

谢谢

4

0 回答 0