Cart

    Sorry, we could not find any results for your search querry.

    Increasing SWAP in Linux

    When there is no more RAM available on your VPS, it can use 'swap memory' instead of RAM. We generally discourage this as it negatively impacts your VPS performance. A better alternative is to upgrade your VPS, for example, with a (temporary) RAM add-on.

    ram

    If you still want to use swap temporarily, for example, for updating a larger software package, it is possible. However, the default amount of swap memory on your Linux VPS might be insufficient. In this guide, we show you how to increase your swap memory in such a case.

    Prolonged intensive swap usage can negatively impact the hypervisor hosting your VPS. In such a case, we will set a temporary limit on your VPS's hard disk capacity and you will receive an email from us about it.


     

    Expanding Swap Memory

    It is possible to expand an existing swap file, but this will move what is stored there to your RAM. If your RAM is already full at that moment, it could lead to out of memory crashes. For this reason, we choose to add a new swap file instead of expanding the existing one. You can easily scale this up and also remove swap files again.

     

    Step 1

    Connect to your VPS via SSH or use the VPS console.


     

    Step 2 - Optional

    Before you begin, it doesn't hurt to check how much swap memory you have available and whether you are indeed fully using it. You can do this with the command:

    swapon --show

    The output looks something like this:

    swapon --show
        NAME      TYPE      SIZE  USED PRIO
        /dev/dm-1 partition 980M 10.6M   -2
        

    In this example, there is a 980MB swap partition of which 10.6MB is used, and the file name is /dev/dm-1.


     

    Step 3

    Create a new swap file in the root partition with the command:

    sudo dd if=/dev/zero of=/swapfile1 bs=1M count=4048

    Explanation

    less

    • dd: A Unix command for converting and copying files. This tool can also be used to create a full copy of your VPS, for example.
    • if= /dev/zero: uses /dev/zero as input; this is a special file in Unix systems that only returns empty data (zeros).
    • of=/swapfile1: the name of the output file to which the empty data is written.
    • bs: The size of the blocks that the file /swapfile1 consists of, here it is 1MB.
    • count: The number of blocks that the file /swapfile1 consists of. Here it is 4048, or 4GB (1MB * 4048).

     

    Step 4

    Next, convert the new file into a swap file:

    sudo mkswap /swapfile1

     

    Step 5

    The file will now warn you that the read and write permissions are incorrect (644 instead of 600). You can change this with the command:

    sudo chmod 600 /swapfile1
        

     

    Step 6

    Activate the new swap file:

    sudo swapon /swapfile1

     

    Step 7

    There is probably not yet an automatic entry in /etc/fstab for this swap file. This is necessary to ensure that you can use the swap file automatically after a reboot.

    Check if there is an entry in /etc/fstab for /swapfile1 with the command:

    cat /etc/fstab | grep /swapfile1

    If you get no output, add the necessary entry with the command below:

    echo "/swapfile1 none swap sw 0 0" >> /etc/fstab

     

    Adjusting Swappiness

    Your operating system has a setting that indicates how much 'swappiness' occurs, i.e., how often your system moves data from RAM to swap memory. This is indicated with a value from 0 to 100, where a value closer to 0 means less swapping and closer to 100 means more swapping. Check the current value with the command:

    cat /proc/sys/vm/swappiness

    Want to change the value? Add the new value to /etc/sysctl.conf with the command below and restart your VPS. Replace 10 with the value you want to apply

    echo "vm.swappiness=10" >> /etc/sysctl.conf

     

    Disabling Swap

    If you want to prevent your VPS from using swap memory and negatively impacting its performance without you possibly realizing the cause, you can completely disable swap with the following commands:

    sudo swapoff -a
     sed -i '/ swap / s/^/#/' /etc/fstab

    By commenting out the swap entry in /etc/fstab, swap will not be used even after a VPS restart.


    This concludes our article on adjusting swap memory in Linux.

    If you have any questions based on this article, do not hesitate to contact our support department. You can reach them via the 'Contact us' button at the bottom of this page.

    Need help?

    Receive personal support from our supporters

    Contact us