Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux high memory usage .net 5 #26

Open
D4n1aLLL opened this issue Mar 15, 2022 · 3 comments
Open

Linux high memory usage .net 5 #26

D4n1aLLL opened this issue Mar 15, 2022 · 3 comments

Comments

@D4n1aLLL
Copy link

Is there any way to diagnose Unmanaged memory leak ?

I am using .NET 5.0 Console App with NetCode. The program it self is simple it calls barcode library & creates a base64string from Image 5000 times, I am using 'using blocks' therefore disposing is also being handled.

static void Main(string[] args)
{
    Console.ReadKey();
    for (int i = 0; i < 5000; i++)
    {
        Barcode bar = new Barcode("123456789123456", Type.Code128);
        using (var image = bar.GetImage())
        {
            using (MemoryStream ms = new MemoryStream())
            {
                image.Save(ms,ImageFormat.Png);
                var base64 = Convert.ToBase64String(ms.ToArray());
                Console.WriteLine(i);
            }
        }
    }
    Console.ReadKey();
    Console.ReadKey();
}

On windows this program consumes 15-25 MB (doesn't go above that) but on Linux Unmanaged memory constantly increases with each iteration but never goes down at all (goes upto 600MB on 5000 iterations).

enter image description here

Linux dotMemory:

enter image description here

enter image description here

Windows dotMemory:

enter image description here

I have tried the same program after fixing the Font, FontFamily disposing issue but the results are same.

I am using docker with:

FROM mcr.microsoft.com/dotnet/aspnet:5.0.15-focal as base
FROM mcr.microsoft.com/dotnet/sdk:5.0.406-focal AS build

Whole demo with dockerfile & dotmemory snapshots can be found here.

@D4n1aLLL
Copy link
Author

D4n1aLLL commented Mar 16, 2022

#27

I was able to solve the issue.

The issue is not related to NetBarcode, System.Drawing.Common (atleast on windows). On linux environment we use libgdiplus.
NetBarcode.Barcode.GetImage() creates a bitmap:

var bitmap = new Bitmap(_width, _height);

This by default creates a bitmap having 32bit depth (RGBA).

public Bitmap(int width, int height) : this(width, height, PixelFormat.Format32bppArgb)
{
}

I suspect on Linux GDI+ is no properly handling 32bit color images.
If you explicitly specify bitmap to use 24Bit:

var bitmap = new Bitmap(_width, _height, PixelFormat.Format24bppRgb);

The memory leak issue will be fixed.

@Tagliatti
Copy link
Owner

Thanks for the contribution. Could you post the memory diagnostics in version 1.4.5?

@D4n1aLLL
Copy link
Author

Thanks for the contribution. Could you post the memory diagnostics in version 1.4.5?

Currently on version 1.4.5 the memory usage is at 50ish MB. This can further be increased (a bit only) by disposing Font & FontFamily Barcode.cs:55.

Also for future versions of .NET (6 and above) I would recommend switching to SkiaSharp or ImageSharp (SkiaSharp is faster compared to ImageSharp) as Microsoft has restricted System.Drawing.Common to Windows only in .NET 6.

image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants