Memory leak detection
Do anyone know of a general algorithm to detect mem开发者_运维技巧ory leak?
Memory Leak Analysis by Contradiction
http://www.cs.cornell.edu/~rugina/papers/sas06.pdf
And less relevantly...
Apple dev docs have an article on the topic also (specific)...
http://developer.apple.com/library/mac/#documentation/Performance/Conceptual/ManagingMemory/Articles/FindingLeaks.html
and on tracking memory usage generally (also specific)...
http://developer.apple.com/library/mac/#documentation/Performance/Conceptual/ManagingMemory/Articles/FindingPatterns.html#//apple_ref/doc/uid/20001882-CJBJFIDD
The simplest way is to have a counter for each kind of object you have in your application. When allocating an object increase the counter, when dellocating decrease it. When the application terminates, check that all counters are zero.
While not an algorithm, there are a plethora of 3rd party tools that will help analyze your code for memory leaks as well. Depending on the size of your project, it might not be reasonable to manually track all allocations yourself.
I personally like to use valgrind if i am in a *nix environment.
alternative, let your program run for a long time and watch the memory allocation the process uses from top or task manager. If it's leaking, it will consistently go up. If not, it should inflate to it's maximum value then hold stay, or fluctuate between this and a lower value.
Unfortunately growth does not necessarily equal a leak, could just be your program needs A LOT of memory.
If you use Windows you need deleaker. If you use *nix try valgrind.
Hi this is the way to detect memory leak in the preprocessor directives of the code include these statements
# define _CRTDbg_Map_Alloc
# include<stdlib.h>
# include<crtdbg.h>
and in the main function use this function to dump the memory leaks.
_CrtDumpMemoryLeaks();
精彩评论