对一个对象垃圾收集,C# [英] Garbage Collection on one object, C#

查看:101
本文介绍了对一个对象垃圾收集,C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要处理的对象,因此它可以释放所有它所拥有的,但它并没有实现IDisposable,所以我可以用一个块不能使用它。我怎样才能让垃圾收集器收集它?

I need to dispose of an object so it can release everything it owns, but it doesn't implement the IDisposable so I can't use it in a using block. How can I make the garbage collector collect it?

推荐答案

您可以强制使用GC.Collect的集合()。要非常小心使用这个,因为一个完整的集合可能需要一些时间。最好的做法是,只让GC确定何时收集的最佳时间是。

You can force a collection with GC.Collect(). Be very careful using this, since a full collection can take some time. The best-practice is to just let the GC determine when the best time to collect is.

请问对象包含非托管资源,但没有实现IDisposable?如果是这样,这是一个错误。

Does the object contain unmanaged resources but does not implement IDisposable? If so, it's a bug.

如果没有,它不应该的问题,如果它被释放的时候了,垃圾收集器应该做的正确的事情。

If it doesn't, it shouldn't matter if it gets released right away, the garbage collector should do the right thing.

这篇关于对一个对象垃圾收集,C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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