4

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?

barlop
  • 12,887
  • 8
  • 80
  • 109
  • 2
    http://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface/538238#538238 – Jesse C. Slicer Dec 19 '15 at 18:51
  • looks like an 'int' isn't garbage collected, but some other memory management deals with it after the procedure its declared in completes. Garbage collection runs sometimes though perhaps not when a procedure completes.. and it applies i think when there's a pointer or reference so in C, when you use malloc.. you'd free it that's garbage collection but you wouldn't do that for an 'int;. Thte IDisposable objects may be some distinct kind of thing needing some distinct form of garbage collection. – barlop Dec 19 '15 at 21:14

4 Answers4

2

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.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
1

There are two different concepts here.

  1. Disposing
  2. 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?

Community
  • 1
  • 1
Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
  • won't GC run when a procedure finishes and GC its variables? I figure an advantage of local variables is they only exist for the life of the procedure and thus less memory is used. – barlop Dec 19 '15 at 18:21
  • No it will not. The variable yes but the memory allocated to the object is living there in memory. There is difference between the variable referencing an object and the object itself in memory. – Hamid Pourjam Dec 19 '15 at 18:23
  • what about `int a` will the memory also not clear after the procedure finishes? why don't we free that manually too? – barlop Dec 19 '15 at 18:24
  • `int` is not a reference variable. It is a value type. The memory used by a local variable of type `int` will be cleared after it leaves the scope(method finished) – Hamid Pourjam Dec 19 '15 at 18:25
  • is that garbage collection on int then when the procedure finishes? – barlop Dec 19 '15 at 18:27
  • No it is not garbage collection. It is because local variables are created on something like stack and after leaving the method they will destroyed. (this is just a simple explanation of a very complicated process) – Hamid Pourjam Dec 19 '15 at 18:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98432/discussion-between-barlop-and-dotctor). – barlop Dec 19 '15 at 18:30
1

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.

Ian
  • 30,182
  • 19
  • 69
  • 107
1

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 ;-)

elnigno
  • 1,751
  • 14
  • 37