July 14th, 2009
There are many complicated and expensive products out there to help find memory leaks and optimize RAM usage, but I find that the built-in Task Manager on Windows (or Activity Monitor on Mac) is very easy to use, and powerful enough for most tasks. Here's what the task manager looks like (Overgrowth is still listed as Lugaru2.exe):
You can access the Task Manager by right-clicking the task bar and selecting it from the contextual menu (or use ctrl-alt-del). It provides a small window with a real-time tally of how much RAM your program is using. This is useful to us, because we can watch it as we run our program, and see if the program is using more memory than we expect it to. If the memory usage keeps increasing when we don't expect it to, then the program probably has a memory leak.
By stepping through our program with a debugger, we can even use the Task Manager to see how much RAM individual functions use. Here is the task manager before and after calling the Terrain::Load() function.
By subtracting the new memory usage from the old, we can tell that Terrain::Load() allocated about 40.5 MB of RAM. That is a significant percentage (27%) of the RAM used by the whole program, so if I want to reduce RAM usage, this would be worth looking into further. In this case, it is not really a problem, because most of that RAM is deallocated soon after.
Do any of you know of better ways to easily detect memory leaks and track memory usage? For more detailed memory tracking, Instruments works well on Mac, and Valgrind works well on Linux, but I don't know of any free equivalents for Windows.