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*;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 >

  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.

Setting up the developer stack issues

There’s a great post on Coding Horror about Configuring the Stack.Basically the gripe is with the complexity of installing the typical developer stack, in this case on Windows, using Visual Studio. My VS setup isn’t vastly different to the one Jeff mentions, and I have similar issues with the other stacks I use. I’ve just set up the Ultra3 mobile workstation again for building MySQL and other stuff on, and it took about 30 packages (from Sun Freeware) just to get the basics like gcc, binutils, gdb, flex, bison and the rest set up. It took the best part of a day to get everything downloaded, installed, and configured. I haven’t even started on modules for Perl yet. The Eclipse stack is no better. On Windows you’ll need the JDK of your choice, plus Eclipse. Then you’ll have to update Eclipse. Then add in the plugins and modules you want. Even though some of that is automated (and, annoyingly some of it is not although it could be), it generally takes me a few hours to get stuff installed. Admittedly on my Linux boxes it’s easier – I use Gentoo and copy around a suitable make.conf with everything I need in it, so I need only run emerge, but that can still take a day or so to get everything compiled.Although I’m sure we can all think of easier ways to create the base systems – I use Parallels for example and copy VM folders to create new environments for development – even the updating can take a considerable amount of time. I suggest the new killer app is one that makes the whole process easier.

Using static disks in Parallels for performance

Using a static disk, or even just multiple disks, within Parallels can make a big difference to performance. This is particularly true with Windows virtual machines within OS X; I’ve managed to change the boot time from about 30 seconds to under 20 just by changing to a static disk for VM. The default disk in Parallels is an expanding type – this saves disk space, because Parallels automatically adds to the size of the disk as you need it, but it also means that Parallels has to manage the allocated disk space, adding to the file used. Not only does the management imply a small overhead, there is a much larger chance of the file being fragmented. A more annoying effect is that the constant use of the expanding disk with virtual memory under Windows, means that size of the disk may increase just because you opened a large application once. You can get round this by creating a statically-sized disk, and then setting the virtual memory within your virtual host, to use this statically sized disk. To do this:

  1. Shutdown your virtual machine – you cannot do this with a machine in the paused stated, because you are effectively adding new hardware to the machine.
  2. Click Edit to edit the configuration for the virtual machine.
  3. Click Add, and select a new hard disk
  4. Unclick the Expanding checkbox and set the size; probably 1-2GB is fine, but keep in mind you will lose this amount of disk space permanently, even if your VM doesn’t use it all.
  5. Save your configuration.
  6. Start up your VM and configure the new drive.

For Windows:

  1. Log in as a user with Administrator privileges.
  2. Right click on My Computerand choose Manage.
  3. Choose Local Disk Management.
  4. Create a new partition/volume.
  5. Once the new disk is ready to use, right click on My Computer again, and choose Properties.
  6. Click the Advanced tab.
  7. Click Settings under Performance.
  8. Click the Advanced tab.
  9. Click Change under Virtual Memory.
  10. Reconfigure the VM settings, creating the new settings for the new drive (I recommend a lower value of 50MB and an upper value 2-10MB below the maximum size of the disk. Windows will use the minimum and dynamically increase it’s usage up until the maximum.
  11. Remove the VM configuration for the original system/expanding disk.

You should be all set.It’s probably a good idea to run the Parallels Compressor and reduce the size of your disk now that you are no longer using the disk for virtual memory. For Linux, Solaris and other Unix variants you might want to run, the process is of course slightly different. For some environments, there are other benefits, but I’ll cover that in a separate post.