Skip to content

Image resizing increases TotalUndisposedAllocationCount #2100

Description

@BaileyMillerSSI

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am running the latest version of ImageSharp
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • I have searched open and closed issues to ensure it has not already been reported

ImageSharp version

2.1.1

Other ImageSharp packages and versions

None

Environment (Operating system, version and so on)

Window 11 Pro, Version 10.0.22000 Build 22000

.NET Framework version

net6.0

Description

When doing a simple image resize the TotalUndisposedAllocationCount increases per image resized.

Steps to Reproduce

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Diagnostics;

var photos = Directory.EnumerateFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "*.png", SearchOption.AllDirectories).Take(5);

foreach (var photo in photos)
{
    Console.WriteLine($"Number of undisposed ImageSharp buffers: {MemoryDiagnostics.TotalUndisposedAllocationCount}");

    using (var outputStream = new MemoryStream())
    using (var imageStream = File.OpenRead(photo))
    {
        var (image, format) = await Image.LoadWithFormatAsync(stream: imageStream);

        image
            .Mutate(x =>
                x.Resize(image.Width/2, 0, KnownResamplers.Lanczos3));

        await image.SaveAsync(outputStream, format);

        outputStream.Position = 0;

        Console.WriteLine($"Number of undisposed ImageSharp buffers: {MemoryDiagnostics.TotalUndisposedAllocationCount}");
    }
}

Images

Snapchat-1117624853

Activity

  1. JimBobSquarePants commented on Apr 28, 2022

    @JimBobSquarePants
    Member

    You're not disposing of the image in your sample code.

     outputStream.Position = 0;
    + image.Dispose();

    image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions