4

Can I use task manager to detect huge memory leaks? I have a small text parsing program that shows memory usage of around 640K when I launch it. When I parse a file and index it the memory usage grows depending on the size of the file. Then when I "clear" the index, my memory usage drops down to around 1400K. After this point, I can add as many files as I want and when I clear the index, memory usage drops down to this 1400k level + or - a ~5%.

This is after I made a change in my program. Before the change the memory usage would continue to go up every time I indexxed some files then cleared. So after many clears, the memory use of my program was growing and growing.

I realize that this is probably a "hackish" way to profile my app but I'm a student and all I've been able to find are commercial profiling tools which are out of reach. I've also read about valgrind which is linux only and I'm developing on windows. Is using the task manager accurate at all or am I misguided?

Pete
  • 10,651
  • 9
  • 52
  • 74
  • did you look in to this http://stackoverflow.com/questions/67554/whats-the-best-free-c-profiler-for-windows-if-there-are – uncaught_exceptions Apr 20 '11 at 17:14
  • Thanks, I did not come across that in my search and there is good info in there. I would still like to know about the accuracy and viability of using windows task manager to get a feel for the memory usage of apps and see if there are any leaks. – Pete Apr 20 '11 at 17:17
  • Simple answer to your question is yes. Task Manager is reliable enough to tell how much memory does the process, in total takes.(from os perspective) .But it will not provide any granular details and help you in debugging.In your case, it looks like you are fine with task manager right now. But the approach will not scale :) – uncaught_exceptions Apr 20 '11 at 17:20

2 Answers2

7

The TaskMgr is way too crude for this purpose. Especially if you have a lot of dynamic allocations and deallocations which will lead to a highly fragmented heap memory, in which case, it is hard to differentiate between leaks and natural growth of the heap due to fragmentation. You should use win32 API calls to inspect the total amount of memory allocated by your application. Some years ago, when I used to still have problems with memory leaks (don't have those anymore thanks to RAII), I used to put at the start of the main() a little piece of code that queried for the total amount of memory blocks allocated on the heap, and then query it again at the very end of the main() function, if the two values didn't match, I would report a "memory leak of X bytes" error at that point.

If you want to do that, you can use either GlobalMemoryStatuxEx or a HeapWalk. The former is simpler to use and faster, but more crude, while the latter is more precise, but much more extensive.

Mikael Persson
  • 18,174
  • 6
  • 36
  • 52
5

TaskMgr is an extremely crude tool, but it is useful nonetheless. If you have memory leaks in the one megabyte range, then it is probably good enough to tell that you have them. But, eventually, you'll be looking for leaks in the 10 kilobyte and under ranges, and TaskMgr is useless for those.

wallyk
  • 56,922
  • 16
  • 83
  • 148
  • Thanks for the tip. At the moment the leaks I am looking for are pretty huge (a few megs). I'm working with sets and pointers and I suspect that the objects created on the heap aren't being removed at all. I still haven't been able to find out why though despite, what I believe to be, a few well placed delete statements. – Pete Apr 20 '11 at 17:21