Stop server from swapping

m1ne

Well-known member
Hey all.

My server is using swap despite having around 50% free memory, how can I stop this?
I think MySQL is the cause, but I'm not sure. Any ideas?

Thanks.
 
Hello,

Please set these variables in /etc/sysctl.conf file:

vm.swappiness = 10
vm.drop_caches = 3
vm.vfs_cache_pressure = 50

Save the file and execute sysctl -p in your command prompt.


Kind regards,
George.
 
  • Like
Reactions: rdn
No, don't do that - it's just asking for trouble/problems

This is a non-destructive operation and will only free things that are completely unused. Dirty objects will continue to be in use until written out to disk and are not freeable. If you run "sync" first to flush them out to disk, these drop operations will tend to free more memory.

Kind regards,
George.
 
This is a non-destructive operation and will only free things that are completely unused. Dirty objects will continue to be in use until written out to disk and are not freeable. If you run "sync" first to flush them out to disk, these drop operations will tend to free more memory.
If you're going to cut n paste from kernel.org (or where ever else referenced it), then the next paragraph reads;
This file is not a means to control the growth of the various kernel caches
(inodes, dentries, pagecache, etc...) These objects are automatically
reclaimed by the kernel when memory is needed elsewhere on the system.

Use of this file can cause performance problems. Since it discards cached
objects, it may cost a significant amount of I/O and CPU to recreate the
dropped objects, especially if they were under heavy use. Because of this,
use outside of a testing or debugging environment is not recommended.
https://www.kernel.org/doc/Documentation/sysctl/vm.txt
 
Yeah, I'm thinking whatever is using swap will just crash with it disabled. I just need to find out how to stop it swapping, there's plenty of free memory.
 
Hello,

Instead of disabling swap altogether, just make sure your OS swaps very rarely. This is controlled by the vm.swappiness setting in /etc/sysctl.conf. The default setting is 60 which is too much for most situations and will cause you to start swapping even while RAM is still available. If you reduce this value, you will be able to keep the safety line of swap while only using it for emergencies. So, open the file:
nano /etc/sysctl.conf

And add this line to it:
vm.swappiness=10

If that is still too much, change the 10 to 5. Now, after you restart, you will only swap when absolutely necessary and you can simply forget about it.


Kind regards,
George.
 
I think MySQL is the cause, but I'm not sure.

If you aren't sure what the cause is, why are you trying to "fix" this? There might very well be nothing that needs to be fixed in the first place. The first step is obviously to determine what is being swapped. Unlike Windows, which swaps once the RAM is used, linux puts things into swap that are seldom used, regardless of how much RAM is in use. This frees up the RAM for more important things that actually need to be in the RAM. With 50% free RAM, I highly doubt MySQL is what's being put into swap, but you never know. Run top, then O (that's a capital oh), then p. That will sort the processes by swap.

If you want to disable swapping completely until the RAM is full, set the swappiness to 0.

I think a lot of people get caught up in the Windows mentality and think that swapping is a bad thing. In Windows it absolutely is. In linux, it's perfectly normal.
 
It is a problem though. I noticed when it starts to swap, MySQL is crippled, insert queries take much longer.
I think I'll set swappiness to 10.
 
And have you determined that MySQL is what's being swapped? MySQL usually isn't something that's going to be swapped (especially if it's being used for queries) with 50% free RAM.
 
And have you determined that MySQL is what's being swapped? MySQL usually isn't something that's going to be swapped (especially if it's being used for queries) with 50% free RAM.

No, but super slow queries made me suspect it. Either MySQL is responsible (it is the biggest RAM hog by far), or it's just being affected by it.
 
Did you run top, like I mentioned, to actually find out what is causing the swapping?
 
it's possible that Apache is playing a part in this, what are your settings and have you considered nginx as a webserver to see if that helps with performance?
 
it's possible that Apache is playing a part in this, what are your settings and have you considered nginx as a webserver to see if that helps with performance?

I use nginx for static files only.
For now I've set swappiness to 10 and adjusted some value in my.cnf (tmp_table_size & max_heap_table_size). Swap is at 0mb right now.
 
Top Bottom