Wednesday, January 20, 2010

.net application memory leak

Memory leaks grow slowly and after sometime they bring down the server by consuming huge chunks of memory. Maximum time people reboot the system, make it work temporarily but after some time again your server is down.

Task Manager - Can this detect memory leak ?

Task manager shows working set memory and not the actual memory used, ok so what does that mean. This memory is the allocated memory and not the used memory. Adding further some memory from the working set can be shared by other processes / application.


Private bytes are those memory areas which are not shared by other application.

Below are the steps we need to follow to track private bytes in an application using performance counters:-
  • Start you application which has memory leak and keep it running.
  • Click start à Goto run and type ‘perfmon’.
  • Delete all the current performance counters by selecting the counter and deleting the same by hitting the delete button.
  • Right click à select ‘Add counters’ à select ‘process’ from performance object.
  • From the counter list select ‘Private bytes’.
  • From the instance list select the application which you want to test memory leak for.
If you application shows a steady increase in private bytes value that means we have a memory leak issue here.


  • What: - We will first try to investigate what is the type of memory leak, is it a managed memory leak or an unmanaged memory leak.
  • How: - What is really causing the memory leak. Is it the connection object, some kind of file who handle is not closed etc?
  • Where: - Which function / routine or logic is causing the memory leak.
.NET application have two types of memory managed memory and unmanaged memory. Managed memory is controlled by garbage collection while unmanaged memory is outside of garbage collectors boundary.

Private bytes are the total memory consumed by the application. Bytes in all heaps are the memory consumed by the managed code.

Un Managed memory + Bytes in all helps = private bytes

  • If the private bytes increase and bytes in all heaps remain constant that means it’s an unmanaged memory leak.
  • If the bytes in all heaps increase linearly that means it’s a managed memory leak.

Finalizers are responsible only for their own resources, while Dispose() also deals with referenced resources.

GC.SuppressFinalize() so that the garbage collector won’t bother to finalize this object.

 





No comments: