C# 为什么我';我要离开记忆异常?
我有一个方法,我从一个计时器滴答事件中每隔X秒调用一次这个方法。 在最初的几秒钟内没有问题,但在20秒钟后,它显示异常C# 为什么我';我要离开记忆异常?,c#,.net,winforms,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; Graphi
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
{
}
}
}
当异常发生时,我看到变量字节包含:1048576
并且rgbValue为null
在异常发生之前,我使用了一个断点,并看到以字节为单位的值是相同的:1048576,但异常仅在20秒左右发生,而不仅仅是在开始时
Weather Radar.exe中发生“System.OutOfMemoryException”类型的未处理异常
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:
表格1中的第314行是:byte[]rgbValues=新字节[bytes]
表格1中的第59行是:返回纹理(scannedCloudsTexture、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;
}
正如Renuiz在评论中所解释的,看起来您没有释放为图像分配的内存。当您调用
GCHandle.Alloc
一个新的GCHandle,用于保护对象不被垃圾回收。当不再需要此GCHandle时,必须免费释放它
如果您不释放GCHandle
,您的代码分配的1MB将永远不会释放。我认为有两种可能的选择:
- 尽可能重复使用
字节[]
,不需要更多内存。保留GCHandle
,这样您就可以将它们设置为0
- 不要使用
GCHandle
将字节[]
固定在内存中。无论如何,当您创建一个新的字节[]
时
GCHandle受垃圾收集器的保护,因此您应该调用GCHandle.free方法释放内存使用量,当不再需要该句柄时,我打赌RAM的每个计时器都会增加。尝试重用阵列,而不是在每个刻度上创建
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;
}