We present and evaluate a new technique for detecting and eliminating memory leaks in programs with dynamic memory allocation. This technique observes the execution of the program on a sequence of training inputs to find -bounded allocation sites, which have the property that at any time during the execution of the program, the program accesses at most only the last objects allocated at that site. If the difference between the number of allocated and deallocated objects from the site grows above throughout the computation, there is a memory leak at that site. To eliminate the leak, the technique transforms the program to use cyclic memory allocation at that site: it preallocates a buffer containing objects of the type allocated at that site, with each allocation returning the next object in the buffer. At the end of the buffer the allocations wrap back around to the first object. Cyclic allocation eliminates any memory leak at the allocation site — the total amount of ...
Huu Hai Nguyen, Martin C. Rinard