valgrind 使用指北

Advantages compared with gprof

  • it runs on an unmodified executable (using a virtual machine).
  • You can compile postgres the regular way, start the system up, and then create a short shell script called "postgres" that you put in place of the original executable that invokes valgrind on the original executable.
  • Valgrind does 100% tracing of the program rather than statistical sampling, and since it runs in a pure virtual machine, it can detect almost all memory corruption and leaks.

Disadvatages compared with gprof

  • The big disadvantage of valgrind is that it slows the process WAY down, like by a factor of 5-10 on CPU. For a pure CPU process, it doesn't screw up your stats, but if a process is mixed CPU and I/O, the CPU will appear to dominate.

How to use

  • Check memory leak
# see https://stackoverflow.com/a/44989219/8966765 for deatails.
valgrind --leak-check=full \
         --show-leak-kinds=all \
         --track-origins=yes \
         --verbose \
         --log-file=valgrind-out.txt \
         ./executable exampleParam1
  • Valgrind Callgrind
# https://web.stanford.edu/class/archive/cs/cs107/cs107.1226/resources/callgrind#:~:text=The%20Valgrind%20profiling%20tools%20are,and%20builds%20a%20function%20callgraph. 
valgrind --tool=callgrind program-to-run program-arguments
callgrind_annotate --auto=yes callgrind.out.pid
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容