Swap Space | How to Provision Swap

When and Why to Add Swap Space

Without swap space – the below middleware will either fail to install or will behave badly once installed. These include

  1. GitLab (for businesses who must host their own Git repositories)
  2. Jira (a Java web application for collaborating on projects)
  3. Jenkins (a job runner used to continuously integrate java software).

Swap space does not come as standard when you either use Amazon EC2 cloud machines or local Virtual Machines (usually through Vagrant and Oracle VirtualBox).

How to Analyse Your Swap Space and Disks

Before adding swap space you should ascertain what you have with these commands.

the /proc/meminfo command

The cat /proc/meminfo command tells you

  1. how much of your swap cache (disk) is used
  2. how much of the swap space is free (for use)
  3. the total swap space you have (sum of the above)
$ cat /proc/meminfo | grep Swap

the /proc/swaps command

This command lists (by name) the swap disks (space) that you have.

$ cat /proc/swaps

the df -h command

df -h is the ubiquitous “disk free” command for looking at all your hard disk partitions.

$ df -h

How to Add Swap Space

Now you know what is available – you can go ahead and add your swap space. You can add these comands into a script and run it all at once ensuring that the script has administrative (sudo) privileges.

Assign 4 Gig of Swap Space

Experience has taught us that a 4Gig Swap partition does the trick for most memory hungry applications including GitLab, Jira and Jenkins.

# -- ------------------------------------------------- -- #
# -- Setting the swap disk to 4 GigaBytes (4 x 1024M). -- #
# -- ------------------------------------------------- -- #
swapsize=4096

Double Check for Idempotency’s Sake

If you’ve already allocated swap – do not do it again. For idempotency you can wrap the swap provisioning commands within a checking conditional.

# -- ------------------------------------- -- #
# -- Is the swap disk already provisioned? -- #
# -- ------------------------------------- -- #
grep -q "swapdisk" /etc/fstab

if [ $? -ne 0 ]; then
    ... not yet - so allocate it now
else
    ... yes it has - do nothing
fi

Allocate the Swap Space

This is where the rubber hits the road.


  echo 'swapdisk not found. Adding swapdisk.'
  sudo fallocate -l ${swapsize}M /swapdisk
  sudo chmod 600 /swapdisk
  sudo mkswap /swapdisk

  # -- --------------------------------------------------------------- -- #
  # -- [4 GiB allocated] - response from the making swap disk command. -- #
  # -- --------------------------------------------------------------- -- #
  # -- Setting up swapspace version 1, size = 4 GiB (4294963200 bytes) -- #
  # -- no label, UUID=3c6f7e73-dfb3-4e5e-9266-d3bc90e2d6a7             -- #
  # -- --------------------------------------------------------------- -- #
  sudo swapon /swapdisk

Append line inside /etc/fstab

sudo echo '/swapdisk none swap defaults 0 0' >> /etc/fstab

If the above command fails with permisions (due to the redirected output making it 2 commands really) you can do it manually. Just open up the /etc/fstab file and insert the line /swapdisk none swap defaults 0 0

Run Through the checks Now.

Now that it is done – it pays to run through the 3 analysis commands again to ascertain that you have indeed provisioned the swap space.

Installing using a Vagrant (VirtualBox) VM.

When creating a memory hungry middleware stack locally – provisioned with Vagrant and executed by Oracle’s VirtualBox – you need to take care to setup the machine with enough uumph.

Within the Vagrant file make sure you allocated 2Gig of RAM at least. Anything up to roughly half what your host has is a good balance. Also you can ramp up the CPUs to between 2 and 4 inclusive.

Leave a Reply

Your email address will not be published. Required fields are marked *