soukoku / ntwain

A TWAIN lib for dotnet.
MIT License
119 stars 50 forks source link

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

Open OilPrince opened 6 months ago

OilPrince commented 6 months ago

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 commented 5 months ago

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.