Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# memory allocation and deallocation patterns

Since C# uses Garbage Collection. When is it necessary to use .Dispose to free the memory?

I realize there are a few situations so I'll try to list the ones I can think of.

  1. If I close a Form that contains GUI type object, are those objects dereferenced and therefore will be collected?
  2. If I create a local object using new should I .Dispose of it before the method exits or just let the GC take care of it? What is good practice in this case?
  3. Are there any times in which forcing a GC is understandable?
  4. Are events collected by the GC when it's object is collected?
like image 276
Neal Avatar asked Apr 27 '10 00:04

Neal


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

Which language is C?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Why C is called mother of all languages?

C language is considered as the mother language of all the modern programming languages because most of the compilers, JVMs, Kernels, etc. are written in C language, and most of the programming languages follow C syntax, for example, C++, Java, C#, etc.


3 Answers

In theory, if you have properly defined componentry, it should never be required to call Dispose() on your objects, as the finalizer should eventually take care of it.

That being said, any time you're using an object that implements IDisposable, it's a good practice to call Dispose() on the object as soon as you are through working with it.

For some of your specific points:

1) If you know you're "done" with the Form, you can call Dispose() on it. This will force a cleanup at that point in time of the unmanaged resources associated with the form.

2) In this case: if your object is just used in that method, use "using" instead:

using (MyObject myObject = new MyObject())
{
   // use your object
} // It'll be disposed of here for you

3) There are rare reasons to do this, but in general, no.

4) Events are a delegate - the memory associated with the delegate will be collected after the delegate itself becomes unrooted, which typically happens when the objects in question are unrooted.

like image 128
Reed Copsey Avatar answered Oct 02 '22 23:10

Reed Copsey


You should call Dispose on every class that implements IDisposable. If it didn't need to be Dispose ed then it wouldn't implement IDisposable.

As for your other questions:

  1. When you add a control to the Form's Controls collection, then the control will be automatically disposed when the form is closed, so there's nothing you need to do there.
  2. If the object implements IDisposable, then you need to call Dispose. For example, if you go new FileStream(...), then the FileStream needs to be disposed, since it implements IDisposable. I would suggest you read up on the using construct in C# which makes it easier to handle IDisposable objects.
  3. Not really, 99.99% of the time, the garbage collector will know when the best time to run is. It's one of those, "you'll know when you need it" kind of situations.
  4. When the object containing the event is no longer referenced, then logically any object references contained in the event are also no longer referenced and will be available to be collected.
like image 38
Dean Harding Avatar answered Oct 02 '22 22:10

Dean Harding


Look at this question:

Is there a common practice how to make freeing memory for Garbage Collector easier in .NET?

If your class instantiates the IDisposable interface, that (probably) means that it has system resources that have to be disposed of directly. One easy way to accomplish that is to use the using keyword, as in:

using(var g = Graphics.FromBitmap(bmp))
{
    //Do some stuff with the graphics object
}

per @Matt S's answer in that question I referenced.

For your questions:

  1. If you instantiate an object that has IDisposable, you will need to dispose it when you close the form. That's tricky in WPF and straightforward in Winforms, since winforms dialogs have Dispose methods. For WPF, I've solved the problem by keeping the WPF class around but hidden, called a dispose method that disposes all objects (like serial ports), and then sets the WPF class to null.
  2. No. Let the GC take care of it.
  3. I think so, but I got dinged with negative votes :) When I've done very large allocations, forcing GC to remove them is, imo, a good idea.
  4. I'm not sure. I think that events are, in and of themselves, objects, so will get collected when no longer used.
like image 28
mmr Avatar answered Oct 02 '22 22:10

mmr