C# 内存释放


DestImage.Dispose();
DestImage = null;
HOperatorSet.GenImageInterleaved(out DestImage, pBufferColorImage, "bgr",
ImageWidth, ImageHeight, -1, "byte", 0, 0, 0, 0, -1, 0);
在C#中图像变量,在再次使用前,一定要先检查存在再释放,否则内存会急剧增长到GCC来释放,这时会引起一系列问题。我提供一个函数模板
/// <summary>
/// 释放当前对象,变量置为null
/// </summary>
public static void FreeObject<T>(ref T Obj) where T : class, IDisposable
{
if (Obj != null) Obj.Dispose();
Obj = null;
}

 

FreeObject<tDataTable>(ref mBoardTable);
FreeObject<BindingSource>(ref mBoardSource);
FreeObject<tDataTable>(ref mProgTable);
FreeObject<BindingSource>(ref mProgSource);
//
FreeObject<tDataTable>(ref mUpSideTable);
FreeObject<BindingSource>(ref mUpSideSource);
FreeObject<tDataTable>(ref mDownSideTable);
FreeObject<BindingSource>(ref mDownSideSource);
//
FreeObject<tDataTable>(ref mUpSideSubTable);
FreeObject<BindingSource>(ref mUpSideSubSource);
FreeObject<tDataTable>(ref mDownSideSubTable);
FreeObject<BindingSource>(ref mDownSideSubSource);
这样释放对象,代码少,代码可读性强。

C# 内存释放

上一篇:WPF将窗体设置为桌面背景【动态桌面】


下一篇:AcWing393 雇佣收银员(差分约束)