The purpose of this article is to show you how I overcame an out-of-memory issue while building the production version of my Angular web application on my Ubuntu server by adding swap space.
Please keep in mind that building the Angular production bundle using another cloud providers resources (like building Angular on an AWS EC2) can get expensive fast. In light of that I figured out a way to automatically build and deploy Angular production builds essentially for free on GitLab.
While Angular CLI was building the production build on the server, the machine returned a
process out of memory error message. Here is what the stack trace looked like on the terminal:
FATAL ERROR: Committing semi space failed.
Allocation failed - process out of memory
1: node::Abort() [ng build --prod --aot]
At the time of writing this post this was my technology stack:
• Ubuntu 16.04.5 LTS (xenial)
• Node 8.12.0
• Angular CLI 6.1.3
• Angular 6.1.2
After some doing some online research I found that a solution to this problem was to add additional swap space on my server. In this post, I will show you how I added swap space to my Ubuntu 16.04.5 LTS.
What is Swap Space?
Swap space is essentially emergency RAM that that sits on your hard drive and is utilized whenever you’re running your RAM close to max capacity. Sometimes large programs make the entire system need extra memory. Swap space can be a dedicated partition of your hard drive, a swap file, or a combination of both. The steps below will show you how to add more swap space to your machine.
Click here for the Official Ubuntu Swap Faq
Checking System Swap Information
First, we will check out our system to see if there are any existing swap configurations with the following:
$ sudo swapon --show
If the machine does not return an output then your system does not swap space available.
You can verify that there is no active swap using the
$ free -h
If you see a row of
0B‘s in the Swap row, no swap is active on the system.
Check Available Hard Drive Space
The most common way of allocating space for swap is to use a separate partition devoted to the task. However, altering the partitioning scheme is not always possible. We can just as easily create a swap file that resides on an existing partition.
Before we do this, we should check the current disk usage by typing:
$ df -h
The device under
/dev in the output is our disk in this case.
For machines with less than 1 GB, it is highly recommended that the swap space should be at bare minimum equal to the amount of RAM. On top of that, it’s also recommended that swap space is at maximum, twice the amount of RAM depending on the currently available disk space. If need more swap space than twice your RAM then you should upgrade your server or add more RAM.
Create a Swap File
Let’s create a file of the swap size that we want to be called
swapfile in our root directory.
My server had 512MB of RAM so for this example I will create a 1GB file with
fallocate. Please adjust the file size to meet the needs of your own server.
More information on fallocate
$ sudo fallocate -l 1G /swapfile
Once the swap file is created we can verify that the correct amount of space was allocated with the following command:
$ ls -lh /swapfile
$ -rw-r--r-- 1 root root 1.0G Oct 02 12:15 /swapfile
Enabling the Swap File
With the swap file available we need to tell the machine to leverage the extra space available in the swap file. First, we need to set the swap file permissions to 600 to prevent other users from being from being able to access or modify the file.
$ sudo chmod 600 /swapfile
Format the file as swap with mkswap:
$ sudo mkswap /swapfile
More information on mkswap
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=41e86209-3802-424b-9a9d-d7683142dab7
After marking the file as swap space, we can enable the swap file allowing our system to start utilizing it:
sudo swapon /swapfile
We can verify that the swap space is available by typing:
$ sudo swapon --show
The terminal should return an output like the following:
NAME TYPE SIZE USED PRIO
/swapfile file 1024M 0B -1
Enable the Swap File Permanently
If we reboot our server at the current moment, the machine won’t keep any of the changes we’ve made to the hard drive partition. To make the changes permanent we will have to copy our swap to the
$ sudo cp /etc/fstab /etc/fstab.bak
Last we must add the swap file details to
/etc/fstab to make it available at boot up permanently.
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Aside from this one case, there are many other reasons that one would need swap space which is included but not limited to memory consuming programs, hibernation (suspend-to-ask), unforeseeable circumstances, or optimizing memory usage. Please let me know if I could improve this post by making anything more clear!