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

[Question] About the problem of increasing memory since the scan #59

Open
OilPrince opened this issue Apr 1, 2024 · 1 comment
Open

Comments

@OilPrince
Copy link

Hello!

I'm trying to scan using 'Sample.Winform' inside the ntwain program source.

The function is working normally, and I am monitoring this program as a task manager.

However, after proceeding with the scan, I checked that the memory of the application continues to increase.

After scanning a large amount of paper documents, there is a risk of memory leak later.

Is there a way to manage memory properly?

Please give me some advice on this.

Thank you.

@Ragnorok567
Copy link

Ragnorok567 commented Apr 24, 2024

I had a similar issue. Every time I initiated a scan the memory usage would just continue to grow and no amount of disposes and forced garbage collection would help.
The solution I ended up with was fairly simple. When calling the e.GetNativeImageStream be sure to make sure it is in a using clause like this
if (e.NativeData != IntPtr.Zero)
{
using (Stream stream = e.GetNativeImageStream())
{
if (stream != null)
{
img = Image.FromStream(stream);
}
}
}
It seems when the stream is put into a variable the system will not garbage collect even after flushing, closing, and disposing of the stream.

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