How to find memory leak in production enviroment

Use this forum for questions on how to use .NET Memory Profiler and how to analyse memory usage.
Post Reply
Posts: 5
Joined: Fri Jun 14, 2013 3:08 pm

How to find memory leak in production enviroment

Post by jensod » Wed Apr 30, 2014 8:01 am


I am trying to find a memory leak that occours only in production environment
I can not run Memory Profiler in production environment

I have found your response to a similar gustion here: ... plus#p5980
where you say:
> It is possible to configure the ADPlus tool included with "Debugging Tools for Windows" so
> that it automatically creates a memory dump file when an OutOfMemoryException occurs.
> If this is something you are interested in, please tell me and I will give you some more information.

This seems to fit my situation, could you provide more information?
We are able to detect that there is a memory problem long before the application crashes
so if you have some assembly we can link to with an api that create a simmilar memory dump that could work too

Thanks Jens

Andreas Suurkuusk
Posts: 1030
Joined: Wed Mar 02, 2005 7:53 pm

Re: How to find memory leak in production enviroment

Post by Andreas Suurkuusk » Thu May 01, 2014 8:42 pm

If you can detect the memory problem from within the process, you can generate a memory dump by calling the MiniDumpWriteDump function.

Below I have provided a small class that can be used to create a memory dump. Just call the MemoryDump.Create method with the name of the dump file and Process.GetCurrentProcess() as process.

Code: Select all

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Diagnostics;

public static class MemoryDump
    public static bool Create( string dumpFilePath, Process process )
        using( FileStream stream = new FileStream( dumpFilePath, FileMode.Create, FileAccess.ReadWrite ) )
            return MiniDumpWriteDump( process.Handle, process.Id, stream.SafeFileHandle, MINIDUMP_TYPE.MiniDumpWithFullMemory, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero );

    [DllImport( "dbghelp" )]
    [return: MarshalAs( UnmanagedType.Bool )]
    static extern bool MiniDumpWriteDump(
        IntPtr hProcess,
        int ProcessId,
        Microsoft.Win32.SafeHandles.SafeFileHandle hFile,
        MINIDUMP_TYPE DumpType,
        IntPtr ExceptionParam,
        IntPtr UserStreamParam,
        IntPtr CallbackParam

        MiniDumpNormal = 0x00000000,
        MiniDumpWithDataSegs = 0x00000001,
        MiniDumpWithFullMemory = 0x00000002,
        MiniDumpWithHandleData = 0x00000004,
        MiniDumpFilterMemory = 0x00000008,
        MiniDumpScanMemory = 0x00000010,
        MiniDumpWithUnloadedModules = 0x00000020,
        MiniDumpWithIndirectlyReferencedMemory = 0x00000040,
        MiniDumpFilterModulePaths = 0x00000080,
        MiniDumpWithProcessThreadData = 0x00000100,
        MiniDumpWithPrivateReadWriteMemory = 0x00000200,
        MiniDumpWithoutOptionalData = 0x00000400,
        MiniDumpWithFullMemoryInfo = 0x00000800,
        MiniDumpWithThreadInfo = 0x00001000,
        MiniDumpWithCodeSegs = 0x00002000,
        MiniDumpWithoutAuxiliaryState = 0x00004000,
        MiniDumpWithFullAuxiliaryState = 0x00008000,

        MiniDumpValidTypeFlags = 0x0000ffff,
Best regards,

Andreas Suurkuusk
SciTech Software AB

Post Reply

Who is online

Users browsing this forum: No registered users and 22 guests