[英]C# MultiThreading is causing unhandled exception of type 'System.OutOfMemoryException'
[英]C# System.OutOfMemoryException was unhandled
我使用foreach从文件夹中读取所有图像
string[] filePaths = Directory.GetFiles(Workspace.InputFolder, "*.*");
foreach (string imageFile in filePaths)
{
// Some Process here, the output are correct, just after output
the error happen
}
但是出来错误
System.OutOfMemoryException was unhandled
Message=Out of memory.
Source=System.Drawing
问题是由foreach循环导致的,在过程完成后继续循环吗? 我应该怎么做才能释放内存? 谢谢。
考虑到您的异常,您似乎正在使用System.Drawing
命名空间中的对象。
例如,如果要在foreach循环中打开和操作图像,请确保在Dispose()
完图像资源后立即调用Dispose()
释放图像资源。 您也可以将其包装在using
语句中,即:
foreach (string imageFile in filePaths)
{
using (var image = Image.FromFile(imageFile)
{
// Use the image...
} // Image will get disposed correctly here, now.
}
请注意,问题可能不仅是图像,还包括实现IDisposable
任何资源。 System.Drawing
中的许多类都是可抛弃的-确保您按上述方式(通过使用)访问它们,或在完成后对其调用Dispose()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.