虽然我实现了IDisposable,但GC不会调用我的Dispose-Method [英] GC dont call my Dispose-Method although I implemented IDisposable

查看:60
本文介绍了虽然我实现了IDisposable,但GC不会调用我的Dispose-Method的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有这个问题。我使用了IDisposable-Interface实现了

的CRootItem。我做了一个测试功能来测试我的

Dispose-Method ....但是当我在Dispose-Method中设置一个断点并调用

时,GC没有发生任何事情! !我的Dispose方法从未被称为!!所以GC

不要调用我的Dispose-Method虽然我实现了IDisposable?


我做错了什么?


问候,Jazper


---代码----------------------------- ----------------------------------

private void Test()

{

for(int i = 0; i< 50000; i ++)

{

CEmployee oEmployee = new CEmployee ();

}

GC.Collect();

}


公共类CEmployee :CRootItem,IDisposable

{

protected OleDbDataAdapter _Adapter;

protected DataTable _Data;


public void Dispose()

{

this._Adapter.Dispose(); // BRAKPOINT在这里!!

this._Data.Dispose();

}


public CEmployee()

{

this._Adapter = new OleDbDataAdapter();

this._Data = new DataTable();

}

}

hi

i have this problem. i made a class deverted by CRootItem with
implementation of IDisposable-Interface. i made a test-funktion to test my
Dispose-Method.... but when set a breakpoint in my Dispose-Method and call
the GC nothing happend!!! my Disposemethod has never been called!! so the GC
dont call my Dispose-Method although I implemented IDisposable?

what am i doing wrong?

Regards, Jazper

---Code ---------------------------------------------------------------
private void Test()
{
for(int i=0; i < 50000; i++)
{
CEmployee oEmployee = new CEmployee();
}
GC.Collect();
}

public class CEmployee : CRootItem, IDisposable
{
protected OleDbDataAdapter _Adapter;
protected DataTable _Data;

public void Dispose()
{
this._Adapter.Dispose(); //BRAKPOINT HERE!!
this._Data.Dispose();
}

public CEmployee()
{
this._Adapter = new OleDbDataAdapter();
this._Data = new DataTable();
}
}

推荐答案

我听说不可能预测真正的垃圾什么时候

即使在调用GC.Collect()时也会发生收集操作...

但是我现在还不知道。


关于这个主题的MVP评论会很明显:​​=)

Cyber​​tof。

文章< e#************** @ TK2MSFTNGP09.phx.gbl>, j.***@bluewin.ch

说......
I have heard that it is not possible to predict when the real Garbage
Collection operation will occur, even when calling GC.Collect()...
But I don''t know anymore about it for now.

A MVP comment on this subject would be appreciable :=)
Cybertof.
In article <e#**************@TK2MSFTNGP09.phx.gbl>, j.***@bluewin.ch
says...


我有这个问题。我通过实现IDisposable-Interface创建了一个由CRootItem摧毁的类。我做了一个测试功能来测试我的
Dispose-Method ....但是当我在Dispose-Method中设置一个断点并调用
GC没有任何事情发生!我的Dispose方法从未被称为!!所以GC没有调用我的Dispose-Method虽然我实现了IDisposable?

我做错了什么?

问候,Jazper
--- Code --------------------------------------------- ------------------
private void Test()
{
for(int i = 0; i< 50000; i ++)
{
CEmployee oEmployee = new CEmployee();
}
GC.Collect();
}
公共类CEmployee:CRootItem ,IDisposable
{
受保护的OleDbDataAdapter _Adapter;
受保护的DataTable _Data;

public void Dispose()
{
this._Adapter.Dispose (); // BRAKPOINT在这里!!
this._Data.Dispose();
}
公共CEmployee()
{
this._Adapter = new OleDbDataAdapter ();
this._Data = new DataTable();
}
}
hi

i have this problem. i made a class deverted by CRootItem with
implementation of IDisposable-Interface. i made a test-funktion to test my
Dispose-Method.... but when set a breakpoint in my Dispose-Method and call
the GC nothing happend!!! my Disposemethod has never been called!! so the GC
dont call my Dispose-Method although I implemented IDisposable?

what am i doing wrong?

Regards, Jazper

---Code ---------------------------------------------------------------
private void Test()
{
for(int i=0; i < 50000; i++)
{
CEmployee oEmployee = new CEmployee();
}
GC.Collect();
}

public class CEmployee : CRootItem, IDisposable
{
protected OleDbDataAdapter _Adapter;
protected DataTable _Data;

public void Dispose()
{
this._Adapter.Dispose(); //BRAKPOINT HERE!!
this._Data.Dispose();
}

public CEmployee()
{
this._Adapter = new OleDbDataAdapter();
this._Data = new DataTable();
}
}



很容易预测.NET GC。


当行星发生碰撞时它会收集垃圾,布什和托尼布莱尔告诉

真相

" Cyber​​tof" < CY **************** @ gmx.net>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...
Its easy to predict the .NET GC.

It will garbage collect when planets collide and Bush and Tony Blair tells
the truth

"Cybertof" <cy****************@gmx.net> wrote in message
news:MP************************@msnews.microsoft.c om...
我听说无法预测真正的Garbage
收集操作何时会发生,即使调用GC.Collect()......
但我不是现在已经知道了。

关于这个主题的MVP评论会很明显:​​=)

Cyber​​tof。

文章< e# **************@TK2MSFTNGP09.phx.gbl> ;, j。*** @ bluewin.ch
说...
I have heard that it is not possible to predict when the real Garbage
Collection operation will occur, even when calling GC.Collect()...
But I don''t know anymore about it for now.

A MVP comment on this subject would be appreciable :=)
Cybertof.
In article <e#**************@TK2MSFTNGP09.phx.gbl>, j.***@bluewin.ch
says...


我有这个问题。我通过实现IDisposable-Interface创建了一个由CRootItem摧毁的类。我做了一个测试功能来测试
我的Dispose-Method ....但是当我在Dispose-Method中设置断点并且
调用GC时没有发生任何事情!我的Dispose方法从未被称为!!所以
GC不会调用我的Dispose-Method虽然我实现了IDisposable?

我做错了什么?

问候,Jazper
--- Code --------------------------------------------- ------------------
private void Test()
{
for(int i = 0; i< 50000; i ++)
{
CEmployee oEmployee = new CEmployee();
}
GC.Collect();
}
公共类CEmployee:CRootItem ,IDisposable
{
受保护的OleDbDataAdapter _Adapter;
受保护的DataTable _Data;

public void Dispose()
{
this._Adapter.Dispose (); // BRAKPOINT在这里!!
this._Data.Dispose();
}
公共CEmployee()
{
this._Adapter = new OleDbDataAdapter ();
this._Data = new DataTable();
}
}
hi

i have this problem. i made a class deverted by CRootItem with
implementation of IDisposable-Interface. i made a test-funktion to test my Dispose-Method.... but when set a breakpoint in my Dispose-Method and call the GC nothing happend!!! my Disposemethod has never been called!! so the GC dont call my Dispose-Method although I implemented IDisposable?

what am i doing wrong?

Regards, Jazper

---Code ---------------------------------------------------------------
private void Test()
{
for(int i=0; i < 50000; i++)
{
CEmployee oEmployee = new CEmployee();
}
GC.Collect();
}

public class CEmployee : CRootItem, IDisposable
{
protected OleDbDataAdapter _Adapter;
protected DataTable _Data;

public void Dispose()
{
this._Adapter.Dispose(); //BRAKPOINT HERE!!
this._Data.Dispose();
}

public CEmployee()
{
this._Adapter = new OleDbDataAdapter();
this._Data = new DataTable();
}
}



Jazper写道:
我有这个问题。我通过实现IDisposable-Interface创建了一个由CRootItem摧毁的类。我做了一个测试功能来测试我的Dispose-Method ....但是当我在我的
Dispose-Method中设置一个断点并且调用GC时没有发生任何事情!我的Disposemethod
从未被称为!!所以GC不要调用我的Dispose-Method
虽然我实现了IDisposable?
i have this problem. i made a class deverted by CRootItem with
implementation of IDisposable-Interface. i made a test-funktion to
test my Dispose-Method.... but when set a breakpoint in my
Dispose-Method and call the GC nothing happend!!! my Disposemethod
has never been called!! so the GC dont call my Dispose-Method
although I implemented IDisposable?




GC调用Finalize,而不是Dispose。决不。实现IDisposable和

通知GC你不希望GC调用Finalize(通过调用GC.SuppressFinalize()来调用
)意味着你遵守为你的对象的清理配置

模式。也就是说,您决定对象的客户端明确地进行对象

清理,该客户端必须调用

自行处理。为了缩短它,你只需说客户负责

触发对象的清理ASAP而不是GC。不过,GC仍然可以解决对象的内存问题。但是因为

对象的客户端已经调用了Dispose,所以在释放之前,对象将被清理很长时间




如果你调用GC.SuppressFinalize,你的对象将被垃圾收集

更快。


使用IDispose接口只是建议的方式这样做。

但是你可以只实现一个Cleanup方法并要求你的

对象的客户端在完成它时调用这个方法。 IDispose是

就是这样。


实现IDispose是为消耗资源的对象完成的,必须尽快释放
为了等待来电的结果,GC到Finalize的费用太高了。


-

Patrick Philippot - 微软MVP [.Net]

MainSoft咨询服务
www.mainsoft。 xx

(通过电子邮件回复时将.xx替换为.fr)



The GC calls Finalize, not Dispose. Never. Implementing IDisposable and
notifying the GC that you don''t want Finalize to be called by the GC (by
calling GC.SuppressFinalize()) means that you adhere to the Dispose
pattern for your object''s cleanup. That is, you decide that the object
cleanup is made explicitly by the object''s client, which must call
Dispose itself. To make it short, you just say "The client is in charge
of triggering the object''s cleanup ASAP instead of the GC". The GC will
still take care of freeing the object''s memory, though. But since the
object''s client has called Dispose, the object will be cleaned up long
before it is freed.

If you call GC.SuppressFinalize, your object will be "garbage collected"
faster.

Using the IDispose interface is just the recommended way of doing this.
But you could merely implement a Cleanup method and require that your
object''s clients call this method when they''re done with it. IDispose is
just that.

Implementing IDispose is done for objects consuming resources that must
be freed as soon as possible and for which waiting for the call made by
the GC to Finalize is too costly.

--
Patrick Philippot - Microsoft MVP [.Net]
MainSoft Consulting Services
www.mainsoft.xx
(replace .xx with .fr when replying by e-mail)


这篇关于虽然我实现了IDisposable,但GC不会调用我的Dispose-Method的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆