问题
Suppose I have a procedure for e.g. a button click.
And I create a Graphics object.
Apparently i'm supposed to dispose of it e.g.
using(Graphics gr__=this.CreateGraphics()) {
}
or with calling .Dispose() in the finally of a try-catch-finally.
But considering that the procedure is going to end pretty quickly.
Suppose I create it local to the procedure (not global, not in a using). But local to the procedure.
Then surely like any other variables, it will get disposed of automatically when the procedure completes, wouldn't it?
So why is it important for me to dispose it manually/explicitly?
Why can't I let it garbage collect automatically like any other variable?
Sure it may be perhaps a bit bigger than an 'int' but it might still be quite small and won't be in memory for long since the procedure ends so fast anyway. It may even be that straight after the using is finished or the Dispose() is called, the procedure ends and thus I suppose it'd be disposed, if the variable was local to the procedure. So why bother with the explicit garbage collection of Dispose()/using?
回答1:
The Microsoft .NET class library provides a managed interface for GDI+ via the System.Drawing namespace. GDI+ is C++-based and yields references to non-managed objects. It is therefore important to dispose the disposable objects of the System.Drawing namespace, because non-manged objects are not disposed automatically by the garbage collector. These objects might contain finalizers that do the job; however, you are not in the control of when these finalizers are executed. Also pending finalizers are cluttering the heap. It is therefore better to call Dispose() explicitly or by the means of a using-statement.
回答2:
Then surely like any other variables, it will get disposed of automatically when the procedure completes, wouldn't it?
Unfortunately, this may not be always the case for all System.Drawing classes (edit: read dotctor answer)
There are some other tricky classes like Bitmap where, though it is "managed", but the size is small. So, whenever it is late to be disposed, it will cause out of memory!
It is kind of better practice to dipose an unused object before memory leak occurs.
The details are more complicated though.
You can search more on unmanaged resource in System.Drawing to understand this more.
回答3:
There are two different concepts here.
- Disposing
- Garbage Collection
There is a lot of difference between those two concepts.
Although Graphics get disposed when collected by GC but why you should wait for GC to run and increase the memory footage of your application?
Here is the code for Graphics destructor.
~Graphics()
{
try
{
this.Dispose(false);
}
finally
{
// ISSUE: explicit finalizer call
// ISSUE: explicit non-virtual call
__nonvirtual (((object) this).Finalize());
}
}
Read more
Fundamentals of Garbage Collection
what is relation between GC, Finalize() and Dispose?
What and where are the stack and heap?
回答4:
In general you can't know when the object will be garbage collected, and thus disposed.
Before disposal, some objects will keep references to resources that will use your memory or inhibit some operations. So it's good to dispose these objects as soon as they are not of use anymore.
I was working on a bug where a temporary SQLite database file could not be deleted after being used, but only sometimes! After three frustrating days of research, I've found that the problem was a SQLite command object which was not timely disposed.
So use your usings ;-)
来源:https://stackoverflow.com/questions/34373828/why-dispose-an-object-which-will-surely-get-disposed-of-soon-regardless