• About Us
  • Blog
  • Basket
  • Account
  • Sign In


How to Find and Manage Memory Leaks

Published on Thursday, July 5, 2018

How to Find and Manage Memory Leaks

51Degrees recommends Valgrind to test and validate builds

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:

  1. Install with ‘sudo apt-get install Valgrind massif-visualizer’ (for Debian/Ubuntu, other Linux distributions will differ).
  2. Check for leaks with ‘valgrind --leak-check=full ./yourprogram’.
  3. 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.

Figure 1 - Go API Memory
Figure 1 – Go API Memory

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.

Figure 2 – C memory allocations
Figure 2 – C memory allocations. Yellow represents the data file, and blue is the Match objects returned for each set of results.
Figure 3 – Memory leaks example
Figure 3 – Memory leaks have occurred causing a difference in the graph. This visual generated from 'valgrind—tool=massif ./yourprogram’ would imply errors that needed to be fixed.
Figure 4 – Memory leaks reported by Valgrind
Figure 4 – Memory leaks reported by Valgrind would imply errors that needed to be fixed.

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.

Contact us or get started with a free trial

Test My Device Start a Free TrialContact Us

Comments (0)


Other posts by Paula
Contact author



.NET 4G 51Degrees 5G Acer Adform Adtech Advertising Afilias Alcatel Amazon AMP Analysis Analytics Android Apache API Apple Asian Market ASP.NET Asus Blackberry Browser C C# Centro Chrome chromium Client hints Cloud CMS CPU CSS3 Data Data Blog Data File Daydream Design Detection Developers Device Device Data Device Detection Device Intelligence Device Models Device property DeviceAtlas Disney dmexco DotNetNuke Download ebay Ericsson Event Facebook Firefox Foundation Framework Galaxy git repositories Google Google Analytics Google Daydream GPU GSMA Guess HAProxy Hash Trie HTC HTML5 HTTP HTTP Headers Huawei Infinix Ingeniux Internet usage iOS iOS 13 ipad iPadOS iPhone iPhone 11 Java Javascript Kentico LG Liferay LTE m.dot Memory Memory leak Meta Data Microsoft Mobile Mobile Analysis Mobile Analytics Mobile Devices Mobile Marketing Mixer Motorola Mozilla MWC MWC 2017 MWC16 Native Apps NET New Release News Nexus NFC NGINX Nokia OnePlus 5 Opera Operating System Oppo Optimisation OS Patent Performance PHP Press Release Price Band programmatic Publishers Python Redirection Research Responsive Images Responsive web design RESS Review RTB RWD Safari Samsung Scala ScientiaMobile SEO Server Server-side optimisation Seznam.cz Sitecore Smart TV Smartphone Smartwatches Sony Swedish Beers Tablet Tencent Testing Tips Tutorial Umbraco Update User Agent User-Agent Valgrind Varnish Varnish Cache Video Vodafone VoLTE Web Web Apps Web content management Webtrekk White Paper Widgets WiFi Windows WURFL Xiaomi Xperia ZTE