2 min read

How To Create Virtual Memory on VPS Server

In this guide, you will learn how to create virtual memory on a VPS server with limited memory.

What is virtual memory?

Virtual memory is an abstraction of physical memory that allows a computer to address more memory than is physically present in the system. Virtual memory provides an address space that is larger than the addressable physical memory, allowing the computer to address more memory than is physically present in the system. Virtual memory is typically implemented as a file or section of memory that is mapped to the address space of a process.

Check if Enabled on your VPS

It is entirely possible your configuration already makes use of virtual memory. The commands below will show you how to determine whether it is enabled or not, and if it is, its size and configuration.Open a terminal or SSH/VNC to your server – these commands are all performed in a terminal or shell.Don’t forget, to make changes, you need to be root.

You can check what user you are logged in with the command whoami. If it does not respond with root or 0, you can type su to start a root shell.You can check if your droplet already has virtual memory enabled by typing the “free” command at the prompt in a terminal:

ubuntu@app1:~$ free

The “free” command shows your system’s available physical and virtual memory.

If you have virtual memory enabled already, you can skip ahead to “A Note About Swap Partitions” and then the configuration section. When enabled, the output will look like this:

ubuntu@app1:~$ free
                    total            used            free            shared  buff/cache   available
Mem:         989860      255592       97940       40004      636328      545184
Swap:       1048572       99288      949284
ubuntu@app1:~$ 

If it is not enabled, the output will look like this:

ubuntu@app1:~$ free
                    total            used        free             shared  buff/cache   available
Mem:         989860      255592    97940       40004      636328      545184
Swap:        0                  0               0
ubuntu@app1:~$ 

You can also narrow down the output with free | grep Swap. This will only show the Swap: line, total, used, and free VM. (Remember, by default, grep is case sensitive!)

ubuntu@app1:~$ free | grep Swap
Swap:       1048572       99288      949284

How To Create Virtual Memory (Swap File) on a low memory VPS Server If your VPS is low on memory, you can create a virtual memory (swap file) to help increase the amount of available memory.

Login to your VPS via SSH

Create a new file called “swapfile” in the root directory Use the command “dd” to create the file with the desired size. For example, to create a 1GB file, use the following command:

dd if=/dev/zero of=/swapfile bs=1024 count=1048576

Change the permissions of the file so that only the root user can read and write to it

chmod 600 /swapfile

Use the “mkswap” command to initialize the file as a swap file

mkswap /swapfile

Enable the swap file by using the “swapon” command

swapon /swapfile

Add the following line to the “/etc/fstab” file so that the file is automatically used as swap space when the system is rebooted

/swapfile swap swap defaults 0 0

You should now have a working virtual memory (swap file) on your VPS.