User Tools

Site Tools


Create a swap file in order to save your system from OOM killer

In case you have limited resources, not having swap memory of any kind installed on your machine, means that once reached a certain limit, the OOM killer start to play, and kill processes like Kill-Bill, so… to avoid that, we can create a swapfile if we have enough disk space too.

To do so, follow the next commands

# create an empty 1G file with 1024KB block size
dd if=/dev/zero of=/root/swapfile01 bs=1M count=1024
# or... a simpler way to do it, using fallocate (if installed)
fallocate -l 1G /root/swapfile01
# give the swap file 0600 perms
chmod 0600 /root/swapfile01
# add the (im a swap file) magic to the file, so that the system knows this is a swap file
mkswap -L swap01 /root/swapfile01
# modify fstab and add this line
vim /etc/fstab
/root/swapfile01        swap                    swap    defaults        0 0
# activate the swap by loading defined swaps on /etc/fstab and other sources
swapon -a
# you can check there is swap memory now with
free -h
# or the fancy htop viewer
docu/csheet/sysadm/mem/add_swapfile.txt · Last modified: 2020/08/21 10:06 by admin