Friday, June 26, 2020

A great way to increase swap on ubuntu

The article is copied from ubuntu community as it is .

In case you don't want or you're not sure how to create a swap partition, you can create a swap file which will work in the same way as partition. Here are the steps (using terminal):
  1. Create an empty file (1K * 4M = 4 GiB).
    sudo mkdir -v /var/cache/swap
    cd /var/cache/swap
    sudo dd if=/dev/zero of=swapfile bs=1K count=4M
    sudo chmod 600 swapfile
    
  2. Convert newly created file into a swap space file.
    sudo mkswap swapfile
    
  3. Enable file for paging and swapping.
    sudo swapon swapfile
    
    Verify by: swapon -s or top:
    top -bn1 | grep -i swap
    
    Should display line like: KiB Swap: 4194300 total, 4194300 free
    To disable, use sudo swapoff swapfile command.
  4. Add it into fstab file to make it persistent on the next system boot.
    echo "/var/cache/swap/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab
    
  5. Re-test swap file on startup by:
    sudo swapoff swapfile
    sudo swapon -va
    
    Note: Above commands re-checks the syntax of fstab file, otherwise your Linux could not boot up properly.

No comments: