Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


why is swap used when RAM is avaiable
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

why is swap used when RAM is avaiable

AdamMAdamM Member
edited December 2015 in Help

Hi,

I have a 512MB OpenVZ vps with the following output of "free -m". My question is, why is it using swap when there is plenty of free memory in the "cached" portion of memory, which as I understand it, is free and ready to be utilized when needed?

             total       used       free     shared    buffers     cached
Mem:           512        507          4          0          0        429
-/+ buffers/cache:         78        433
Swap:          512         39        472

Comments

  • Compare that to dacentec's vm:

    root@server:~# free -h
                 total       used       free     shared    buffers     cached
    Mem:          512M        78M       433M        30M         0B        62M
    -/+ buffers/cache:        15M       496M
    Swap:         256M        24M       231M
    
    

    That 40MB on swap's nothing to worry about on ovz. It's slowed down ram from what I've been told.

  • exception0x876exception0x876 Member, Host Rep, LIR

    This is actually a good thing, since it puts rarely used pages out of memory so it can use it for more active tasks. If you want to prevent this behaviour, consider adjusting /proc/sys/vm/swappiness

  • WHTWHT Member
    edited December 2015

    As I know swap is used when the server is out of memory (oversold) but not sure.

  • RIYADRIYAD Member, Patron Provider

    run cat /proc/sys/vm/swappiness and set to 30 or something .

    You can read (learn) a bit more here :

    https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-centos-7

  • run cat /proc/sys/vm/swappiness and set to 30 or something .

    my current swappiness is set to 60, perhaps too high.

  • perennateperennate Member, Host Rep
    edited December 2015

    From what I know, you can't change swappiness in OpenVZ container (but you can try sysctl -w vm.swappiness=30). You shouldn't worry about it, as others have said, it's used for rarely accessed pages, and it's actually stored in RAM and just artificially slowed a bit. 60 is a typical default swappiness for many distributions; there's no optimal value since it depends on the application.

  • Its actually normal and good actually, when it comes to the system later needing psychical memory for something else.

  • AdamM said: My question is, why is it using swap when there is plenty of free memory in the "cached" portion of memory, which as I understand it, is free and ready to be utilized when needed?

    When the OS "utilizes" (i.e., reclaims) from the cached memory, the cache gets smaller, and less effective. This may result in more disk I/O to fetch file contents.

    When the OS chooses to swap a page out rather reclaiming an additional page from cached memory, it's betting that the page it is swapping out won't be used as often as the least frequently used page in the cached memory.

    The people who wrote the OS actually know a lot more about these things than you do, so my suggestion is not to mess with the swappiness settings unless you learn at least as much as they know about it, and can figure out how changing the swappiness value will benefit your use case.

    Additionally, don't assume that non-zero values in swap means that your pages aren't in RAM. Swap may appear to be "used" because the OS writes pages that haven't been written in a long time to swap in "readiness" to swap them out if/when necessary. Usage of swap doesn't necessarily mean the pages aren't in RAM.

    Thanked by 3jar sin Clouvider
  • "cat /proc/meminfo" give a bit more info than free presents, and an important figure to look at is SwapCached. This counts pages that are both in RAM and on disk, because they were swapped out and back in, but since being copied back into RAM they haven't changed (so the copy on disk is still accurate) and the space in swap hasn't been needed for anything else. This means the Kernel can swap therm back out for free later: there is no write cost because the page is already saved to swap.

Sign In or Register to comment.