Reputation: 151
I'm trying to figure out where the leak is. I decided to use Valgrind. But with its use, the application's performance dropped almost fivefold. Can I speed up the application using Valgrind?
Upvotes: 11
Views: 10951
Reputation: 1069
If you are looking for an alternative tool that doesn't slow down the process at all, but has the limitations that it won't give you any stack traces, needs to be run on Linux and only supports libc malloc (as opposed to jemalloc or tcmalloc or the equivalent) try the free open source software https://github.com/vmware/chap
The way you use it is, roughly, grab a live core (using gcore, or generate from gdb or something equivalent) of your un-instrumented process after the process has exhibited undesirable memory growth, then from the command line:
chap core-file-name
From the chap prompt typing count leaked will tell you how many leaks there are, list leaked will list the leaked allocations, show leaked will give you hex dumps of the leaked allocations, describe leaked will attempt to describe the leaked allocations or summarize leaked will give you a summary of allocations by type, to the extent that chap can figure out the types.
...
If it should happen that count leaked says that there are no leaks, or not enough to explain your process growth, what that means is that rather than the leaked objects actually being unreachable from stack or registers or static memory (roughly chap's definition of leaked) they are probably in some container or containers, such as a set or map or queue. In that case chap can also help because it gives you the opportunity to walk the graph of allocations, where the nodes in such a graph are the allocations themselves, and if allocation A references allocation B there are edges in both directions between A and B. Using this fact, you can typically walk the graph from an allocation that you believe to be no longer needed to the container that holds it. Given that you are using C++, a very helpful chap command to start with in such a case is summarize allocated.
As a disclaimer here, I wrote most of this tool and am definitely promoting it here, but OTOH it is open source and doesn't cost anything. The documentation is still a work in progress but you can also get help from the tool's command line. Also, if you ask questions (either relative to this answer or by raising an issue on the github repository) I will be happy to answer them.
Upvotes: 4
Reputation: 38267
The memcheck tool slows down the application because of all the virtualization behind the scenes. You could study the command line parameters to look for tweaks that speed up the environment. As an alternative for gcc/clang, compile and link with -fsanitize=address
. This might detect your leak with fewer performance limitations.
Upvotes: 8