为什么需要处理图形?
为什么Graphics
需要被处置? 还有Pen
和SolidBrush
?
Why should Graphics
needs to be disposed?
also the Pen
and SolidBrush
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
为什么Graphics
需要被处置? 还有Pen
和SolidBrush
?
Why should Graphics
needs to be disposed?
also the Pen
and SolidBrush
?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
简单的答案是它们实现了“IDisposable”,因此需要将其丢弃。
稍微长一点的答案是它们消耗了需要释放的非托管资源。通过直接调用 dispose(或使用“using”语句),您可以释放这些资源,而不是等待 GC 为您执行此操作。
Well the simple answer is they implement 'IDisposable' so they need to be disposed.
The slightly longer answer is they consume unmanaged resources that need to be released. By calling dispose directly (or using a 'using' statement) you can release those resources rather than waiting for the GC to do so for you.
这些对象是非托管资源(特别是 GDI+ 对象)的包装器。如果这些对象没有显式释放,那么 GC 需要调用它们的终结器。这将延迟为另一个 GC 回收资源。*这意味着您的应用程序将使用比必要的更多的资源。
更新:澄清上面的措辞加上......
* 当 GC 遇到带有终结器的对象时 - 假设尚未在 Dispose() 方法中调用 GC.SuppressFinalize() - 它将终结器排队以在将来某个时间在终结器线程上运行。因此,对象使用的内存和资源至少会保留到当前运行的 GC 之后的 GC 为止。
These objects are wrappers around unmanaged resources, specifically GDI+ objects. If these objects are not explicitly disposed, then the GC needs to call their finalizer. This will delay reclaiming their resources for another GC.* It means that your application will use more resources than necessary.
UPDATE: Clarified wording above plus...
* When the GC encounters an object with a finalizer - assuming that GC.SuppressFinalize() hasn't been called in the Dispose() method - it queues the finalizer to run on the finalizer thread at some future time. The memory and resources used by the object are thus held until at least the GC following the currently running one.