1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Stop server from swapping

Discussion in 'Server Configuration and Hosting' started by Solidus, Jul 30, 2015.

  1. Solidus

    Solidus 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.
     
  2. Solidus

    Solidus Well-Known Member

    Swappiness is set to 60 btw.
     
  3. Set3sh

    Set3sh Active Member

    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.
     
    RoldanLT likes this.
  4. Mouth

    Mouth Well-Known Member

    No, don't do that - it's just asking for trouble/problems.
     
    eva2000 likes this.
  5. Set3sh

    Set3sh Active Member

    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.
     
  6. Mouth

    Mouth Well-Known Member

    If you're going to cut n paste from kernel.org (or where ever else referenced it), then the next paragraph reads;
    https://www.kernel.org/doc/Documentation/sysctl/vm.txt
     
    eva2000 likes this.
  7. Solidus

    Solidus Well-Known Member

    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.
     
  8. Set3sh

    Set3sh Active Member

    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.
     
  9. WSWD

    WSWD Well-Known Member

    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.
     
    SneakyDave and MattW like this.
  10. Solidus

    Solidus Well-Known Member

    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.
     
  11. WSWD

    WSWD Well-Known Member

    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.
     
  12. Solidus

    Solidus Well-Known Member

    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.
     
  13. WSWD

    WSWD Well-Known Member

    Did you run top, like I mentioned, to actually find out what is causing the swapping?
     
  14. Solidus

    Solidus Well-Known Member

    Yep, mysqld and drwebd mostly. Postfix and Apache a little.
     
  15. alegeek

    alegeek Member

    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?
     
  16. Solidus

    Solidus Well-Known Member

    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.
     

Share This Page