linux server optimization: swappiness

Don´t be shy, Linux is fun! =)
Post Reply
User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

linux server optimization: swappiness

Post by bad_brain »

today I was finally annoyed enough to get behind the swap usage on my servers....they made no sense, because there was always enough free RAM available but still swap space was used (usually when doing the daily database backups). and swap space usage is never good, because compared to data stored in RAM the disk activity of swap is of course extremely slow and causes i/o waits which again cause CPU load.

sooo.....I found out that by default the swappiness of Debian, and Debian-like distros like Ubuntu, is set to 60, which is a pretty high value when you are running a system with a decent amount of RAM (I would say 4 GB and more). because a value of 60 means: "begin to swap when the free RAM is lower than 60% of the total RAM"...and that makes only sense on systems with a really small amount of RAM (like 512 MB or something).

to find out your swappiness:

Code: Select all

cat /proc/sys/vm/swappiness


you can change it on the running system by:

Code: Select all

sysctl vm.swappiness=10
where 10 means "begin to swap when free RAM is less than 10% of total RAM", you might have to adjust that, depending on your system.

if you have a system with a really good RAM equipment you can even turn it completely off:

Code: Select all

swapoff -a
the same command can be used to write the swap space back into RAM, simply by disabling swap and then enabling it again (disabling can take a bit, depending on the amount of memory that has to be read from HDD and written into RAM):

Code: Select all

swapoff -a
swapon -a
personally I wouldn't completely disable it though, a swappiness of 10 is low enough to not write into swap space with having still a lot of free RAM idling, but it also gives you a bit of a safety net in case you really run out of RAM for what reason ever....it gives you at least a longer time frame to start counter-measures because the system will not freeze up instantly as it would do with swap completely disabled.

P.S.
one thing I haven't figured out yet is if the cache space also counts as "used"...because Debian automatically assigns idling RAM slowly as cache space (which doesn't mean it's really used, it just COULD be used IF needed for caching).
free says it IS labeled as used, but it also labels it extra as cache space...so I am not 100% sure about that:
serv4:~# free -m
total used free shared buffers cached
Mem: 7753 7567 185 0 276 2252
-/+ buffers/cache: 5039 2713
Swap: 1951 0 1951
seems I'll have to read some man-pages...:-k
Image

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: linux server optimization: swappiness

Post by ayu »

Oh nice, I should play around a bit with this on my Minecraft server :-k
"The best place to hide a tree, is in a forest"

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

Re: linux server optimization: swappiness

Post by bad_brain »

go for it! of course it's not having a stunning "whoah...system so fast now!!" effect, but it definitely makes the server perform better under stress because it's not starting to swap unnecessarily early.

and it seems I was right, and the idle RAM assigned as cache/buffer is obviously labeled as "used", which doesn't make too much sense but which also isn't a real issue because once the swappiness limit is reached it assigns some cache/buffer RAM back to free RAM again, which is still resulting in a much lower swap usage because it happens later and less often:
Mem: 7939164k total, 6467540k used, 1471624k free, 63328k buffers
Swap: 1998832k total, 4744k used, 1994088k free, 1077312k cached
usually my swap usage was between 100-400MB caused by the daily database backups and the resulting mysql load, now it's just 4MB which is nothing.
Image

Post Reply