是否需要处理本地刷变量? [英] Is disposing a local brush variable necessary?

查看:76
本文介绍了是否需要处理本地刷变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSDN建议在释放最后一个引用之前处理System.Drawing.Brush类型的任何变量。否则,在垃圾收集器调用Brush对象的Finalize方法之前,它所使用的资源不会被释放。



我们知道,当控件自动销毁时,局部变量会被自动销毁流程超出了它所属方法的范围。那么,如果它是本地的,每次都需要处理一个画笔对象吗?

MSDN recommends disposing any variable of type System.Drawing.Brush before its last reference is released. Otherwise, the resources it is using will not be freed until the garbage collector calls the Brush object's Finalize method.

As we know, local variables are destroyed automatically when the control flow goes out of the scope of the method it belongs. So, is it necessary to dispose a brush object every time if it is local?

推荐答案

是。

项目不是自动销毁 当他们超出范围时 - 他们只是无法使用。使用 new 关键字在堆上创建的实际对象仍保留在堆上,并占用系统资源(刷柄)以及其余部分的内存。刷类。在调用Dispose方法之前,不会销毁堆对象 - 如果不这样做,它会等待垃圾收集器被踢入并将其作为未引用的变量销毁。这可能是几分钟,几小时甚至几年。



如果一个类实现了IDisposable,那么当你不再需要它们时,你应该总是处理它们调用自己处理,或者使用块将创建和用法包含在中。



一个常见的错误不要为基于文件的Stream执行此操作:这意味着它连接的文件保持打开状态,直到垃圾收集器将其释放 - 并且您自己的程序无法再次打开它,因为它已经对文件具有独占锁定!
Yes.
Items are not "destroyed automatically" when they go out of scope - they are just unavailable for use. The actual object you created on the heap with the new keyword remains on the heap, and uses up system resources (the brush handle) as well as the memory for the rest of the brush class. The heap object is not destroyed until the Dispose method is called - and if you don't do it then it waits around until the garbage collector is kicked in and destroys it as an unreferenced variable. This could be minutes, hours, or even years later.

If a class implements IDisposable, then you should always Dispose of instances when you no longer need them, either by calling Dispose yourself, or by enclosing the create and usage in a using block.

One of the frequent mistakes is not to do this for a file based Stream: which means that the file it is connected to remains open until the garbage collector frees it up - and your own program can't open it again as it already has an exclusive lock on the file!


答案肯定是肯定的,当你完成它们时,你应该对它们进行处理。



。当方法超出范围时,NET垃圾收集器不会立即清理对象。收集器(在4.5之前的版本中)在单独的线程上运行,并且仅在系统进行垃圾收集时才到达它。在4之前的版本中,垃圾收集器在清理垃圾时阻塞主线程。



这意味着您可以调用足够的代码而不是垃圾邮件实际上足够快地释放GDI资源,并且当垃圾收集器确定这些引用是否真正被使用时(垃圾收集器是引用计数系统),您将耗尽GDI资源或使应用程序变慢。



所以是的,重要的是在完成后需要释放资源的对象上调用Dispose。这适用于所有实现IDisposable的对象,而不仅仅是GDI对象。
The answer is most definately yes, you should call dispose on them when you are done with them.

The .NET garbage collector doesn't get around to cleaning up objects immediately when the method goes out of scope. The collector (in versions prior to 4.5) runs on a separate thread and only gets to it when the system does garbage collection. In versions prior to 4, the garbage collector blocks the main thread while it cleans up the trash.

This means that you could be calling enough code that you aren't actually freeing the GDI resources fast enough, and you will run out of GDI resources or make your application slower as the garbage collector determines if those references are really used or not (the garbage collector is a reference counting system).

So yes, it is important to call Dispose on an object that needs to free resources when you are done with it. This is true for all objects that implement IDisposable, not just GDI ones.


这篇关于是否需要处理本地刷变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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