Dr. Dobb's Journal - January 2008 - (Page 61) d01gopsh_p4ma 11/9/07 11:19 AM Page 61 Figure 1: Schematic lifetime of session objects. noise in reports. For example, a web server may have a pool of threads that handles user requests. You suspect that the memory leak is hiding in the code processing those requests, so you want to filter out all allocations made from other parts of the code. In many cases, you still want to process all memory deallocations, even in other threads (for example, you may send objects from processor threads to a special logger thread, and have the latter release those objects). External Tools Versus Integrated Solutions There are trade-offs in deciding which tool is most appropriate for your project. The first step is choosing between an independent external tool or an integrated solution that requires some changes in your source code. The clear advantage of the first option is the ease of use—you just need to plug it in to the application, no extra work required. Going back to the list of expectations from the memory debugging tool, you will probably need to give up the last requirement— selection of threads—because external tools are not aware of the internal layout of the process. You will also need to guess the best times to start/end monitoring, without knowledge of the current state of the program. On the other hand, adding simple code to your web server allows you to monitor specific threads while it processes a single request. You may prefer to invest some effort now, and build an infrastructure that will make memory debugging much easier in the future. In the long run, it will save you a lot of time. Existing Tools? Are there existing tools that implement the functionality I’ve examined here? To be truthful, I’m not aware of any commercial memoryprofiling tool, but there are a couple of open-source projects that implement this approach: • Windows Leaks Detector (sourceforge.net/projects/winleak) for Win32 attaches to any running process (no source code required), provides a basic UI to start and end monitoring memory activity, and finally produces a report of all memory leaks with full call stack information. It also lets you automatically add debugger breakpoints when memory allocation occurs. • LeakTracer (www.andreasen.org/LeakTracer) for Linux requires special compilation of your project. DDJ January 2008 l www.ddj.com l Dr. Dobb’s Journal 61 http://sourceforge.net/projects/winleak http://www.oreilly.com http://www.oreilly.com http://www.andreasen.org/LeakTracer http://www.ddj.com
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.