Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are ways to solve Memory Leaks in C#

I'm learning C#. From what I know, you have to set things up correctly to have the garbage collector actually delete everything as it should be. I'm looking for wisdom learned over the years from you, the intelligent.

I'm coming from a C++ background and am VERY used to code-smells and development patterns. I want to learn what code-smells are like in C#. Give me advice!

What are the best ways to get things deleted?

How can you figure out when you have "memory leaks"?


Edit: I am trying to develop a punch-list of "stuff to always do for memory management"


Thanks, so much.

like image 700
Jeremiah Avatar asked Mar 27 '09 14:03

Jeremiah


People also ask

What causes memory leaks C?

Memory leaks occur when new memory is allocated dynamically and never deallocated. In C programs, new memory is allocated by the malloc or calloc functions, and deallocated by the free function.

What is memory leaks in C?

The memory leak occurs, when a piece of memory which was previously allocated by the programmer. Then it is not deallocated properly by programmer. That memory is no longer in use by the program. So that place is reserved for no reason. That's why this is called the memory leak.

What is a memory leak and how do you prevent it?

Memory leak occurs when programmers create a memory in heap and forget to delete it. The consequences of memory leak is that it reduces the performance of the computer by reducing the amount of available memory.


3 Answers

C#, the .NET Framework uses Managed Memory and everything (but allocated unmanaged resources) is garbage collected.

It is safe to assume that managed types are always garbage collected. That includes arrays, classes and structures. Feel free to do int[] stuff = new int[32]; and forget about it.

If you open a file, database connection, or any other unmanaged resource in a class, implement the IDisposable interface and in your Dispose method de-allocate the unmanaged resource.

Any class which implements IDisposable should be explicitly closed, or used in a (I think cool) Using block like;

using (StreamReader reader = new StreamReader("myfile.txt"))
{
   ... your code here
}

Here .NET will dispose reader when out of the { } scope.

like image 150
Dead account Avatar answered Oct 07 '22 18:10

Dead account


The first thing with GC is that it is non-deterministic; if you want a resource cleaned up promptly, implement IDisposable and use using; that doesn't collect the managed memory, but can help a lot with unmanaged resources and onward chains.

In particular, things to watch out for:

  • lots of pinning (places a lot of restrictions on what the GC can do)
  • lots of finalizers (you don't usually need them; slows down GC)
  • static events - easy way to keep a lot of large object graphs alive ;-p
  • events on an inexpensive long-life object, that can see an expensive object that should have been cleaned up
  • "captured variables" accidentally keeping graphs alive

For investigating memory leaks... "SOS" is one of the easiest routes; you can use SOS to find all instances of a type, and what can see it, etc.

like image 32
Marc Gravell Avatar answered Oct 07 '22 19:10

Marc Gravell


In general, the less you worry about memory allocation in C#, the better off you are. I would leave it to a profiler to tell me when I'm having issues with collection.

You can't create memory leaks in C# in the same way as you do in C++. The garbage collector will always "have your back". What you can do is create objects and hold references to them even though you never use them. That's a code smell to look out for.

Other than that:

  • Have some notion of how frequently collection will occur (for performance reasons)
  • Don't hold references to objects longer than you need
  • Dispose of objects that implement IDisposable as soon as you're done with them (use the using syntax)
  • Properly implement the IDisposable interface
like image 34
Michael Meadows Avatar answered Oct 07 '22 17:10

Michael Meadows