Using Valgrind

valgrind is a suite of tools for debugging and profiling Linux programs. We only use it to detect memory leaks, but it has a lot of other useful features. To install valgrind on Debian, use the following command:

  # apt-get install valgrind

Detecting Memory Leaks

A daemon that leaks memory when handling requests will eventually consume all of the free system memory. When the system cannot allocate any more memory, malloc() will fail and programs on the system will begin to fail. To prevent this situation from happening it is important to make sure that no memory is leaked. To help identify memory leaks, we use valgrind's memcheck tool. Use the following command to run yoctohttpd with valgrind:

  # valgrind --tool=memcheck --leak-check=full ./src/examples/yoctohttpd

yoctohttpd should work normally as if it wasn't being profiled. Try browsing to /. When you are satisfied that you've exercised some of the code, stop yoctohttpd with the following command (issue it in a separate console):

  yoctohttpd -k

The above command will (hopefully) cause yoctohttpd to shutdown gracefully. When yoctohttpd exits, valgrind will display a report about the memory usage and show if any memory was leaked. A sample report is shown below:

  ==18884== LEAK SUMMARY:
  ==18884==    definitely lost: 292 bytes in 1 blocks.
  ==18884==      possibly lost: 136 bytes in 2 blocks.
  ==18884==    still reachable: 26,291,152 bytes in 538,338 blocks.
  ==18884==         suppressed: 0 bytes in 0 blocks.
  ==18884== Reachable blocks (those to which a pointer was found) are not shown.
  ==18884== To see them, rerun with: --show-reachable=yes

It is important to test yoctohttpd multiple times with differing input exercise all of the server code. Information about leakages will be displayed in the valgrind output above the LEAK SUMMARY.