ntwain icon indicating copy to clipboard operation
ntwain copied to clipboard

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

Open OilPrince opened this issue 10 months ago • 1 comments

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.

OilPrince avatar Apr 01 '24 06:04 OilPrince

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.

Ragnorok567 avatar Apr 24 '24 20:04 Ragnorok567