It’s likely that if you don’t know how to change the Swappiness value, you don’t know what you should be changing it to, and maybe don’t know what Swap is at all, so this is actually a few questions
What is Swappiness?
Swappiness is the setting relating to how aggressively your linux system will use the Swap Memory.
What is Swap Memory?
Swap memory is a type of overflow memory that is used to prevent your system crashing when the memory required approaches or passes the size of RAM on a linux system. It is a partition of your Hard Drive or SSD that is dedicated to “act like” RAM. Remember that Hard Disk Drives are physical drives that need to mechanically spin to retrieve data. You should not use Swap as a permanent replacement for RAM. Solid State Drives are faster than HDDs, but they’re not as fast as RAM. A 128GB SSD shouldn’t be viewed as 128GB of cheap RAM.
How much Swap Memory do I need?
How long is a piece of string? The answer varies depending on a number of circumstances that your environment has. The minimum you should aim for is at least the same size as the RAM you have available. If you’ve got a simple 512MiB RAM installation, then you should have at least 512MiB HDD space. If HDD space is at a premium as well as your RAM size, then you’ll have to make considerations. For example if you’ve got 4GB RAM and a 20GB HDD (for some insane reason) then you’re not going to want to spend 4GB of your HDD on Swap. If HDD space is plentiful then go for at least the size of your RAM, and see how that works. If you’re reaching 100% of your swap memory on a regular basis, consider increasing your RAM alongside the size of your swap.
If HDD space is sparse compared to your RAM then make a reasonable swap partition, monitor it for a few days, particularly under high load, and see how the server runs.
So, what should my Swappiness be set to?
Annoyingly for anyone looking for a quick-fix it’s not as simple as just saying “60″. You need to take a few things into consideration:
Swapiness can be configured to anything between 0-100. The 0 and 60 aren’t %ges of Swap that will be used, they’re an aggression variable. A low aggression means that your system will prefer to steal pages form cache, a high aggression means that it prefers move to swap memory. Your mileage may (will) vary from system to system.
Adding to swap and taking from swap is in itself quite expensive on the system. Having said that, it’s also generally less expensive than grabbing from a (potentially external) Hard Drive.
A good number to start with is 50. Then work from there. If you find that your system is slowing, or locking up more often than you’d like, then check the status of your system in #top or #free. If the RAM usage is too high, then increase the swappiness, if the swap is too high then decrease swappiness. If you’re getting as low as 10-15 and still not seeing an improvement, then you’ll want to start looking into other issues. Equally, if you’re getting as high as 80-90 and not seeing an improvement then you’ve likely got issues elsewhere (consider checking MySQL buffers, and your actual RAM size).
How to Change Swappiness
You’ll need to have super user privileges for most of the following, and you’ll need to be able to turn off the system to reboot.
First let’s check swappiness.
cat /proc/sys/vm/swappiness 60
So mine was 60 (it’s actually the default, because I spun up the server for this tutorial). The server is an 8GB one, and it’s got a 4GB database running on it, I expect that Apache is going to take up a lot of memory, and then the actual site running will take up quite a bit too. Since in this instance I’ve got a big DB that I want kept mainly in RAM I’ll set the Swappiness quite low.
sudo nano /etc/sysctl.conf
then either find or change this line:
vm.swappiness = 60
to whatever you choose. I made it 20 as a start.
Next you’ll want to reboot the system. Once it’s back online run
cat /proc/sys/vm/swappiness 20
again and make sure it returns the number you input.