How reliable is windows task manager for determini

2019-02-18 08:30发布

问题:

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?

回答1:

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.



回答2:

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.