-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Memory leak occurs when QR codes used Base64QRCode class #466
Comments
I took a long look at this. I don't think there's an actual bug here. Ultimately I think what's happening is that there is enough memory being allocated such that the majority of memory gets bumped to generation 1/2 where it sits for a long time before being collected. It seems to be a combination effect of (1) ASP.NET Core (2) generating the QR code matrix (3) using System.Drawing.Common to generate the bitmap (4) the long input string. Take away any one part and the effect is greatly minimized. However, regardless of the amount of memory that gets allocated, there's no memory leak. The process simply allocates memory up to a threshold, 1.3GB on my PC, and then it performs more GC generation-2 collections to compensate. To test this I've downloaded the sample program above, and added this immediately prior to the Debug.WriteLine($"Total memory: {GC.GetTotalMemory(false)}"); Running the program and repeatedly downloading the image then produces results like the following:
Note that this reduction of utilized memory does not release the RAM to the system, so a process viewer would still show 650+ MB allocated to the process. It is, however, available for reuse by .NET. This results in a process memory graph like this: The bottom line is that the The best start towards solving this problem would be to add a benchmarking project to QRCoder relying on the |
I added PR #495 which uses the |
Thanks for the conclusion. In view of the issue title (memory leak) - which is not present as shown and the fact that with #495 there is also an option with better performance (Base64QRCode with ImageType == PNG) now, I would like to close the issue here. |
Type of issue
[X] Bug
[ ] Question (e.g. about handling/usage)
[ ] Request for new feature/improvement
Expected Behavior
To generate QR codes through endpoints, is there any recommended approach or is there a standard way?
var qrGenerator = new QRCodeGenerator();
var input = "00263fd622";
var qrCodeData = qrGenerator.CreateQrCode(input, QRCodeGenerator.ECCLevel.Q);
var qrCode = new Base64QRCode(qrCodeData);
var qrCodeImage = qrCode.GetGraphic(20);
Current Behavior
When there are multiple calls to the endpoint that contains the QR code generation code, the application's memory keeps increasing. While debugging, I was able to verify that this happens when the CreateQrCode method is invoked.
The application starts using 82.7 MB.
After 20 requests, the memory increases to 398.9MB.
After 100 requests, the memory increases to 1GB.
I have already tried calling the dispose methods of the qrGenerator, qrCodeData, and qrCode objects. I have also used the using statements for these objects and directly called GC.Collect(), but none of them were able to deallocate from memory.
Possible Solution (optional)
Steps to Reproduce (for bugs)
QrCoderWebTests.tar.gz
I created (POC) with the mentioned scenario, execute dotnet run, call endpoint http://localhost:5098/
and used a memory monitoring tool to track memory usage.
Your Environment
Version used: 1.4.1
Environment : .net Core 6
The text was updated successfully, but these errors were encountered: