Swap Disk Size

What is swap?

Swap space is the area on a hard disk which is part of the Virtual Memory of your machine, which is a combination of accessible physical memory (RAM) and the swap space. Swap space temporarily holds memory pages that are inactive. Swap space is used when your system decides that it needs physical memory for active processes and there is insufficient unused physical memory available. If the system happens to need more memory resources or space, inactive pages in physical memory are then moved to the swap space therefore freeing up that physical memory for other uses.

Note that the access time for swap is slower therefore do not consider it to be a complete replacement for the physical memory. Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap files.

Why do I need swap?

Memory Consuming Programs

Sometimes, a large program can make the entire system need extra memory. A significant number of the pages used by these large programs during its startup may only be used for initialisation and then never used again. The system can swap out those pages and free the memory for other programs or even for the disk cache. In these cases, swap will be used to help the system handle any extra load.

Unforeseeable Circumstances

Unforeseeable events can and will happen (a program going crazy, some action needing much more space than you thought, or any other unpredictable combination of events). In these cases, swap can give you an extra delay to figure out what happened, or to finish what you are working on.

Optimising Memory Usage

Since mechanical hard drives are considerably slower than RAM (SSD - Solid State Drive - storage is not as slow as physical drives, but still slower than RAM), when you need a file the Linux kernel reads the file into RAM and keeps it there, so that the next time you need it, it is already in RAM and data access is much faster. The portions of RAM that accelerate disk read are called "cached memory." You will notice that they make a huge difference in terms of responsiveness. The Linux kernel automatically moves RAM reserved by programs--but not really used--into swap, so that it can serve the better purpose of extending cached memory.

Optimising Swap Performance

Because swap space uses a disk device, this can cause performance issues in any system that uses swap space significantly because the system itself may also be using the same disk device at the same time that it is required for swap operations. One way to reduce this problem is to have swap space on a different physical drive so that the competition for that resource is either reduced or eliminated.

How much swap do I need?

As a base minimum, it's highly recommended that the swap space should be equal to the amount of physical memory (RAM). Also, it's recommended that the swap space is twice the amount of physical memory (RAM) depending upon the amount of hard disk space available for the system. The only downside to having more swap space than you will actually use is the disk space you will be reserving for it.

Example Scenarios

Low RAM and low disk space

With 512 MiB RAM and 30 GB hard disk, use 512 MiB for swap since RAM is very low.

Low RAM and high disk space

With 512 MiB RAM and 100 GB hard disk, use 1 GiB for swap since RAM is very low and hard disk space is plentiful.

High RAM and low disk space

With 2 GiB RAM and 30 GB hard disk, use 1 GiB for swap since hard disk space is very low.

High RAM and high disk space

With 2 GiB RAM and 100 GB hard disk, use 2 GiB for swap since hard disk space is plentiful.

  • 2 Users Found This Useful
Was this answer helpful?

Related Articles

cPanel Server Time

Setting the server timezone in cPanel is very easy. Having the correct server time helps when...

Dovecot Is Not Running on cPanel

Quite a common error with Dovecot running on Xen hypervisors, especially for clients who use...

mySQL Errcode: 24

The mySQL error: Errcode: 24 is an indication that the number of files that mySQL is allowed to...

CentOS Change Hostname

One of the most common changes to CentOS servers is modifying the hostname. Hostnames are...

Log Slow MySQL Queries

MySQL can often use excessive CPU cores or time out unexpectedly. The slow-query-log can assist...