Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Valgrind works slowly

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?

like image 434
kirill-782 Avatar asked Jun 12 '18 12:06

kirill-782


People also ask

Why is Valgrind so slow?

According to Valgrind Manual, Valgrind will very noticeably slow down your application: The amount of instrumentation code added varies widely between tools. At one end of the scale, Memcheck adds code to check every memory access and every value computed, making it run 10-50 times slower than natively.

How can I make Valgrind faster?

Valgrind doesn't actually execute your code natively - instead it runs it inside a simulator. That's why it's so slow. So, there's no way to make it run faster, and still get the benefit of Valgrind. Your best bet is to set the ulimit so that your program generates a core file when it crashes.

What are the problems with Valgrind?

Valgrind reports two types of issues: memory errors and memory leaks. When a program dynamically allocates memory and forgets to later free it, it creates a leak. A memory leak generally won't cause a program to misbehave, crash, or give wrong answers, and is not an urgent situation.

Is Valgrind ever wrong?

Yes, there are false positives with Valgrind, that's why it has suppression files for particular glibc and gcc versions, for example.


1 Answers

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.

like image 64
Tim Boddy Avatar answered Oct 21 '22 14:10

Tim Boddy