Look at Memory Leaks - Classify and Identify

In my previous post I talked about type of Memory Pressures. Today I would like to talk about related subject – Memory Leaks.  Before you continue reading please make sure that you are familiar with how memory is managed on Windows.

 

Memory leaks are bugs that happen very often. I think it is very important to know how to approach the issue. Once you learned the process, these bugs are not as evil as everyone things.

 

When leaks occur, the first thing developer needs to do is to classify them. How many times you have heard people are just saying the application failed or became unresponsive due to memory leak? A phrase memory leak could mean leak of several memory resources.  Remember in my previous posts I talked about VAS, physical memory, page file and etc. An application can leak any of these resources. This means that memory leak is incomplete phrase that doesn’t have much meaning. One needs to be very specific when referring to a leak.

 

Leak Classification is very important. The table below represents memory leak type and tools that could be used to classify them. Keep in mind that there could be more tools and more leak types. I just enumerated the most common types that I dealt with in the past.

 

Memory Resource/Tool

Task Manager

PerfMon

WinDbg

WinDbg – Local Kernel Debugger

VADUMP

SQL Server

VAS

 

Process/Virtual Bytes

!vadump

!heap

!vad

 X

 

Physical Memory

Virtual Memory

Process/Private Bytes

!vadump

!heap

!memusage

!vm

 X

 

Page File

Commit Charge

Memory/Committed Bytes

 

!memusage

!vm

 X

 

AWE

 

 

 

 

 

Buffer Manager/Total pages

Handle

Handles

Objects

!handle

!handle

 

 

 

 

 

Once leak is classified, it needs to be identified. Identification of a leak involves a tracking of leaking stack. Once stack is known leak is identified. Keep in mind in many cases it is not sufficient to identify file line and number.  Below table represents tools that could be used to identify a leak:

 

 

Memory Resource/Tool

umdh/pageheap

LeakDiag

WinDbg

WinDbg – Local Kernel Debugger

VAS

 

Virtual Allocator

!heap -l

 

Physical Memory / Page File

Windows Heaps

Windows Heaps

CRT Heap

COM Heap

MDAC Heap

!vadump

!heap

!memusage

AWE

 

 

 

 

Handle

 

 

Break points on CreateEvent,

CreateMutex,

CloseHandle

!htrace

 

 

Usually classifying a leak is not hard and should be done very fast. You should be able to teach your testers, QA personal, to do it. Moreover the classsification process could be automated. Identifying a leak is usually harder and more time consuming process. It might involve multiple restarts, use of different tools and etc... Knowing how a tool works and being comfortable with it is the key. Getting there does require experience.  Once you are there, dealing with memory leaks becomes easier process. The only problem is that there will be always a leak that your tools can’t handleJ.  This is the case when you sit down and write your own LeakDiag.

 

There are several commercially available tools enabling you to classify and identify leaks. I haven’t looked at them for long time but from my past experience they don’t work as well as the ones I outlined in here. Before you decide to buy one make sure you understand how the tool works. If tool is capable of handling leaks only at process shutdown, it has very low value. Moreover, If you work on high-end server and have your own memory manager, most of those tools are not applicable.

 

I will be glad to hear your comments.

 

Enjoy weekend!