/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
if (disposing)
{
//手动释放图像资源
foreach (System.Windows.Forms.Control ctl in this.m_ControlCollection.Values)
{
ctl.Dispose();
}
this.m_ControlCollection.Clear();
//手动释放产品描述控件资源
//if (null != this.descBrowser)
//{
// this.descBrowser.Dispose();
// this.descBrowser = null;
//}
}
base.Dispose(disposing);
}上面的方法不起任何作用,内存不释放,怎么回事?附图 :类图
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
if (disposing)
{
//手动释放图像资源
foreach (System.Windows.Forms.Control ctl in this.m_ControlCollection.Values)
{
ctl.Dispose();
}
this.m_ControlCollection.Clear();
//手动释放产品描述控件资源
//if (null != this.descBrowser)
//{
// this.descBrowser.Dispose();
// this.descBrowser = null;
//}
}
base.Dispose(disposing);
}上面的方法不起任何作用,内存不释放,怎么回事?附图 :类图
而非托管资源是CLR不能控制或者管理的部分,这些资源有很多,比如文件流,数据库的连接,系统的窗口句柄,打印机资源等等这些资源一般情况下不存在于Heap(内存中用于存储对象实例的地方)中。
CLR的GC功能也只能释放托管资源 ,非托管资源要自己释放
还是由CLR来决定,它也主要是根据内存的使用压力来决定的~
//Force garbage collection.
GC.Collect();
//Wait for all finalizers to complete
GC.WaitForPendingFinalizers();
ls的兄弟已经解释的很清楚了
请参考http://www.cnblogs.com/sutengcn/archive/2008/06/22/1227539.html
.....gc.collect()回收的是CLR托管资源。
Dispose()释放的是非托管资源,例如一个窗口的Form.close()方法,其实是调用了Dispose(),释放的是例如显示设备资源之类的非托管资源。你可以自己试一试。
Form a = new Form();
a.Dispose();
GC.collect();
If(a == null) Console.WriteLine("a is null!")
else Console.WriteLine("a is not null!");
If(a.Isdispoesd) Console.WriteLine("a is disposed!")
//设置主窗体
private void SetControl(ItemKit.UICommon.BaseControl.CUserControlBase ctl)
{
if (null != ctl)
{
if (this.pnlMain.Controls.Count > 0)
{
//移除中文产品描述编辑器,获取产品表述信息
if (this.m_ControlCollection.ContainsKey(KeyProductCNDesc)
&& this.pnlMain.Contains(this.ctlProductCNDesc))
{
this.ctlProductCNDesc.SetDescriptionToDataModel();
} //移除英文产品描述编辑器,获取产品表述信息
if (this.m_ControlCollection.ContainsKey(KeyProductENDesc)
&& this.pnlMain.Contains(this.ctlProductENDesc))
{
this.ctlProductCNDesc.SetDescriptionToDataModel();
} ((ProductBase)this.pnlMain.Controls[0]).EndEdit();
this.pnlMain.Controls.Clear();
this.pnlMain.Controls.Add(ctl);
////第二次加载刷新控件
ctl.RefreshControlData();
}
else
{
this.pnlMain.Controls.Add(ctl);
} if (ctl.Dock != DockStyle.Fill)
{
ctl.Dock = DockStyle.Fill;
}
}
}
//点击label事件
private void lblProductCNDescription_Click(object sender, EventArgs e)
{
if (this.m_CurrentLeftMenuLabel != this.lblProductCNDescription)
{
this.SetCurrentLabel(this.lblProductCNDescription); this.SetControl(this.ctlProductCNDesc);
}
}//私有属性
private ProductCNDesc ctlProductCNDesc
{
get
{
ProductCNDesc ctlBase = null;
if (!this.m_ControlCollection.ContainsKey(KeyProductCNDesc))
{
ctlBase = new ProductCNDesc(new Uri(DescUrl), cnDesc);
ctlBase.BackColor = System.Drawing.SystemColors.Control;
ctlBase.IBusiness = base.IBusiness;
ctlBase.Security = base.Security;
ctlBase.pageParams = base.pageParams; this.m_ControlCollection[KeyProductCNDesc] = ctlBase;
}
else
{
ctlBase = (ProductCNDesc)this.m_ControlCollection[KeyProductCNDesc];
} return ctlBase;
}
}这是m_controlcollection中一个控件的周期
Dispose()方法应该是释放对象内部占用的资源,对象本身如果还被某处引用的话,那么它本身占用的资源是不会释放的。
我感觉对象内部的资源类似托管资源,而对象本身可以理解为非托管资源,所以释放非托管资源需要确保任何途径都无法再次访问到它了才行。