Configure Virtual Memory Swap File On A Vps
Table of Contents & Preface
- Introduction - Requirements and Why
- Pros & Cons - machine
- Check if Enabled on your VPS
- Swap Partitions, Swap Files, & Disk Images
- Creating the Swap File
- Enabling and Disabling Swap
- Configuration, Priority and sysctl settings
This article will cover the pros and cons of using virtual memory or a swap file (paging), determining if your machine already uses virtual memory or paging, the differences between a swap partition and a swap file, information on how to create a swap file, and how to configure the system's "swappiness" (how likely it is to use virtual memory as well as determining the appropriate size to use).
A quote from the Paging (swap file) article on Wikipedia: (emphasis on the second paragraph for its clear explanation)
"In computer operating systems, paging is one of the memory-management schemes by which a computer can store and retrieve data from secondary storage for use in main memory. In the paging memory-management scheme, the operating system retrieves data from secondary storage in same-size blocks called pages. The main advantage of paging over memory segmentation is that it allows the physical address space of a process to be non-contiguous. Before paging came into use, systems had to fit whole programs into storage contiguously, which caused various storage and fragmentation problems."
"Paging is an important part of virtual memory implementation in most contemporary general-purpose operating systems, allowing them to use disk storage for data that does not fit into physical random-access memory (RAM)."
Although swap is generally recommended for systems utilizing traditional spinning hard drives, using swap with SSDs can cause issues with hardware degradation over time. Due to this consideration, we do not recommend enabling swap on F(x) data cloud or any other provider that utilizes SSD storage. Doing so can impact the reliability of the underlying hardware for you and your neighbors.
If you need to improve the performance of your server, we recommend upgrading your machine. This will lead to better results in general and will decrease the likelihood of contributing to hardware issues that can affect your service.
Introduction - Requirements and Why
What is it and why would I use it?
Whether you have a 512mb machine or a 8gb machine; Arch, Fedora, CentOS, Debian, or Ubuntu; applications or servers/daemons may need more memory (or sometimes more memory allocated) than you have physically. More specifically in our case, what has been allocated to the virtual server to get the job done.
If you're dealing with any production server, you need to know that if virtual memory is not enabled and your system has no more free memory...then if a program or service - perhaps your web server - needs to allocate more memory, it will fail! Depending on your platform and configuration, this can result in many undesirable or unstable conditions including other applications (i.e. other processes than the one asking for memory) being forced to close to free the needed memory, to it failing and crashing the program - or whole server - entirely.
Because of this, I personally recommend anyone, on nearly any system - be it a machine, dedicated server, your Windows PC or Mac or even your Android tablet or phone - should have at least a small amount of virtual memory enabled.
How it Works
Virtual memory allows your system (and thus your apps) additional virtual RAM beyond what your system physically has - or in the case of machines, what is allocated. It does this by using your disk for the extra, 'virtual' memory and swaps data in and out of system memory and virtual memory as it's needed.
You should be aware that reading/writing from disk (even F(x) data cloud's lightning fast SSDs) is at least several times slower than reading/writing from real system RAM. While virtual memory will give you wiggle room, allow more applications/servers to run on one machine and guard against out-of-memory errors, it is not a replacement solution in cases where you actually need more memory/to upgrade your virtual server.
While the contents of this article may seem trivial to seasoned admins, I feel it is valuable information for anyone using F(x) data cloud's hosting services, especially those new to F(x) data cloud, VPS systems in general, or to managing their own servers.
The requirements are pretty simple and this technique should work on all distributions and machine types - in fact, it will even work on your Android phone or tablet (if you have root & busybox installed).
- a machine or virtual server, powered on (or a dedicated server, a linux-based system, etc.)
- root terminal access (ssh, vnc, local)
- the commands free, swapon, swapoff, dd, mkswap will be used, and these all should be available on any platform you use with your machine.
Most platforms automatically use and manage virtual memory and will create either a special swap partition or a file on the system partition automatically during installation with the size usually being based on, or a multiple of, available system RAM, eg. 1024mb swap for 512mb RAM.
This isn't always the case with virtual servers, including DigialOcean's machines.
Pros & Cons - machine
F(x) data cloud's machines use SSDs (solid state disks) which are several times faster than regular hard drives and do not suffer from low seek times (caused by a hard drive's head having to physically move across the disk to read data) and low IO requests per second. SSDs can read from multiple areas simultaneously while hard drives can generally only read from one area at a time.
While it is never a good idea - especially with web, mail, db servers - to rely heavily on virtual memory, DO's SSDs help using virtual memory be less painful and more logical.
- Protection against OOM (out of memory) errors, crashes, memory-related system unpredictability/instability.
- Increases available memory to the system and allows more programs to be run concurrently & more safely
- DigtalOcean's SSD storage reduces VM-associated lag and thrashing while increases paging response times
- Thrashing is a possibility: when the system is busy and actively using on average more memory than is available physically, the VM system is forced to continually 'swap' program data to and from disk and in and out of RAM as needed. We have probably all seen the result of this in a Windows PC slightly overloaded with the hard drive seemingly grinding away endlessly. It's unbearably slow and not fun. It can, however, be avoided with correct configuration and this is one con that is not as bad here - again due to the speed of F(x) data cloud's SSDs.
- Uses disk space, generally depending on system memory. If your machine has 512mb, I recommend using 512mb-1.5gb for swap; however that is 512mb-1.5gb less space you will have available on your machine's disk.
- It is generally recommended and preferred to use a dedicated disk partition for swap; however this is not possible in a machine and we must use a swap file/disk image instead.
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, it's 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 machine already has virtual memory enabled by typing the "free" command at the prompt in a terminal:
[email protected]:/# 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:
[email protected]:/# free total used free shared buffers cached Mem: 361996 360392 1604 0 1988 54376 -/+ buffers/cache: 304028 57968 Swap: 249896 0 249896 [email protected]:/# _
If it is not enabled, the output will look like this:
[email protected]:/# free total used free shared buffers cached Mem: 361996 360392 1604 0 2320 54444 -/+ buffers/cache: 303628 58368 Swap: 0 0 0 [email protected]:/# _
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!)
[email protected]:/# free | grep Swap Swap: 249896 0 249896 [email protected]:/# _
Swap Partitions, Swap Files, & Disk Images
A Note About Swap Partitions
Generally, for Linux-based systems, it is preferred to have a dedicated swap partition on your hard disk. Most systems will automatically do this during a normal installation, and if your swap is already configured, it may very well be possible it is setup with a partition. Unfortunately, other than configuring the below sysctl settings, resizing swap partitions is outside the scope of this article.
You can however still use the method presented here to add a swap file and increase available virtual memory above and beyond the partitioned swap area. But if a swap is already configured and managed by the system, additional virtual memory may be unnecessary and it may be better to leave the default settings unless you know what you are doing.
What is a Swap File?
Instead of using a dedicated partition, some systems (especially Windows) will store their virtual memory in a special file. This is also an option when partitioning is not possible or feasible. On Linux, this file is actually a disk image.
What is a Disk Image?
A disk image normally includes files and information about those files, as well as the filesystem that contains them.
Examples include your machine's snapshots (which I like to call snowflakes - frozen machines), a function which creates a disk image of your entire machine for backup, duplication, migration, etc.
Another prime example is most Linux distributions (or distros) come as disk images, usually in .iso format, which allows you to either mount them or burn them to a disk.
Whatever way you use an image, the important thing to remember is that they store the filesystem information, and as such they can be used just like the filesystem they were imaged from - or recreate it entirely.
Creating the Swap File
In the case of a machine, we cannot partition our storage, so we need to use a disk image. *NIX operating systems use a dedicated filesystem for swap, this is why as I said above that using a partition is preferred. What we can do is create an empty disk image sized according to required virtual memory, initialize it with a swap filesystem, then turn it on.
First, you need to decide where to put this file. You need to have enough space free on the partition you put it on for whatever amount of MB you use for swap, eg. 512mb of swap creates a ~512mb swap file.
You can use the command df -h to view your mounted partitions and filesystems, as well as their sizes & free space.
I recommend placing this file in /var and calling it "swap.img". We will cd to /var and create this file, then set its permissions to 600.
NOTE: It is important for security to set the file permissions to 600 so no other users can read the file directly, otherwise system memory could be read or worse.
[email protected]:/# cd /var [email protected]:/var# touch swap.img [email protected]:/var# chmod 600 swap.img [email protected]:/var# _
Now we will size the file. Sizing is important, and the best size will vary depending on your system and use case.
In general, I recommend 1-2x the available system RAM. So, if you have a 512mb machine, use 512mb-1gb swap. If you have a 1gb machine use 1gb-2gb swap, etc. This is not a hard and fast rule, for example if you have a 4gb machine it may be best to use little (512mb) or no swap at all.
It entirely depends on your use, but these instructions are tailored for a 512mb machine. We will use the command dd to fill our swap file with zeroes or nothingness to stretch it to the size we need. In this case we are using 1gb or 1024mb. This may take a minute.
[email protected]:/var# dd if=/dev/zero of=/var/swap.img bs=1024k count=1000 1000+0 records in 1000+0 records out 1048576000 bytes (1.0 GB) copied, 4.0868896 s, 253 MB/s [email protected]:/var# _
Preparing the Disk Image
And here, we will initialize the swap filesystem.
[email protected]:/var# mkswap /var/swap.img Setting up swapspace version 1, size = 1020 GiB no label, UUID=72761533-8xbe-436l-b07e-c0sabe9cedf3 [email protected]:/var# _
Once that's done, it's ready for use!
Enabling and Disabling Swap
Enable your Swap File
We will use swaponto enable it. Upon success there will be no output, but you can check with
[email protected]:/var# swapon /var/swap.img [email protected]:/var# free total used free shared buffers cached Mem: 503596 478928 24668 0 38832 102384 -/+ buffers/cache: 337712 165884 Swap: 1048572 1780 1046792 [email protected]:/var# _
You can use
swapoff /var/swap.img to turn it off.
Enable your Swap File During Boot
Note that swapon only enables the file for your current boot; if you reboot it will not come back online unless you either script your
swapon to run at boot, or change your
/etc/fstab which in most cases is much easier and is the method we will use here.
All you have to do is add a line to your
/etc/fstab file to make it ready at boot. Be careful! This file can break your system if it isn't formatted correctly or if it's overwritten. If you put your swap.img in /var, you can copy/paste the below command with no issue. (If you type it, make sure there are two > symbols, using one will overwrite the file instead of appending a line at the end.
[email protected]:/var# echo "/var/swap.img none swap sw 0 0" >> /etc/fstab [email protected]:/var# _
Configuration, Priority and sysctl settings
Once your swap space is online, there is not much that needs to be configured. You could even stop here, if you aren't interested in the nitty gritty details - once it's enabled generally it will work just fine for most setups.
If you are going to be using swap files or partitions spanning multiple devices and device types (unlikely on a machine), you may want to order the priority for each of these swap areas, either using the faster or more idle storage over slower and/or busier storage. In most cases, you can specify the priority as a parameter to swapon or in your
The system will use swap areas of higher priority before using swap areas of lower priority.
swaon -p Example:
[email protected]:/var# swapon -p 100 /var/swap.img [email protected]:/var# swapon -p 10 /mnt/SecondDrive/swap.img
/etc/fstab priorities can be set using the
pri= parameter, like this:
/var/swap.img none swap defaults,pri=100 0 0 /mnt/SecondDrive/swap.img none swap defaults,pri=10 0 0
sysctl settings (and sysfs)
We will use the command sysctl to change settings dedicated to the Linux virtual memory manager.
There is only one setting I would recommend changing:
vm.swappiness. This setting tells the Linux kernel/VM handler how likely it should be to use VM. It is a percent value, between 0 & 100.
If you set this value to 0, the VM handler will be least likely to use any available swap space, and should use all available system memory first. If you set it to 100, the VM handler will be most likely to use available swap space and will try to leave a greater portion of system memory free for use.
I personally recommend using a value of 30%, this should be a happy medium between swapping and system memory. Please note that this value is more of a target goal than a hard rule.
Below is an example on how to modify sysctl settings. For more info, you can type
sysctl --help or
[email protected]:/var# sysctl -w vm.swappiness=30 vm.swappiness = 30 [email protected]:/var# _
You can also run
sysctl -a to list ALL sysctl options (not just VM) or
sysctl -a | grep vm..
Or, to view a single setting (you can change the key name after the grep command for each setting):
[email protected]:/var# sysctl -a | grep vm.swappiness vm.swappiness = 30 [email protected]:/var# _
There are many other settings for the Linux VM (vm.*) - though they are outside of the general scope of this article and I generally recommend not changing their settings. I have, however, included a small list of options at the end of this article (in the APPENDIX) that may be worth tweaking or learning about and I added my recommended values (in brackets) for those options.
The Linux kernel website has a full list of options and their use, though not every kernel may implement all options. At the time of writing, this document is already slightly out of date but by no means useless or depricated. Here's the link: https://www.kernel.org/doc/Documentation/sysctl/vm.txt.
Please note that some of the descriptions in that document refer to the settings as 'files' and that is because they are also accessible/configurable through Linux's SysFS (Wikipedia) system.