How to change swap size on Ubuntu 14.04 ?

October 2, 2014 40.3k views

Now, I am using ubuntu 14.04 with 512 RAM and Swap 512 too. I want to change my swap to 1 GB. Can anyone help me ? Should I repeat this step on this article with different size ?


1 comment
  • No, you don't have to repeat these step if you already have swapfile in root directory.
    Check swapfile
    cd /
    ls -a

    Just follow these steps:

    1. Make all swap off
      sudo swapoff -a

    2. Resize the swapfile
      sudo dd if=/dev/zero of=/swapfile bs=1M count=1024

    3. Make swapfile usable
      sudo mkswap /swapfile

    4. Make swapon again
      sudo swapon /swapfile

5 Answers

You could just delete the file and start from scratch, but if you just want to resize it you can do that as well. Just remember to disable it with swapoff first. For example:

# sudo swapon -s
Filename                Type        Size    Used    Priority
/swapfile                               file        1048572 0   -1
# sudo swapoff /swapfile 
# sudo fallocate -l 4G /swapfile
# sudo mkswap /swapfile
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=fb0aadaf-b52f-4928-b57f-c60ef7a0bd49
# sudo swapon /swapfile
# sudo swapon -s
Filename                Type        Size    Used    Priority
/swapfile                               file        4194300 0   -1
  • Thanks for answer.

    When I run

    sudo swapoff /swapfile

    I got :

    swapoff: /swapfile: swapoff failed: Cannot allocate memory

    When I delete it with :

    sudo rm -rf /swapfile 

    I got :

    rm: cannot remove ‘/swapfile’: Operation not permitted

    Why this happen ?

  • This maybe caused by insufficient free memory when turning off swap.

    You may try to create another swap file and turn it on then turn off the old swap.

    sudo fallocate -l 4G /swapfile4G
    sudo mkswap /swapfile4G
    sudo swapon /swapfile4G
    sudo swapoff /swapfile # < old swapfile
    sudo rm /swapfile
    sudo swapon -s # verify 
    # After all edit your /etc/fstab, if you use it to turn on swap when reboot, to switch /swapfile to /swapfile4G
    • this worked for me, having the issue of not being able to straight swapoff the current file without making a new one

  • I tried to delete my old swap in /etc/fstab and the code which I delete is

    /swapfile   none    swap    sw    0   0

    Then create the new swap with 1 GB, and repeat the step in the article above.
    Now, everything is better.
    Thank you for helping.

  • 2didit:

    While switching off the swap, the running apps, doesn't know to take which memory.

    Try to drop all caches from memory, but maybe first stop all main apps and then drop caches:

    echo "3" > /proc/sys/vm/drop_caches

    wait... for like a min, then

    echo "0" > /proc/sys/vm/drop_caches

    and check if you can swap off after.

    // http://www.linuxquestions.org/questions/linux-server-73/problem-with-swapoff-a-860137/

Thanks @TonyTsang this is exactly what I was looking for. With your instructions I was able to increase my swapfile size from 3GB to 6GB on Ubuntu 14.04.

Thanks for instructions & discussion.

Funny thing, I had to reduce my swap file from 8G to 4G. Gobble up too much disk space.


Have another answer? Share your knowledge.