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

Use the ArrayPool API in .NET Standard 2.1 or later to reduce the creation of LOH objects. #1812

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Wak1Z2
Copy link

@Wak1Z2 Wak1Z2 commented Mar 21, 2025

Hi, I'm a programmer from China. The text below is translated using AI, as my English isn't proficient enough for direct conversation, though I'm actively working to improve.

I'm developing a personal web application using Magick.NET. During development, I noticed that converting RAW photo files to JPG generates a significant number of LOH (Large Object Heap) objects. After reviewing the source code, I traced the issue to the ResizeBytes method in the ByteArrayWrapper class. This method reallocates byte arrays based on the length returned by localized code, causing unused arrays to accumulate in the LOH. To address this, I implemented the ArrayPool API to reduce LOH allocations. After the modifications, the program now generates only a small number of LOH objects per run, drastically reducing LOH allocation.

My environment uses .NET 8, and the ArrayPool API is only available in .NET Standard 2.1 or later, meaning it cannot compile in versions prior to .NET Standard 2.0 (e.g., .NET Framework 4.6.1).

Below are screenshots of how my interface uses Magick.NET and the memory monitoring. Each LOH change in the screenshots corresponds to one program execution.

eca6fe889feeb0061d073ceab97fcf34

Before:
before1
before2
After:
after1
after2
after3

Here are my test photos, captured using a Nikon Z8 in uncompressed RAW format.
https://github.com/Wak1Z2/Magick.NET/tree/main/testImage

Thank you for reviewing my code! This is my first attempt at contributing to an open-source library.

Wak1Z2 added 2 commits March 19, 2025 02:30
…前的读取一次创建80MB的LOH对象减少到读取一次只创建16MB的LOH对象。目前仅在net8编译。
@CLAassistant
Copy link

CLAassistant commented Mar 21, 2025

CLA assistant check
All committers have signed the CLA.

@dlemstra
Copy link
Owner

Thanks for your pull request and detailed explanation. I am right now traveling so it might take a while before I have time to review this.

@Wak1Z2
Copy link
Author

Wak1Z2 commented Mar 22, 2025

Thanks for your pull request and detailed explanation. I am right now traveling so it might take a while before I have time to review this.

Okay, wish you a pleasant journey~

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

Successfully merging this pull request may close these issues.

3 participants