What is a Memory Leak:
A Memory Leak occurs when memory is allocated in a program and is never returned to the operating system, even though the program does not use the memory any longer. The following are the four basic types of memory leaks:
- In a manually managed memory environment: Memory is dynamically allocated and referenced by a pointer. The pointer is erased before the memory is freed. After the pointer is erased, the memory can no longer be accessed and therefore cannot be freed.
- In a dynamically managed memory environment: Memory is disposed of but never collected, because a reference to the object is still active. Because a reference to the object is still active, the garbage collector never collects that memory. This can occur with a reference that is set by the system or the program.
- In a dynamically managed memory environment: The garbage collector can collect and free the memory but never returns it to the operating system. This occurs when the garbage collector cannot move the objects that are still in use to one portion of the memory and free the rest.
- In any memory environment: Poor memory management can result when many large objects are declared and never permitted to leave scope. As a result, memory is used and never freed.
When the amount of memory that a program is using continues to increase during the execution, this is a symptom of a memory leak. (You can watch this count of memory through a performance monitor.) The amount of memory that the program uses can eventually cause the program to run out of resources and to crash.
Common Causes of Memory Leaks in Managed Applications:
- GC Class
- Memory Performance Counters
- Automatic Memory Management
- Interoperating with Unmanaged Code
- Holding references to managed objects
- Failing to release unmanaged resources
- Failing to dispose Drawing objects
ref:
How to identify memory leaks in the common language runtime -
http://support.microsoft.com/kb/318263
Identify And Prevent Memory Leaks In Managed Code -
http://msdn.microsoft.com/en-us/magazine/cc163491.aspx
Three Common Causes of Memory Leaks in Managed Applications -
http://blogs.msdn.com/davidklinems/archive/2005/11/16/493580.aspx
CLR Profiler for the .NET Framework 2.0 download -
.Net Memory Profiler - http://memprofiler.com/
Redgate ANTS Performace Profiler - http://www.red-gate.com/products/ants_performance_profiler/index.htm
How To: Use CLR Profiler(allows developers to see the allocation profile of their managed applications) -
http://msdn.microsoft.com/en-us/library/ms979205.aspx
Tracing memory leaks in .NET applications with ANTS Profiler -
Tracing memory leaks in .NET applications with ANTS Profiler -
http://www.c-sharpcorner.com/Reviews/DisplayReview.aspx?ReviewID=41
Memory Leak Detection in .NET -
http://www.codeproject.com/KB/dotnet/Memory_Leak_Detection.aspx
Detecting .NET application memory leak - http://www.codeproject.com/KB/dotnet/BestPractices5.aspx
.NET Memory Leak: To dispose or not to dispose, that’s the 1 GB question –
Detecting .Net Memory Leaks -
Identifying Memory Leak With Process Explorer And Windbg –
Finding Memory Leaks In Silverlight With WinDbg -
http://davybrion.com/blog/2009/08/finding-memory-leaks-in-silverlight-with-windbg/
Troubleshooting ASP.NET using WinDbg and the SOS extension –
http://support.microsoft.com/kb/892277
Debugging Native Memory Leaks via LeakDiag -
(does its magic by using Detours technology to intercept memory allocators calls) -
ftp://ftp.microsoft.com/PSS/Tools/Developer%20Support%20Tools/LeakDiag/
The Debug Diagnostics 1.1 tool (is designed to help troubleshoot performance issues in any Win32 user-mode process) –