Test for Memory Leak
A number of ways are available to determine if a computer program is leaking memory. A programmer can sift through the source code to make sure that all memory allocated has been deallocated, he can write a program that tests his software for memory leaks, he can devise a memory management scheme that allows him to keep track of used memory or he can use a third-party program that tests for memory leaks.
-
What is a Memory Leak?
-
A memory leak is an unwanted memory usage by a computer program, where the program allocates memory for usage and fails to deallocate it once the memory is no longer in use. This chunk of memory will no longer be available to the computer program. A program that leaks enough memory over enough time can cause the system to run out of available memory, which can cause the program or computer system to fail. Memory leaks can also cause a system to slow down to an unsatisfactory speed, due to memory thrashing. Programs that run for long periods of time, perform many allocations or allocate memory in large chunks can be especially prone to dangerous memory leaks.
Use Good Coding Practices
-
The best way to ensure that your program is not leaking memory is to write good code that appropriately manages the system's resources. Make sure that for every function call that allocates memory, there is a corresponding call that deallocates memory. Some languages like Java, C#, and VB.NET, provide garbage collection, where the system regularly checks that allocated memory is still in use. While these systems do not prevent memory leaks completely, it is less likely that they will occur. Other languages, like C++, allow stack allocation, where a chunk of memory is deallocated automatically once the variable that references it goes out of scope. By using the correct language for the task and maintaining good software writing practices, many memory leaks can be avoided entirely.
-
Create a Memory Manager
-
Some computer programs require so much code to run that reading through the program to find where memory has leaked is simply not feasible. In these cases, it may make sense to write a memory manager component to the program. The software would then have a uniform interface for allocating any chunks of memory, and the manager keeps track of where and how these chunks are allocated. In its simplest form, the manager simply provides feedback, allowing the programmer to see how much memory is used, and what components are using it. It can also provide garbage collection and control how memory is allocated and deallocated, giving a programmer only one place to look for memory leaks, instead of all the code for the entire program.
Create a Test Program
-
One can often look at the memory used by a program with a system tool, such as the Windows Task Manager or "top" on Unix/Linux to determine if memory is being leaked. This is not always evidence of a memory leak, but can provide some initial feedback. Sometimes it is possible to write a simple program that tests the various components of a piece of software to determine if memory is leaking. The test program can dynamically determine or be given prior knowledge of how much memory a computer program should use. It then ensures that the amount used is the amount that should be used. If a memory leak occurs, the test program provides feedback on how that happened, allowing the programmer to fix the leak.
Third Party Software
-
Programmers can use many third-party programs and libraries to test if their software is leaking memory. In the Microsoft .NET Framework, one can enable memory leak testing by using the debug versions of memory allocation functions. On Linux, Unix and Mac, you can use tools, such as Valgrind, dmalloc and Insure++ to isolate a memory leak. Many memory debuggers are also available for Windows. The best tool for the job depends on the development environment and the language being used, as well as specific characteristics of the program to debug, such as size, speed requirements and they way it interacts with the system. There are many tools from which to choose, and they can be a boon for the frustrated programmer tired of searching for that last elusive leak.
-
References
- Photo Credit Image by Flickr.com, courtesy of Michael Vroegop