Skip to content

Latest commit

 

History

History
223 lines (169 loc) · 6.68 KB

howto-configure-swap-file-for-linux.md

File metadata and controls

223 lines (169 loc) · 6.68 KB

HowTo Configure Swap with a Swap File for a Linux System

Your applications consume memory. What happens when an application maxes out the memory pool available? It could destabilize your application and maybe your system.

This situation is resolved by setting aside harddrive space to serve as a "swap disk". This allows the operating system to artificially increase the RAM avialable to that application. Remember though, that swap space is not a replacement for RAM. If the system continually runs out of RAM, you don't have enough RAM (or something else is wrong). Swap is "emergency memory".

Swap disk? Swap file? Swap partition?

If you control your own build or installation process for a system, simply ensure you have adequate swap configured as a partition during that process. Almost all linuxes come with slick installation wizards now. And many/most distributions create a swap partition for you. A partition is a dedicated chunk of disk space that does nothing but swap operations. This is the most efficient swap type.

The purpose of this document, though, is to configure swap after a system has been provisioned or to configure swap after it was determined that the swap you do have is not enough. For example, many VPS cloud instances do not deploy instances that have swap already created. It's up to you to configure that or create a swap file after the fact.

You can adjust your partitions after the fact using LVM, but it is much simpler to use a swap file instead. What is a swap file? It's a file, on the file system, that acts as a dedicated chunk of storage. In the past, swap files were terribly inefficient, but now... they approach the performance of normal dedicated swap partitions.

Hey wait! Maybe I have swap already?

Open up a terminal and type this...

free -h

This is what I see on one of my systems...

              total        used        free      shared  buff/cache   available
Mem:           2.0G        485M        113M        276K        1.4G        1.3G
Swap:          3.9G         11M        3.9G

Ah ha! I have swap already, and twice the size of my RAM. I should be good to go. But maybe it didn't exist at all or was too small! If so, read on.

Decision time. How much space to allocate?

I.e., how big of a swap file to I create?

Here's the general advice:

  • Do you have far too little RAM? Buy more RAM (challenging if a laptop, I know)
  • Do you have a relative small amount of RAM? A swap file sized 2x RAM should be sufficient.
  • Do you have an overwelming large amount of RAM? 1x RAM or even less is adequate. Note: You need to really test this scenario - YMMV.
  • Are you somewhere in the middle, or aren't sure? 2x RAM

I will show you how to implement a couple options...

Creating a swap file and enabling it is easy:

# As root...
sudo su -

# Full path to your swap file.
swapfile=/swapfile

# Multiple (how many times the size of RAM?)...
m=2

Prep the swap file - BTRFS version

There's a different process if the root filesystem is BTRFS or if it is EXT4. Check with cat /etc/fstab

If BTRFS, the process is MUCH slower and more convoluted. It is what it is …
Read more about that here:
https://superuser.com/questions/1067150/how-to-create-swapfile-on-ssd-disk-with-btrfs

# BTRFS version of swap file prep
# Create the file, soon to become a swap file
rm $swapfile
touch $swapfile

# To satisfy BTRFS's demand that it NOT be copy-on-write
lsattr $swapfile
# ... should look something like:
#     ---------------------- /swapfile
chattr +C $swapfile
lsattr $swapfile
# ... should now look something like:
#     ---------------C------ /swapfile

# Do some math and fill that file with nothing but zeros ...
bs=1024
size=$(free -b|grep Mem|awk '{print $2}')
size=$(($size*$m))
count=$(($size/$bs))
# ... now fill it (this *will* take some time and bog down your computer)
#     (that nice setting of 19 will *help* not thrash your machine, but this
#      is a heavy I/O process, so go take a nap or something)
nice -n 19 dd if=/dev/zero of=$swapfile bs=$bs count=$count
ls -lh $swapfile

Prep the swap file - EXT4 version

Things are simpler and way faster with an ext4 filesystem.

# EXT4 version of swap file prep
# Size, in bytes
size=$(free -b|grep Mem|awk '{print $2}')
size=$(($size*$m))
size=$(printf "%.0f\n" $size)

# Create the swap file...
fallocate -l $size $swapfile
ls -lh $swapfile

All filesystems
Make it a swap file, turn it on, and add it to /etc/fstab

# All filesystem types
# Make it a swap file (has to have 0600 perms):
chmod 0600 $swapfile
mkswap $swapfile
ls -lh $swapfile

# Turn it on and check that it is running
swapon $swapfile
swapon --show
free -h

# Enable even after reboot
cp -a /etc/fstab /etc/fstab.mybackup # backup your fstab file
echo "$swapfile none swap defaults 0 0" >> /etc/fstab

# check that it wrote and that it is correct
cat /etc/fstab

That's it! Good luck.

Please send comments and feedback to [email protected]


Resources...