How to add Swap on Linux/Android/Docker stack

UPDATE: swap is now pre-configured on all stacks since: Weekly Virtual Machine / Stack updates - 2018.02.24 (added swap to Linux/Android stacks, the Mac ones already had swap configured).

You can of course still reconfigure it via the instructions here, but in general there’s no reason to do that anymore.


Currently the Android / Linux / Docker stack comes with no swap configured at all. There are two reasons for this:

  1. The standard Ubuntu Server installer does not configure a swap by default, and our policy is to deviate as little as possible from the “default” install of the operating systems.
  2. The right Swap amount might depends on the actual project, and it’s actually quite simple to configure if required.

So, if you need Swap (“additional RAM” / virtual memory - but keep in mind swap as RAM is way slower than actual RAM - Swap - Debian Wiki) for some reason you can simply configure it with a Script step. It takes only about 2-3 seconds to create 4 GB swap.

Just drop in a Script step into your workflow, as the very first step in the workflow, with this content, to create 4 GB swap (if you need more or less swap just change the sudo fallocate -l 4G /bitrise/swapfile line):

#!/usr/bin/env bash
set -ex

# create a swap file
sudo fallocate -l 4G /bitrise/swapfile
sudo chmod 600 /bitrise/swapfile

# now make this file a swap file
sudo mkswap /bitrise/swapfile
# and activate it as swap
sudo swapon /bitrise/swapfile

That’s all, no other steps required :wink:

Just for fun here’s an alternative version with additional debug logs, “just in case”:

#!/usr/bin/env bash
set -ex

# check swap
sudo swapon --show
# and free memory / RAM
free -h

# create a swap file
sudo fallocate -l 4G /bitrise/swapfile
ls -lh /bitrise/swapfile
sudo chmod 600 /bitrise/swapfile
ls -lh /bitrise/swapfile

# now make this file a swap file
sudo mkswap /bitrise/swapfile
# and activate it as swap
sudo swapon /bitrise/swapfile

# it should show up now here in the swap stats
sudo swapon --show
# and in the free memory / RAM stats
free -h

This one prints the swap config (swapon --show) and the available memory (including both swap and RAM - free -h) before it would create the swap, then creates the swap (with some debug logs along the way) and prints the swap and RAM/memory infos again at the end, after the swap is created.

The first version should be enough if you don’t want to do anything else other than just creating the swap, the second one might be useful for those who want to “explore” additional options :slight_smile:

If you have any questions just let us know!
Happy Building!

Why sudo? Linux stacks uses root by default so everything should work without sudo.

2 Likes

True, sudo is not required on the Linux / GCE stacks.

I think sudo was left in the script to make it more “universal” (we used this in other environments). But good catch, no need for sudo - although it’ll work both with and without sudo in our Linux environments :slight_smile:

2 Likes

@viktorbenei
How much swap can I try to add to the most basic $50 plan? My builds take longer than expected and I want to try out different swap values.

Swap is now preconfigured on all stacks, so it is no longer required to do it yourself.

@coltonidle the amount of swap will not make your builds faster or slower. Swap is pretty much just “slow RAM” from a user perspective. Smaller swap might make the system to write to swap less and prefer RAM more, but if you’re close to the RAM limit it doesn’t really matter.

Swap is mainly used to not crash the build when the build (e.g. compilation etc) eats up all the available RAM. The only way to make it actually faster if you’re RAM constraint is to have more RAM - you can get 2x RAM via the Elite plan.

Let me know if you’d have any questions! :wink:

Thanks for the response. I’m looking to see if I can speed up my companies android app at all. It’s pretty slow, the gradle command section takes ~10 minutes but if I copy that command into my local machine (MBP 13) it takes about 4 minutes.

I was just curious if theres anything I can do to speed things up.

I would pay double for the Elite machine, but I can’t pay 6x what I’m paying. I would need to move from Team account to Org account. Elite machines for a Team account is something I’m interested in! :grinning:

Thanks for the help

Any time :wink:

Is 10 mins with Caching or without? Feel free to ping our support via email or the onsite chat and we’ll check your config and try to see if there’s anything which could be made faster :wink:

Will do. Thanks @viktorbenei

1 Like