处理掉这个对象就足够了吗? 或者我需要做更多的事情吗?
我有这段代码:-
using (System.Security.Cryptography.SHA256 sha2 =
new System.Security.Cryptography.SHA256Managed())
{ .. }
我是否需要在离开该处置范围之前放置这行代码..或者已经进行处置“调用”。
sha2.Clear();
I have this code :-
using (System.Security.Cryptography.SHA256 sha2 =
new System.Security.Cryptography.SHA256Managed())
{ .. }
Do I need to put this line of code, just BEFORE I leave that dispose scope .. or does the dispose 'call' that already.
sha2.Clear();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于据我所知,Clear() 方法仅调用 Dispose,因此 using 块应该足以确保释放所使用的资源。
Since AFAIK the Clear() method just calls Dispose, the using block should be enough to ensure that the resources used are released.
恕我直言,如果调用 Dispose() 不足以处置对象,那么要么代码中存在严重错误,要么设计中存在严重缺陷。 因此,不必担心在您自己的代码中采取任何其他步骤!
IMHO if calling Dispose() is not enough to dispose off an object then either there is a serious bug in the code or a serious flaw in the design. So don't worry about taking any additional steps in your own code!
如果您使用 Reflector 看一下,您会发现
Clear
仅调用Dispose
,因此无需在示例中调用Clear
。许多框架类为
Dispose
提供了Close
/Clear
/Whatever 覆盖,以使使用更加简单。If you take a look using Reflector, you'll see that
Clear
just callsDispose
, so there's no need to callClear
in your example.Many of the framework classes offer a
Close
/Clear
/Whatever cover forDispose
to make the usage a little more straightforward.还有一个通用的有用提示 - 不要忘记现在所有这些东西的来源都是可用的 - 它经常帮助我回答这类问题,而不必猜测或推断。
这是一个很好的起点:http://www.codeplex.com/NetMassDownloader
And a generic helpful hint - don't forget at that the source for all this stuff is available nowadays - it often helps me answer this sort of question, without having to guess or infer.
This is a good place to start: http://www.codeplex.com/NetMassDownloader
Dispose() 已经足够好了。
我不确定 .NET 是如何工作的。 但是附加函数调用或“set null”会降低Java的性能。
CLR/Java VM 将(并且必须)能够在下一次垃圾收集中从“根”清除所有取消引用的托管对象。
附言。
Dispose() 会清理“非托管”资源,以提高 GC 性能,因为它不等待 Finalllizer 线程完成。
Dispose() is good enough.
I am not sure how .NET works. But addition function call or "set null" will degrade the performance in Java.
The CLR/Java VM will(and must) able to cleanup all dereferenced managed object from "roots" in the next garbage collection.
PS.
Dispose() does cleanup "unmanaged" resources, to improve the GC performance as it don't wait for the Finallizer thread to complete.