Skip to content

Instantly share code, notes, and snippets.

@joscdk
Last active September 9, 2019 13:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save joscdk/a193d9eb7e24c26d81d3c8c85d798419 to your computer and use it in GitHub Desktop.
Save joscdk/a193d9eb7e24c26d81d3c8c85d798419 to your computer and use it in GitHub Desktop.

It often be helpful to be able to debug memory usage in Swift on Linux, for example to debug memory leaks.

This is a quick overview of how to generate a report of memory usage for your Swift app. This guide will only show you how to generate the report, not how to analyze it, it might be written up in a blog post later.

Install Valgrind

First we need to install a tool called Valgrind, which is used to trace memory usage.

sudo apt-get install valgrind

Run your executable

You need to build your swift app in debug mode first:

swift build

Now we need to run our Swift executable through Valgrind.

/usr/bin/valgrind --tool=massif <Path-to-executable>

e.g.:

/usr/bin/valgrind --tool=massif .build/debug/Run

Generate report

This will generate a file that looks like massif.out.<number>, run this through ms_print

ms_print massif.out.<number> | swift-demangle > report.txt

Now you can check the content of report.txt which have the full report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment