2
votes

I have quite a complex system, with 30 applications running. One quite complex C++ application was leaking memory, and I think I fixed it.

What I've done so far is:

  • I executed the application using valgrind's memcheck, and it detected no problems.
  • I monitored the application using htop, and I noticed that virtual and residual memory is not increasing
  • I am planing to run valgrind's massif and see if it uses new memory

The question is, how can I make sure there are no leaks? I thought if virtual memory stopped increasing, then I could be sure there are no leaks. When I test my application, I trigger the loop where the memory is allocated and deallocated several times just to make sure.

3

3 Answers

0
votes

You can't be sure except you know exactly all the conditions under which the application will allocate new memory. If you can't induce all of these conditions neither valgrind nor htop will guarantee that your application doesn't leak memory under all circumstances.

Yet you should make at least sure that the application doesn't leak memory under normal conditions.

0
votes

If valgrind doesn't report leaks, there aren't leaks in the sense of memory areas that aren't accessible anymore (during the runs you checked). That doesn't mean that the program doesn't allocate memory, uses it and doesn't free it when it won't use it anymore (but it is still reachable). Think e.g. a the typical to-do stack, you place new items on top, work on the item on top and then push another one. Won't ever go back to the old ones so the memory used for them is wasted, but technically it isn't a leak.

What you can do is to monitor the memory usage by the process. If it steadily increases, you might have a problem there (either a bona fide leak, or some data structure that grows without need).

If this isn't really pressing, it might be cheaper in the long run just letting it be...

-1
votes

You need to use a tool called Valgrind. It is memory debugging, memory leak detection, and profiling tool for Linux and Mac OS X operating systems. Valgrind is a flexible program for debugging and profiling Linux executables. follow steps..

Just install valgrind

To run... ./a.out arg1 arg2

Now how to Use this command line to turn on the detailed memory leak detector: valgrind --leak-check=yes ./a.out arg1 arg2 valgrind --leak-check=yes /path/to/myapp arg1 arg2

Or You can also set logfile: valgrind --log-file=output.file --leak-check=yes --tool=memcheck ./a.out arg1 arg2

You can check its log for error of memory leak... cat output.file