1

I have this problem when running my program:

  • The memory consumption increases pretty quickly when one of the functionalities of my program is running.
  • I use Performance Monitor and Virtual Leak Detection, both say no leak.
  • After the functionality is ended (program not exiting), the memory will slowly drops to normal level.
  • Program is basically C#, WPF, C++;

So when the memory consumption gets high, the hardware (motor), which my program drives, responses pretty sluggishly.

I am very confused. Is this a memory leak?

I know it is probably hard to determine where the problem is, but is there any common logic how I should get in look at this problem? Or any commonly used tools? Like checking for intake leak/system lean on a car would be normally starting with tubes, mass air flow sensors, or O2 sensors.....

Thanks a lot!

Nick X Tsui
  • 2,737
  • 6
  • 39
  • 73
  • Since your program does eventually reclaim all of the used memory, I would say no it is not a leak. You may have references being held longer than necessary which could cause the behavior you are seeing. Try using a memory profiler like [scitech](http://memprofiler.com) or something similar. – Mark Hall Oct 24 '13 at 22:48
  • But it causes trouble. As the program runs, the hardware responses more and more sluggish, which is not bearable for the users. – Nick X Tsui Oct 25 '13 at 12:11
  • I am not saying it isn't a problem, but with the information given anything would be a guess. That is why I suggested a Memory Profilier, you need to first determine what is hogging memory before you can mitigate the problem. – Mark Hall Oct 25 '13 at 13:00
  • So if my program consists of many dlls, is there anyway I can find which dll is leaking? Thanks. – Nick X Tsui Oct 25 '13 at 13:57

2 Answers2

1

There are a couple of things you can try - Try running Sysinternals ProcessMonitor tool (Process Monitor v3.2) and configure the symbol path and your source code path correctly assuming you are running on windows platform. The logs will most probably tell you the line number and source which is causing the leak. You need to know how to use process monitor and navigate through the logs.

Otherwise, you can also try the below CRT API's to track memory allocation/ deallocation and spit out the memory leak dump for further investigation. The below code works only in debug mode.

    #define _CRTDBG_MAP_ALLOC
    #include <stdlib.h>
    #include <crtdbg.h>

    #ifdef _DEBUG   
      #ifndef DBG_NEW      
         #define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )      
      #define new DBG_NEW   
      #endif
    #endif  // _DEBUG

    _CrtMemState crtMemStateStart;
    _CrtMemState crtMemStateFinish;

    _CrtMemCheckpoint(&crtMemStateStart);


    // Your suspisious code


     _CrtMemCheckpoint(&crtMemStateFinish);

      int nDifference(0);
      _CrtMemState crtMemStateDifference;
      nDifference = _CrtMemDifference(&crtMemStateDifference, &crtMemStateStart, &crtMemStateFinish);

    if(nDifference > 0)
        _CrtDumpMemoryLeaks();

See this link for more information : Finding Memory Leaks Using the CRT Library

Remember always memory leaks can be a bit tricky to find especially if there is COM code involved. But having the right knowledge and tools definitely makes life a bit easier.

DanielV
  • 2,076
  • 2
  • 40
  • 61
Tushar Jadhav
  • 335
  • 4
  • 14
  • Thanks a lot. My program has a starting application coded with C#, but it loads a lot of dlls, managed and unmanaged, runtime, and I have no idea which dll might cause the problem. So in this case, what source code I should point to? The starter, or anyone suspicious? Also, here is my current symbol path: srv*http://msdl.microsoft.com/download/symbols, but I have no idea how I should configure it. Any idea? Thanks. – Nick X Tsui Oct 25 '13 at 12:10
  • Set the source path to the parent folder which contains all the other projects. If you have not organized your projects in such a manner, then if possible do it and rebuilt the DLL's and executables. Set the source path to SRV*C:\MySymCache*http://msdl.microsoft.com/download/symbols and make sure that you create C:\MySymCache directory so that the symbols are cached locally and not always downloaded from Microsoft symbol server. – Tushar Jadhav Oct 25 '13 at 20:05
  • I would also recommend installing Microsoft Windows SDK (available freely on Microsoft website, just search for it with your favorite search engine) to get dbghelp.dll from it. This is an updated version of DLL and more powerful that the one which ships with Windows installed in C:\Windows\System32 directory. The default one is not capable of caching symbols locally. It is extremely critical that you use the updated version and for the correct platform (x86/ x64). – Tushar Jadhav Oct 25 '13 at 20:11
0

Memory Profiler works for me:

Memory Profiler trial version download

This the trial version, and I like it.

Nick X Tsui
  • 2,737
  • 6
  • 39
  • 73