为什么我得到OutOfMemoryException?

时间:2014-09-30 07:14:58

标签: c# .net winforms

我有这种方法我每隔X秒从一个计时器滴答事件中调用该方法。 在最初的几秒钟内没有问题,但是在20秒后它显示异常。

private void ReturnTexture(Texture texture_take, List<Point> lstPnt, float[] anglArr, float angle)
        {
            int i, j, bytes = 2048 * 512, stride = 2048;
            GraphicsStream textureStream;
            byte[] rgbValues = new byte[bytes];

            //sets texture to complete transparent
            GCHandle gch = GCHandle.Alloc(rgbValues, GCHandleType.Pinned);
            MemSet(gch.AddrOfPinnedObject(), 0x0, rgbValues.Length);

            //add 90 degrees because the cone starts at 90
            angle += 90F + 23.75F;

            //checks all points and draws yellow only those how are inside the cone
            if (lstPnt != null)
            {
                for (i = 0; i < lstPnt.Count - 1; i++)
                {
                    if (anglArr[i] <= angle && anglArr[i] >= angle - 47.5F) //if point angle is inside cone. Cone angle is 47.5 degrees
                    {
                        j = lstPnt[i].Y * stride + lstPnt[i].X * 4;

                        //yellow
                        rgbValues[j + 0] = (byte)0;
                        rgbValues[j + 1] = (byte)255;
                        rgbValues[j + 2] = (byte)255;
                        rgbValues[j + 3] = (byte)255;
                    }
                    else
                    {

                    }
                }
            }

例外是在线:

byte[] rgbValues = new byte[bytes];

当异常发生时,我看到变量bytes包含:1048576 并且rgbValues为空

我在异常发生之前使用了一个断点,并看到以字节为单位的值是相同的:1048576但是异常仅在20秒左右发生,而不仅仅发生在开头。

未处理的类型&#39; System.OutOfMemoryException&#39;发生在Weather Radar.exe

System.OutOfMemoryException was unhandled
  HResult=-2147024882
  Message=Exception of type 'System.OutOfMemoryException' was thrown.
  Source=Weather Radar
  StackTrace:
       at Weather_Radar.Form1.ReturnTexture(Texture texture_take, List`1 lstPnt, Single[] anglArr, Single angle) in d:\C-Sharp\Weather Radar\Weather Radar\Weather Radar\Form1.cs:line 314
       at Weather_Radar.Form1.timer1_Tick(Object sender, EventArgs e) in d:\C-Sharp\Weather Radar\Weather Radar\Weather Radar\Form1.cs:line 59
       at System.Windows.Forms.Timer.OnTick(EventArgs e)
       at System.Windows.Forms.Timer.TimerNativeWindow.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(IntPtr 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 Weather_Radar.Program.Main() in d:\C-Sharp\Weather Radar\Weather Radar\Weather Radar\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly 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.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

form1中的第314行是:byte [] rgbValues = new byte [bytes];

form1中的第59行是:ReturnTexture(scanningCloudsTexture,cloudPoints,angleArray,angleF);

这是计时器刻度事件代码:

static float angle_ = 0.0F;
        static float angleF = 0.0F;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (angleF > 360F)
            {
                angleF -= 360F;
            }

            ReturnTexture(scannedCloudsTexture, cloudPoints, angleArray, angleF);

            DisplayOnScreen(angle_);

            angle_ += (float)(Math.PI * 1d / 180d);
            angleF += 1.0F;
        }

1 个答案:

答案 0 :(得分:1)

正如Renuiz的评论中所解释的那样,看起来你没有释放为你的图像分配的内存。当您致电GCHandle.Alloc

  

保护对象免受垃圾回收的新GCHandle。这个GCHandle必须在不再需要时免费发布。

如果您没有Free GCHandle,则代码分配的1MB永远不会被释放。我认为有两种可能的选择:

  • 尽可能重用byte[],不需要更多内存。保留GCHandle,这样你就可以将它们设置为每个刻度为零。
  • 请勿使用byte[]GCHandle固定在内存中。无论如何,当您创建新的byte[] all values will be set to zero时。