2

I have a .net project that uses C++ project and is eating a lot of memory.

I wonder if there is quick and easy way to count inflow bytes allocated by new and outflow bytes freed by delete operator. Add some logging or something.

Source code for both operator is provided by Visual Studio so I can hack it.

The problem is I can see huge VM consumption and I wanna investigate why. I tried several memory profilers but none of them can deal with unmanaged C++ allocation within .NET application

Captain Comic
  • 15,744
  • 43
  • 110
  • 148
  • I'm not sure about your platform but on many platforms you can use [DTrace](http://hub.opensolaris.org/bin/view/Community+Group+dtrace/WebHome) to observe memory leaks. –  Aug 04 '11 at 12:17

2 Answers2

4

Usually memory profiling tools like Valgrnid or Rational Purify can help you profile the memory utilization of programs.

In case you still want to have your own implementation,
You can Replace the global new and delete operators by overloading them and inside your own overloaded operators you can maintain count of the memory allocated.

In case you choose/are forced to follow the second option, there are certain aspects to be taken care of, You can read the details in this answer here.

If you are using STL:
The STL container classes in turn use the Global new & delete operators for allocations. So If you replace the new & delete global operators then STL will use them instead of std new and delete operators.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • +1, but is some classes overloads `operator new` the same should be done for those classes. – sharptooth Aug 04 '11 at 12:19
  • Als, Rational Purify DOES not count unmanaged allocation for .NET app. It provides C++ profiling as well but my app crashes in that mode on MemoryViolationException so its pretty useless for me. Also something is not clear. My third-party library uses STL. STL containers call new and delete somewhere. Where should I overload operator? – Captain Comic Aug 04 '11 at 12:25
  • 1
    @Captain Comic: Updated the answer to answer your doubt. As for how to do it, Please refer the link. – Alok Save Aug 04 '11 at 12:32
1

You can attach with windbg and start your investigation using !heap -s command.

cprogrammer
  • 5,503
  • 3
  • 36
  • 56