使用垃圾收集的? [英] Use of Garbage Collection?

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

问题描述

我想知道当我们调用Dispose()方法执行什么操作。是Object迅速释放处置上的所有资源()调用或Dispose()标记的物体,准备进行垃圾回收。发生了什么事,当我们设置对象引用为NULL。其实我在.NET 2.0的Windows窗体应用程序。我想打电话给垃圾收集一定时间传递(例如5分钟后)后,收集所有未被引用的对象。

I want to know what action is performed when we call Dispose() method. Is Object frees all resources quickly on Dispose() call or Dispose() marks the Object is ready for garbage collection. And What happened when we set Object reference to NULL. Actually I have Windows form application in .NET 2.0. And I want to call garbage collector after certain time is passed(For Example after 5 mins) to collect all unreferenced Object.

推荐答案

没有什么神奇的有关Dispose方法,它只是像任何其他方法。调用Dispose方法不会做背景中的任何清理或改变对象的状态,它只是做你放什么方法。有什么特别的地方是它在IDisposable接口定义,所以它是告诉一个对象来清理它的资源的标准方法。

There is nothing magic about the Dispose method, it's just like any other method. Calling the Dispose method doesn't do any cleanup in the background or change the state of the object, it just does what you have put in the method. What is special about it is that it's defined in the IDisposable interface, so it's the standard way of telling an object to clean up it's resources.

在Dispose方法的对象应采取一切非托管资源的照顾,如数据库连接和字体的对象。

In the Dispose method the object should take care of all unmanaged resources, like database connections and Font objects.

当你释放一个对象,你不必理会任何托管资源。就像一个字节数组的结构完全是由垃圾收集处理,你可以把它留在物体,当你让它去。你并不需要设置为任何引用,当你不使用对象更多,垃圾收集器会找到最好的时间来删除它,任何对象它引用。

When you free an object you don't have to bother about any managed resources. A structure like a byte array is completely handled by the garbage collector, and you can just leave it in the object when you let it go. You don't need to set any references to null, when you don't use an object any more, the garbage collector will find the best time to remove it and any objects that it references.

垃圾收集器正常工作时,你最好不要管它,没有必要告诉它时,它应收集未使用的对象。它会找出自身时应该做的,通常它是比你更好,因为它获得了许多关于记忆的状态,你的代码没有机器的状态信息。

The garbage collector normally works best when you leave it alone, there is no need to tell it when it should collect unused object. It will figure out by itself when that should be done, and usually it does that better than you, as it has access to a lot of information about the state of the memory and the state of the machine that your code doesn't have.

您可能会觉得,你应该尽量保持内存使用较低,但没有这种优势本身。计算机无法工作得更好,因为它有更多的可用内存左侧。相反,如果你的代码试图做清理工作,或势力垃圾收集器做anyhting,它在做的时候它应该是忙于做更重要的东西清理。如果它需要的垃圾收集器将清理未使用的对象。

You might feel that you should try to keep the memory usage low, but there is no advantage of that in itself. A computer doesn't work better because it has more free memory left. On the contrary, if your code tries to do cleanup, or forces to garbage collector to do anyhting, it is doing the cleanup when it should be busy doing something more important. The garbage collector will clean up unused object if it's needed.

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

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