为什么不析构? [英] Why not destructor?

查看:74
本文介绍了为什么不析构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说如果我添加一个构造函数就不好了,它会使

的东西复杂化并且最好使用Dispose。

任何人都可以解释这个对我来说?


-

问候

Sharon G.

I have heard that if I add a constructor it is not good, it complicates
things and that it is better to use the Dispose.
Can anybody explain this for me?

--
Regards
Sharon G.

推荐答案

Sharon< Sh **** @ discussion.microsoft.com>写道:
Sharon <Sh****@discussions.microsoft.com> wrote:
我听说如果我添加一个构造函数它不好,它会使事情变得复杂,并且最好使用Dispose。
任何人都可以解释这个我?
I have heard that if I add a constructor it is not good, it complicates
things and that it is better to use the Dispose.
Can anybody explain this for me?




我相信你的意思是析构函数,而不是构造函数。


终结者(你真正得到的)不是'确定地称为

- 当GC转向它时它们被调用。这个

可能比你真正想要的要晚得多,而且实际上它可能永远不会发生b $ b。如果你需要

释放资源以便其他东西可以使用它们,这是一个非常糟糕的情况。


终结者也需要更长的时间释放,因为GC必须注意到

没有任何东西引用该对象,运行终结器,然后实际上

在下次运行时回收内存。 />

-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要给我发邮件



I believe you mean destructor, not constructor.

Finalizers (which are what you actually get) aren''t called
deterministically - they''re called when the GC gets round to it. This
could be much later than you really want, and indeed it may never
happen at all. That''s a really bad situation to be in if you need to
free up resources so that something else can use them.

Finalizers also take longer to free up, as the GC has to notice that
nothing is referencing the object, run the finalizer, and then actually
reclaim the memory the following time it runs.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


" Sharon" <嘘**** @ discussions.microsoft.com>在消息中写道

新闻:20 ********************************** @ microsof t.com ...
"Sharon" <Sh****@discussions.microsoft.com> wrote in message
news:20**********************************@microsof t.com...
我听说如果我添加一个构造函数它不好,它会使事情变得复杂,并且最好使用Dispose。
Can有人为我解释这个吗?
I have heard that if I add a constructor it is not good, it complicates
things and that it is better to use the Dispose.
Can anybody explain this for me?




构造函数很好(你的消息)。析构函数(你的

主题)的问题是在.NET中你不知道它们什么时候执行,因为

对象在它们去的时候不会被破坏超出范围,但当垃圾

收藏家清理然后上去。调用Dispose时,Dispose模式可以保证执行




Tim

..NET优缺点 http://www.itwriting.com/phorum/list .php?f = 6


C#''析构函数'存在各种问题。


首先,你永远不知道何时或是否会被召唤。所以它不像C ++

析构函数,在对象超出范围或者删除
时调用它。在垃圾收集发生之前不会被调用。如果现在你的
系统不忙,它可能会在

析构函数运行前几个小时,即使该对象没有使用。

当您的应用程序退出时,.NET会尝试运行所有终结器(C#

析构函数实际上是伪装的终结器),但如果它需要超过

几秒钟,它就放弃了。因此,您永远无法确定析构函数是否会运行。


此外,您不能在析构函数中使用其他可终结对象。

(即其他具有析构函数的对象。)这是因为.NET没有对它运行终结器的顺序提供

保证。因此,您使用的任何对象已经由析构函数运行的

时间确定,这可能是非常可靠的。在析构函数

已经运行的对象上调用方法永远不是一个好主意。而且你不能确定

哪些已经完成(或者你使用的是哪些物品

甚至有一个终结器)它不是安全使用对象。


所以这一切都意味着析构函数实际上并没有太大的用处。

实现终结器的主要原因是因为你的对象包装了某种原始句柄(例如一个原始的Win32句柄,或者可能是通过Interop从一些非托管库中获得的句柄来获得
)并且你需要确保它会释放
。但这只是一个安全网,而不是一个非常可靠的安全网,鉴于上述限制,

.

更糟糕的是,终结器会导致性能问题。系统必须更加努力地工作以跟踪可终结的对象。尝试编写一个循环,看看你有多快可以创建没有析构函数的对象,以及使用

析构函数创建对象的速度有多快 - 就像这样:


int cout = 0;

while(true)

{

new MyClass();

count + = 1;

if(count%10000000 == 0)Console.WriteLine(count);

}


如果MyClass没有析构函数,它会在我的系统上创建大约2000万个对象,然后是
秒。如果它确实有一个析构函数,它只能管理大约2亿美元!


此外,终结器会导致对象在内存中停留更长时间

比他们原来要多,这会增加流程的工作集。


所以最好避免使用它们。

-

Ian Griffiths - http://www.interact-sw .co.uk / iangblog /

DevelopMentor - http: //www.develop.com/

" Sharon"写道:
There are various problems with the C# ''destructor''.

First, you never know when or if it will be called. So it''s not like a C++
destructor, where it gets called the moment the object goes out of scope or
is deleted. It won''t be called until a garbage collect occurs. If your
system is not busy right now it could literally be hours before the
destructor runs, even though the object is out of use.

When your application exits, .NET tries to run all finalizers (C#
destructors are really finalizers in disguise), but if it takes more than a
few seconds, it just gives up. So you can never be sure the destructor will
run.

Also, you can''t use other finalizable objects from within your destructor.
(I.e. other objects that have destructors.) This is because .NET makes no
guarantees about the order in which it runs finalizers. So it''s quite
possible that any objects you were using have already been finalized by the
time your destructor runs. Calling methods on an object whose destructor
has already been run is never a good idea. And since you can''t be sure
which ones have been finalized (or indeed which of the objects you are using
even have a finalizer) it''s not safe to use objects.

So all of that means that destructors aren''t actually a great deal of use.
The main reason for implementing a finalizer is because your object wraps
some kind of raw handle (e.g. a raw Win32 handle, or maybe a handle obtained
through Interop from some unmanaged library) and you need to ensure that it
gets freed. But this is just a safety net, and not a terribly reliable one,
given the limitations mentioned above.
Worse, finalizers cause performance issues. The system has to work harder
to track finalizable objects. Try writing a loop to see how fast you can
create objects without destructors and how fast you can create objects with
destructors - something like this:

int cout = 0;
while (true)
{
new MyClass();
count += 1;
if (count % 10000000 == 0) Console.WriteLine(count);
}

If MyClass does not have a destructor, it creates about 20 million objects a
second on my system. If it does have a destructor, it only manages about 2
million!

Also, finalizers cause objects to hang around in memory very much longer
than they otherwise would, which increases the working set of the process.

So they are best avoided.
--
Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
DevelopMentor - http://www.develop.com/

"Sharon" wrote:
我听说如果我添加一个构造函数它不好,它会使事情变得复杂,并且最好使用Dispose。
任何人都可以解释这个我?
I have heard that if I add a constructor it is not good, it complicates
things and that it is better to use the Dispose.
Can anybody explain this for me?



这篇关于为什么不析构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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