Tuesday, November 11, 2008

How to test memory leaks??

Hi,I've been tasked with monitoring a Windows process developed in the .Net framework. I'm looking to monitor the memory for any consistent increase and also any degredation in CPU performance. I have some ideas below, but would really appreciate your insights before I go ahead!
Ideas from:http://msdn.microsoft.com/msdnmag/issues/07/01/ManagedLeaks/#void
Is it a memory leak?--------------------
Note that constantly increasing memory usage is not necessarily evidence of a memory leak. Some applications will store ever increasing amounts of information in memory (e.g. as a cache). If the cache can grow so large as to cause problems, this may be a programming or design error, but is not a memory leak as the information remains nominally in use. In other cases, programs may require an unreasonably large amount of memory because the programmer has assumed memory is always sufficient for a particular task; for example, a graphics file processor might start by reading the entire contents of an image file and storing it all into memory, something that is not viable where a very large image exceeds available memory.
To put it another way, a memory leak arises from a particular kind of programming error, and without access to the program code, someone seeing symptoms can only guess that there might be a memory leak. It would be better to use terms such as "constantly increasing memory use" where no such inside knowledge exists.
The term "memory leak" is evocative and non-programmers especially can become so attached to the term as to use it for completely unrelated memory issues such as buffer overrun.
Checking for Leaks------------------
There are a number of telltale signs that an application is leaking memory.• Maybe it's throwing an OutOfMemoryException.• Maybe its responsiveness is growing very sluggish because it started swapping virtual memory to disk.• Maybe memory use is gradually (or not so gradually) increasing in Task Manager.
When a memory leak is suspected, you must first determine what kind of memory is leaking, as that will allow you to focus your debugging efforts in the correct area.
Use PerfMon to examine the following performance counters for the application:
• Process/Private BytesThe Process/Private Bytes counter reports all memory that is exclusively allocated for a process and can't be shared with other processes on the system.
Test: If Process/Private Bytes is increasing, but # Bytes in All Heaps remains stable, unmanaged memory is leaking.
• .NET CLR LocksAndThreads/# of current logical Threads.The .NET CLR LocksAndThreads/# of current logical Threads counter reports the number of logical threads in an AppDomain.
Test:If an application's logical thread count is increasing unexpectedly, thread stacks are leaking.
Test:If both counters for 'logical thread count' and 'Private Bytes' are increasing, memory in the managed heaps is building up.
• .NET CLR Memory/# Bytes in All HeapsThe .NET CLR Memory/# Bytes in All Heaps counter reports the combined total size of the Gen0, Gen1, Gen2, and large object heaps.
Test:By default, the stack size on modern desktop and server versions of Windows® is 1MB. So if an application's Process/Private Bytes is periodically jumping in 1MB increments with a corresponding increase in .NET CLR LocksAndThreads/# of current logical Threads, a thread stack leak is very likely the culprit.
Test:If total memory use is increasing, but counters for 'logical thread count' and 'Private Bytes' (measuring managed heap memory) are not increasing, there is a leak in the unmanag

0 comments: