When our customers need to test and validate builds, we can advise on the best 3rd party tools to assist in that process. One such tool used to test 51Degrees Device Intelligence products is Valgrind. In this article, we shed further light on why Valgrind is used and the benefits of the tool.
What is Valgrind?
“Valgrind is a programming tool for memory debugging , memory leak detection and profiling”
In this article, we will be focusing on the use of Valgrind alongside the 51Degrees API as a memory error detector.
Why use Valgrind?
Installing Valgrind and running it alongside your project can be a useful strategy in finding memory leaks within your API. This is especially important when much of the logic sits in unmanaged code, like the 51Degrees Go API. This approach gives a large performance improvement but introduces the risk of memory leaks if not properly tested.
Not closed an instance of something? Let a reference fall out of scope?
Then expect there to be leaks present within your project. It is good practice to run leak check tests before releasing any new code changes.
Want to add caching to your project?
Valgrind is also a good tool for informational purposes. it can produce a visual output of the additional memory that will be consumed within your project.
Running Valgrind with ‘--leak-check=full’ will output a list of memory allocations which may not have been freed.
When writing tests intended to induce potential leaks, we advocate simplicity of code. An output with 2 memory leaks is much easier to view than one with >100, and will partially (if not fully) solve a leak when the source is found.
Setting up Valgrind
To set up Valgrind for your appropriate environment please visit their download page.
The following prompts help to solve problems with memory leaks while maintaining code that is safe to progress with.
These short code snippets summarise the actions:
- Install with ‘sudo apt-get install Valgrind massif-visualizer’ (for Debian/Ubuntu, other Linux distributions will differ).
- Check for leaks with ‘valgrind --leak-check=full ./yourprogram’.
- Generate memory usage graph with ‘valgrind --tool=massif ./yourprogram’ and open the file with massif-visualizer.
Example 1 - Go API Memory
Figure 1 shows the memory usage of simple Go program which stresses the 51Degrees API. This is the memory profile we would expect to see from any language which implements garbage collection, i.e., an initial increase, then a steady sawtooth.
Example 2 - C memory allocations
When calling a compiled library though, there is more going on underneath the surface. Figure 2 shows the visual output from Valgrind when running the same Go program. The graph shows that much of the memory is allocated in the C layer, rather than Go.
As the API being tested here is the 51Degrees Device Intelligence, the memory is constant. All the memory is allocated up front to ensure there are no surprises.
Finally, in figures 3 and 4 we have intentionally introduced a memory leak in the API. This can be seen in the graph where the memory continues to rise. But more usefully, in the leak check output. This indicates exactly where the memory was allocated, making fixing the issue simple.
We hope you found this article useful.
If you have any suggestions, or you require support for your device detection project, you can contact us and we can look into it. Our developer documentation provides all the information required to get started with 51Degrees Device Detection using your preferred API.