How reliable is the Windows task manager for determining memory usage in programs?

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

This is after I made changes to my program. Before the change, memory usage will continue to grow every time I indexed some files and then cleaned up. Therefore, after many purifications, the use of my program in memory increased and increased.

I understand that this is probably a β€œhacker” way to profile my application, but I'm a student, and all I managed to find are commercial profiling tools that are not available. I also read about valgrind, which is linux only, and I am developing it on Windows. Is using the task manager accurate or am I mistaken?

+4
source share
2 answers

TaskMgr is an extremely rude tool, but nonetheless useful. If you have memory leaks in the 1 megabyte range, then this is probably good enough to say that you have them. But in the end, you will look for leaks in 10 kilobytes and under ranges, and TaskMgr is useless for them.

+5
source

TaskMgr is too rude for this purpose. Especially if you have a lot of dynamic allocations and releases that will lead to a highly fragmented heap memory, in which case it is difficult to distinguish between leaks and natural heap growth due to fragmentation. You should use the Win32 API calls to check the total amount of memory allocated by your application. A few years ago, when I still had problems with memory leaks (no longer thanks to RAII), I used to start the main () small piece of code that requested the total amount of memory blocks allocated on the heap, and then request it back in at the very end of the main () function, if these two values ​​do not match, I would report a "memory leak of X bytes" error at this point.

If you want to do this, you can use GlobalMemoryStatuxEx or HeapWalk . The former is simpler and faster, but more rude, and the latter is more accurate, but much more extensive.

+7
source

All Articles