How to Test for Memory Leaks?

By grant.volker ·
Test for constantly increasing memory use

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:

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 Bytes
The 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.

If an application's logical thread count is increasing unexpectedly, thread stacks are leaking.

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 Heaps
The .NET CLR Memory/# Bytes in All Heaps counter reports the combined total size of the Gen0, Gen1, Gen2, and large object heaps.

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.

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

This conversation is currently closed to new comments.

6 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +


Collapse -
by sowjanya544

can we please continue providing more information on this

All Answers

Collapse -


by Tony Hopkinson In reply to How to Test for Memory Le ...

If you get an OOM exception it's almost certain you have some improperly managed unmanaged pointers.

Memory usage always going up is a leak in my book, the fact that you have a bucket under it is immaterial.

One of the other things to look for with the garbage collector is pinning.

By default the GC will free up memory and then shuffle up things still in use to maximise the free space. If you have exported the address of something on the heap, the GC will move it and bad things will happen. To get round this you can pin, however that will fragment the heap and going to town with it impact performance quite badly.

Only touched this subject in passing, but I do remember the GC can expose a lot of information of what it is doing and when.

Collapse -


by MastAvalons In reply to How to Test for Memory Le ...

I usually use deleaker for similary case.....

Collapse -


by Robert Miles In reply to How to Test for Memory Le ...

I need examples of working tests, maybe somebody have video or tutorial?

Collapse -

Reponse To Answer

by wizard57m-cnet Moderator In reply to test

Instead of tagging along on a long dead thread, you probably should start
a new Q&A, with an appropriate title. This one is nigh on 6 years old.
TR Moderator

Back to Networks Forum
6 total posts (Page 1 of 1)  

Hardware Forums