我这里有一些代码
private void Run()
{
MyClass c = new MyClass();
c.Load(somepath1);
using (StreamReader sr = new StreamReader(filepath))
{
string line = string.Empty;
while ((line = sr.ReadLine()) != null)
{
using (Bitmap B = new Bitmap(line))
{
Point p = SomeMethod(ref c, new Point());
using (MemoryStream ms = new MemoryStream())
{
B.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
using (Bitmap T = new Bitmap(new Bitmap(Image.FromStream(ms))))
using (Graphics g = Graphics.FromImage(T))
{
g.DrawEllipse(new Pen(Brushes.Red, 4), p.X - 5, p.Y - 5, 10, 10);
FileInfo fi = new FileInfo(somepath2);
T.Save(Path.Combine(somepath3, fi.Name));
}
}
}
}
}
}
并且函数 SomeMethod 是:
Point SomeMethod(ref MyClass c, Point mid)
{
float[] Mat = new float[9];
Point p;
c.Method1(Mat);
c.Method2(Mat, out p);
return p;
}
MyClass 是:
public class MyClass
{
public void Method1(float[] Mat, out Point point)
{
//calculation point from values in Mat
}
public void Method2(float[] Mat)
{
//Do some Operation in Mat
}
public void Load(string FileName)
{
//Do Some Data Loading From a small file about 400 byte
}
}
StreamReader sr打开了一个包含大约400行图像位置的文件,我根据我的计算读取它们并在它们上面绘制一些东西,我没有使用任何外部库或不安全的代码。问题是为什么我会耗尽内存?
-------------编辑--------------------
程序启动时使用约20MB的内存,在调用Run之后,内存使用量开始增加,如果我运行大约200张图片,内存使用量就会达到约1.7GB,并且Run函数完成工作后,内存使用量会恢复到20MB。
------------编辑------------ 将位图B保存在MemoryStream中是因为图形无法使用索引像素格式图像。 主要问题是垃圾回收器在这里做了什么? 我没有留在内存中的对象。
----------编辑----------------
异常是:
System.OutOfMemoryException was unhandled
Message=Out of memory.
Source=System.Drawing
StackTrace:
at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height)
at System.Drawing.Bitmap..ctor(Image original, Int32 width, Int32 height)
at System.Drawing.Bitmap..ctor(Image original)
at WindowsFormsApplication1.Form1.buttonrun1_Click(Object sender, EventArgs e) in C:\Users\hamidp\Desktop\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 115
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at WindowsFormsApplication1.Program.Main() in C:\Users\hamidp\Desktop\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 17
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
在第 using (Bitmap T = new Bitmap(new Bitmap(Image.FromStream(ms))))
行抛出异常。
-------------------编辑-----------------------
我还在 while ((line = sr.ReadLine()) != null)
后面添加了一行 GC.Collect();
,但是同样的错误再次发生。