Quick way to add swap – Ubuntu


One of the quickest way to avoid out of memory errors is by adding some swap space. Swap is an area on hard drive where operating system can temporally store data that it can no longer hold in RAM.

Use following way to add additional swap space in Ubuntu:

First check about swap space with free command:

free -m

Lets say if we need 10GB of swap, for this first create a 10GB file with following command:

sudo fallocate -l 10G /swapfile

View details about created swapfile:

ls -hl /swapfile


-rw-r–r– 1 root root 10G May 19 06:54 /swapfile

Change permissions of swapfile:

sudo chmod 600 /swapfile

Set swap space with following command:

sudo mkswap /swapfile

Check for new swap space:

sudo swapon -s


Filename                Type        Size    Used    Priority
/swapfile                               file        10485756    2700    -2

Make swap file permanent by adding it in /etc/fstab

/swapfile none swap sw 0 0

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s