Skip to content

Instantly share code, notes, and snippets.

@swizzlr
Created November 15, 2017 19:51
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swizzlr/0ef030606d1bcc727a8ce67582e3ac63 to your computer and use it in GitHub Desktop.
Save swizzlr/0ef030606d1bcc727a8ce67582e3ac63 to your computer and use it in GitHub Desktop.
Profiling Swift Apps on Linux

Profiling Swift in Linux

In order to profile memory leakage/heap growth in Swift on Linux, you need to a) run it on Linux b) use a profiling tool.

Docker and Valgrind to the rescue.

Installing Valgrind in a Linux container

We're using the official Swift image here: FROM swift:4.

The latest version of Valgrind that can be installed on Ubuntu 16.04 using the system package manager, apt-get, is from 2015. Unsurprisingly, if you have a machine with a CPU more modern than that, it's likely Valgrind won't support the latest instruction sets.

Building Valgrind from source...

... is remarkably painless. Either inside your container, or your Dockerfile, you'll need to apt-get install automake libc6-dbg. Then, based on this webpage, you want to:

git clone git://sourceware.org/git/valgrind.git &&\
	cd valgrind  &&\
	./autogen.sh &&\
	./configure --prefix=/usr/local &&\
	make         &&\
	make install 

Double check you're on the latest version by running valgrind --version. For me, it was 1.13.GIT.

Using Valgrind with Swift

This is the easy part. Just ensure you've made a debug build, and then run valgrind --tool=TOOLHERE .build/debug/.../YourExecutable. For more info on using massif, the super awesome heap profiler, check out this page: http://valgrind.org/docs/manual/ms-manual.html

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