How to analyze memory leaks on Windows


We use valgrind to find memory leaks in MySQL on Linux. The tool is a convenient, and often enlightening way of finding out where the real and potential problems are location. On Windows, you dont have valgrind, but Microsoft do provide a free native debugging tool, called the user-mode dump heap (UMDH) tool. This performs a similar function to valgrind to determine memory leaks. Vladislav Vaintroub, who works on the Falcon team and is one of our resident Windows experts provides the following how-to for using UMDH:

  1. Download and install debugging tools for Windows from hereMS Debugging ToolsInstall 64 bit version if you’re on 64 bit Windows and 32 bit versionotherwise.

  2. Change the PATH environment variable to include bin directory of Debugging tools.On my system, I addedC:Program FilesDebugging Tools for Windows 64-bit to the PATH.

  3. Instruct OS to collect allocation stack for mysqld with gflags -imysqld.exe +ust.On Vista and later, this should be done in “elevated” command prompt,it requires admin privileges.

    Now collect the leak information. The mode of operation is that: take theheap snapshot once, and after some load take it once again. Comparesnapshots and output leak info.

  4. Preparation : setup debug symbol path.In the command prompt window, do

    set _NT_SYMBOL_PATH= srv*C:websymbols*http://msdl.microsoft.com/download/symbols;G:bzrmysql-6.0sqlDebug

    Adjust second path component for your needs, it should include directorywhere mysqld.exe is.

  5. Start mysqld and run it for some minutes
  6. Take first heap snapshot

    umdh -p:6768 -f:dump1

    Where -p: actually, PID of my mysqld was 6768.

  7. Let mysqld run for another some minutes
  8. Take second heap snapshot

    umdh -p:6768 -f:dump2

  9. Compare snapshots

    umdh -v dump1 dump2 > dump.compare.txt

  10. Examine the result output file. It is human readable, but all numbers arein hex, to scare everyone except geeks.
  11. gflags -i mysqld.exe -ust

    Instruct OS not to collect mysqld user mode stacks for allocationsanymore.

These are 10 steps and it sounds like much work, but in reality it takes 15minutes first time you do it and 5 minutes next time.Additional information is given in Microsoft KB article about UMDHKB 268343.