WHAT'S NEW?
Loading...
Showing posts with label swap. Show all posts
Showing posts with label swap. Show all posts
Swap is space on the hard disk that is reserved to use as virtual memory. When a Cloud Server runs out of memory the Linux kernel moves inactive processes into swap to make room for active ones in working memory.

In this example we will create 2GB swap file on external disk /media/hhd/

dd if=/dev/zero of=/media/hhd/2GB.swap bs=2048 count=1048576 status=progress

This command will create a file of size count*bs bytes, which in the above case will be 2GB


This process take a lot of minutes!

Format the swap file with the command:

mkswap /media/hhd/2GB.swap

add the file to the system, enable the swap file immediately but not automatically at boot time:

swapon /media/hhd/2GB.swap

After adding the file to the system, add this line in /etc/fstab (to edit /etc/fstab run this command nano /etc/fstab). The next time the system boots, it enables the new swap file:

/media/hhd/2GB.swap none swap sw 0 0

After adding the new swap file and enabling it, verify it is enabled by viewing the output of the command cat /proc/swaps or free

The Linux kernel provides a tweakable setting that controls how often the swap file is used, called swappiness. A swappiness setting of zero means that the disk will be avoided unless absolutely necessary (you run out of memory), while a swappiness setting of 100 means that programs will be swapped to disk almost instantly. Most Linux system comes with a default of 60, meaning that the swap file will be used fairly often if the memory usage is around half of my RAM. You can check your own system's swappiness value by running:

cat /proc/sys/vm/swappiness

I'd like to turn that down to 10 or 15. The swap file will then only be used when my RAM usage is around 80 or 90 percent.

You can change the value while your system is still running with:
sysctl vm.swappiness=10
You can also clear your swap by running swapoff -a and then swapon -a as root instead of rebooting to achieve the same effect.

Reboot the server and after rebooting chmod the file to take effect.

chmod 600 /media/hhd/2GB.swap

We can check the swap file by using

swapon -s

You successfully created a swap file in Linux.
Some programs requires, at peak, more than 1GB of RAM in order to run stable. To ensure a stabile behavior on a Raspberry Pi it is recommended to set a swapfile with 2048 MB to buffer these peaks.

To create a swapfile on your Raspberry Pi (Raspbian) you can use the following commands:

1) sudo su -c 'echo "CONF_SWAPSIZE=2048" > /etc/dphys-swapfile'

2) sudo dphys-swapfile setup

 After creating the swapfile, you have to activate it once by using this command:

3) sudo dphys-swapfile swapon

Now your Raspberry PI has enough SWAP in place if its needed, and will not go down because of RAM overload. You can set up swap space on an external HDD.

You can edit the /etc/dphys-swapfile config file to make the changes, then reboot.

Something like this (assuming you have created a partition and set your hdd to mount at /media/sda1 - change that to suit):

CONF_SWAPFILE=/media/sda1/swapfile
CONF_SWAPSIZE=2000

That will create and activate a swapfile called /media/sda1 of 2000MB at the next reboot.