0

I have a C++ application running all time (24 x 5) and I doubt there are memory leaks. How can I identify the memory leak. Are there any tools I can use ?

I have a set of files which have commands to change the state of the C++ application. When I replay those same files, I could see an increase in the memory occupied by the process during each iteration of the replay. I tried valgrind and it runs for more than 5 days for completing a single iteration of replaying them

valgrind --tool=memcheck --leak-check=yes \
  --show-reachable=yes --num-callers=20 --track-fds=yes obj

I am new to this domain. Kindly suggest me a good tool to check the memory leak or give some ideas to identify it.

Marek R
  • 32,568
  • 6
  • 55
  • 140
  • 1
    best tool for that is [Address Sanitizer](https://clang.llvm.org/docs/LeakSanitizer.html). This is compiler functionality available in clang and gcc. Here is some helpful [SO answear](https://stackoverflow.com/a/40215639/1387438). But note to detect leaks your application should be run in all possible ways. – Marek R Sep 12 '19 at 14:53
  • 1
    Valgrind will considerably slow your application (especially if it's multithreaded). You could just try to monitor the RAM usage of your OS overall and see if it's continually increasing or not. – Clonk Sep 12 '19 at 15:08
  • @Clonk Yes, my RAM usage keeps on increasing. But how to find where the memory leak happens ? – Vinodini Natrajan Sep 12 '19 at 15:37
  • @MarekR I tried adding the flags like mentioned, but I am getting this error while compiling ```/usr/bin/ld: cannot find /usr/lib64/libasan.so.0.0.0 collect2: error: ld returned 1 exit status``` – Vinodini Natrajan Sep 12 '19 at 16:26
  • What OS do you use? Linux? Distribution? Probably you need to install some additional package. – Marek R Sep 12 '19 at 16:57
  • @MarekR Yes, I use linux – Vinodini Natrajan Sep 13 '19 at 11:12
  • `sudo apt-get install libasan0` or `sudo yum install libasan` – Marek R Sep 13 '19 at 11:15
  • ```yum list installed libasan Installed Packages libasan.x86_64 4.9.2-6.2.el7 ``` Its still giving the same error – Vinodini Natrajan Sep 13 '19 at 13:17
  • It interesting that it looks for `0.0.0` and you version is `4.9.2` – Marek R Sep 16 '19 at 10:23
  • @MarekR I uninstalled and installed the new package. Its compiling fine now. Where can I see the output after running my code ? – Vinodini Natrajan Sep 16 '19 at 18:07
  • I usually use this on MacOS from Xcode (which is integrated with this feature). AFAIK reports are printed to stdout, when you run application, if not it should go errors stream. For each memory leak it should print two callstacks. First for the function where leak has been detected and second one when problematic block of memory was allocated. – Marek R Sep 17 '19 at 09:08
  • @MarekR I am unable to use gcc - https://stackoverflow.com/questions/57971882/clang-vs-gcc-behavioral-difference-with-addresssanitizer – Vinodini Natrajan Sep 17 '19 at 10:02
  • You might consider https://github.com/vmware/chap if you are running on Linux. To use chap to check for leaks just grab a live core of your process (no instrumentation needed) and run chap with that newly created core as the argument. You can then do things like "show leaked"... – Tim Boddy Oct 31 '19 at 10:17

0 Answers0