Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory Leaks C# [closed]

I am trying to understand the concept of memory leaks better. Can anyone point up some useful information that can help me better understand exactly what memory leaks are and how I would find them in my code.

like image 720
atrljoe Avatar asked Feb 16 '11 18:02

atrljoe


People also ask

What are memory leaks in C?

In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in such a way that memory which is no longer needed is not released. A memory leak may also happen when an object is stored in memory but cannot be accessed by the running code.

What is the main cause of memory leaks in 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.

How do you prevent memory leaks in C?

The only way to avoid memory leak is to manually free() all the memory allocated by you in the during the lifetime of your code. You can use tools such as valgrind to check for memory leaks. It will show all the memory that are not freed on termination of the program.

What is memory leak why it should be avoided in C?

Answer: Memory leak occurs when programmers create a memory in heap and forget to delete it. Memory leaks are particularly serious issues for programs like daemons and servers which by definition never terminate.


2 Answers

There are many kinds of memory leaks, but in general the term refer to some kind of resource that is no longer used, but still takes up memory. If you have many of those your application takes a lot of memory and eventually you run out of it.

In C#, these are some common memory leaks:

  • Not removing event listeners. Any event listener that is created with an anonymous method or lambda expression that references an outside object will keep those objects alive. Remember to remove event listeners when they are no longer used.
  • Keeping database connections or result sets open when they are not used. Remember to call Dispose() on all IDisposable objects. Use the using statement.
  • Call to C functions using p/Invoke which allocate memory which you then never release.
like image 73
Markus Johnsson Avatar answered Sep 24 '22 05:09

Markus Johnsson


A traditional memory leak happens when you allocate memory, and then somehow "forget" to return or deallocate it. In old C++ code, this means calling new without a corresponding delete. In C, it meant a call to alloc()/malloc() without a corresponding free().

In .Net, you don't get memory leaks in the traditional sense, because you aren't supposed to release memory yourself. There is no equivalent to free() or delete you need to use. Even IDisposable and finalizers are not about memory. Instead, you rely on the garbage collector (GC) to release memory for you.

However, this doesn't mean you'll never lose track of memory. There are several ways you might accidentally keep a reference around that prevents the garbage collector from doing it's job. These include global variables (especially lists, dictionaries, and other collection types that might be used to "cache" objects), event handlers hanging on to an object reference, recursive history references, and the large object heap.

It's important to also note here a pattern of increasing memory use in .Net doesn't necessarily mean your app is leaking memory. In cases of low overall memory pressure the garbage collector could instead be opting to save time by not collecting yet, or by collecting within the process's existing address space only without returning the memory to the operating system.

like image 22
Joel Coehoorn Avatar answered Sep 21 '22 05:09

Joel Coehoorn