How to Set More Virtual RAM for Linux (with Swap File)

Sometimes computers freeze during development usually because our physical RAM isn’t enough for local development environment and there is a way to support our physical RAM with virtual RAM for Linux too.

By creating swap files, you can automatically move inactive pages in your RAM to these swap files when your RAM is almost full.
Since only the inactive pages are moved to this swap file, you will not lose your read-write speed by continuing to actively use your physical RAM and your computer will not freeze.

Simply put, swap files host unnecessary data from your RAM on your disk until it’s needed.

image

Step 1 : Delete your old swap file if you have one already

Some of the Linux distributions comes with swap files configured by default. We want to delete it and create a larger one.

This command will show your swap files:

sudo swapon -show

image

This command will deactive your swap file:

sudo swapoff -v /swapfile

image

This command will delete the swap file:

sudo rm /swapfile

Step 2 : Create a new Swap File

This command will create a new swap file and you can adjust it’s size by changing 16G (16GB) to something you want.

sudo fallocate -l 16G /swapfile

This command will set the wap file’s read and write permissions. Only root user should read and write.

sudo chmod 600 /swapfile

This command will let the system know you have a new swap file

sudo mkswap /swapfile

Step 3 : Activate the Swap file and Confirm It’s Working

This command will activate the swap file:

sudo swapon /swapfile

You can check it’s working with this command:

free -h

image

As you can see I have 16GB of physical RAM and also 16GB of virtual RAM. (swap memory)