SWAP in Linux

In OS Linus, the random access memory (RAM) is divided into partitions named pages. Swapping (filling) is a process during which the memory pages are copied into a specially for this configured drive partition named swap space (swap partition that can be both a file and a hard drive partition) for RAM release. The cumulative size of the physical memory and the swap partition is the volume of the existing virtual memory.

Swapping is necessary for the following reasons:

  • When the system needs more memory (i.e. the application or process requests more memory from the system) than is now free in RAM, the core unloads (rolls out) the least used pages and spends the unloaded memory to the ongoing application or process. 
  • A considerable number of pages used by programs at the launching stage are only used during the initialization and never more. Correspondingly, the system can swap these pages, so unloading (rolling out) RAM.

The disadvantages of SWAP:

  • Compared to RAM, the work with a hard drive goes much slower. For the estimation of time needed on reading/recording in RAM, the system uses nanoseconds, while for the hard drive, milliseconds are used, i.e. the same processes on a hard drive take tens of thousands more time than in RAM. So, the more pages are being swapped, the slower your system is working.


The lack of available memory is often the reason why the database suffers first. You can face such problems as:

  • Constant site drop: Constant database connection failed – this means, for example, that MySQL dropped;
  • In studying logs /var/log/mysql.log we notice the error InnoDB: Fatal error: cannot allocate memory for the buffer pool. It means that the allocated random access memory is not enough for the database to create the buffer.

If a file or a swap partition is stored on an SSD, we need to understand that data searching and reading happens through the random access memory, which in turn raises the response time.

So, if you face a lack of random access memory, we recommend you shift to a higher tariff plan. 

Verification of SWAP in the system:

swapon -s

Filename Type Size Used Priority

/swapfile file 40956 40956 -1

Extra verification with a command: 

free -m 

total used free shared buff/cache available

Mem: 488 160 7 40 320 259

Swap: 39 39 0

We see that SWAP is ready and will be used if necessary.

In case of absence, you can create a file for SWAP.

dd if=/dev/zero of=/swap.file bs=1M count=512

This command will create a file /swap.file sized 512 megabytes. For raising this figure, change count 

Set the rights to it so that no one could record or read the file except the owner – root

chmod 600 /swap.file 

Format the file for swap

mkswap /swap.file 

Edit the file /etc/fstab 

For swap to switch on with download, you should add it into /etc/fstab

Add the line

/swap.file swap swap defaults 0 0

Then you can reboot the server and check the availability of swap with the command free -m



Blog