处理一个类对象 [英] Dispose a class object
本文介绍了处理一个类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个List< myclass> myList,其中包含Class Myclass类型的对象。
我需要从myList中删除一个条目,并希望从堆中删除内存分配。我实现了IDisposable和Dispose方法。但即使在调用dospose方法(Myclass.Dispose())后,我也可以访问该对象。
Hi,
I have a List<myclass>myList which contains object of type Class Myclass.
I need to remove an entry from myList and want to remove memory allocation from the heap. I implemented IDisposable and Dispose method. but even after calling dospose method (Myclass.Dispose()) I was able to access the object.
public void Dispose()
{
GC.SuppressFinalize(this);
}
请注意。谢谢!
Please advise. Thanks !
推荐答案
您根本无需做任何事情。 GC工作是一些对象变得无法访问。这在此解释:
http://en.wikipedia.org/wiki/Garbage_collection_ %28computer_science%29 [ ^ ] 。
这也解释了为什么你能够访问该对象。您的代码引用它的纯粹事实使得无法进行垃圾回收。为什么你认为调用GC.SuppressFinalize(this);
可能会清理内存?它恰恰相反...... :-)
你应该明白System.IDisposable
本身与垃圾无关采集。我知道你来自哪里:你在文档中读到了一些GC.SuppressFinalize
的技巧,你真的不明白:
http://msdn.microsoft.com/en-us/library/system.gc.suppressfinalize。 aspx [ ^ ]。
但谁告诉你应该使用类似的东西?这是最简单,最有效的建议:不要碰GC。
请查看我过去的答案:
Garbage collectotion负责所有内存管理 [ ^ ],
推迟循环中的变量会导致内存泄漏? [ ^ ],
< a href =http://www.codeproject.com/Answers/407516/Best-way-to-get-rid-of-a-public-static-List-Causin#answer1>最好的办法摆脱public static List Ca.使用内存不足 [ ^ ],
MDI表格中的内存管理 [ ^ ],
WPF数据绑定中的内存泄漏 [ ^ ]。
另外,请注意使用使用
语句和<的一些有趣示例code> System.IDisposeable 在我的文章中:
使用使用语句:DisposalAccumulator [ ^ ],
沙漏鼠标光标始终更改回其原始图像。怎么样? [ ^ ]。
实施 RAII 很好: http://en.wikipedia.org/wiki/RAII [ ^ ]。
请参阅此替代方案:沙漏鼠标光标始终更改回原始图像。怎么样? [ ^ ]。
-SA
You don't have to do anything at all. GC works is some object becomes unreachable. This is explained here:
http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29[^].
This also explains why you have been able to access the object. The mere fact that your code had a reference to it makes it impossible to be garbage collected. And why do you think callingGC.SuppressFinalize(this);
could possibly clean up memory? it does just the opposite… :-)
You should understand thatSystem.IDisposable
is itself unrelated to garbage collection. I know where you come from: you read about some technique in the documentation toGC.SuppressFinalize
, which you did not really understand:
http://msdn.microsoft.com/en-us/library/system.gc.suppressfinalize.aspx[^].
But who told you that you should use something like that? Here is the simplest and most effective advice: don't touch GC at all.
Please see my past answers:
Garbage collectotion takes care of all the memory management[^],
deferring varirable inside the loop can cuase memory leak?[^],
Best way to get rid of a public static List Causing an Out of Memory[^],
Memory management in MDI forms[^],
Memory leak in WPF DataBinding[^].
Also, pay attention for some interesting examples of using theusing
statements andSystem.IDisposeable
in my articles:
Using "using" Statements: DisposalAccumulator[^],
Hourglass Mouse Cursor Always Changes Back to its Original Image. How?[^].
It's good to implement RAII: http://en.wikipedia.org/wiki/RAII[^].
Please see this alternative: Hourglass Mouse Cursor Always Changes Back to its Original Image. How?[^].
—SA
这篇关于处理一个类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文