Making sense of Linux memory management

Discussion in 'all things UNIX' started by Sadeghi85, Nov 4, 2013.

Thread Status:
Not open for further replies.
  1. Sadeghi85

    Sadeghi85 Registered Member

    Joined:
    Dec 20, 2009
    Posts:
    747
    I'm working with a server with 12GB ram, installed VMware Workstation, created a VM and gave it 4GB ram. The OS is Centos 6. I expect it to use around 5GB at this point, 700MB for OS, 4GB for VM and 300MB for other VMware processes. So I should have 7GB free right?

    Both "htop" and "free" give a lot of different numbers, I don't know which is which.(see attachement)

    If I try to create another VM and give it 6GB ram the server goes to swap. What is happening?
     

    Attached Files:

    • htop.png
      htop.png
      File size:
      460.4 KB
      Views:
      19
  2. Mrkvonic

    Mrkvonic Linux Systems Expert

    Joined:
    May 9, 2005
    Posts:
    10,213
    Memory management in linux is black magic.
    But some basic pointers.

    Your expectations are not realistic.

    The kernel itself will not use much, but the graphical stack may. Second, lots of apps will be cached in memory if there's free space. Buffers and cached are basically free memory from your perspective, because the system will free them when other apps ask. Your swapping policy determines how the swap will be used, even if you have free memory available.

    Your screenshot shows 400mb buffers and 9.2gb cached.
    So you're quite all right in that regard.
    You don't seem to be swapping btw.

    Mrk
     
  3. Sadeghi85

    Sadeghi85 Registered Member

    Joined:
    Dec 20, 2009
    Posts:
    747
    Hi Mrk,

    So how much is "really" free?
    I was hoping I could use the main OS as web server and use VMs for test as to put all the resources to good use. But if the OS hit the swap the websites will become really slow and I should avoid that.
    Is there a reliable way to determine the real free ram?

    I thought the green bar in htop shows used memory, blue bar shows buffer and yellow shows cached?

    "1117mb used" can't be correct, whereas 4140mb RES for VM seems to be correct. What are these numbers? Which of them are reliable?
     
  4. Gullible Jones

    Gullible Jones Registered Member

    Joined:
    May 16, 2013
    Posts:
    1,466
    In your case ~1 GB is unused, and a little less than that is being used by programs. The rest is buffers and cache.

    VMs don't usually preallocate huge amounts of RAM. I don't know about VMWare, but KVM will dynamically allocate more memory up to a limit, rather than just preallocating several gigabytes.

    Linux generally tries to cache as much stuff as it can, but keeps a small amount of RAM truly free, in order to reduce memory fragmentation. The minimum amount is dictated by the vm.min_free_kbytes sysctl setting.

    Re swapping, it helps maintain a healthy amount of disk cache, and is not necessarily a bad thing. What's bad is thrashing - writing stuff out to swap and then immediately trying to retrieve it, which is what happens under low memory conditions.
     
Thread Status:
Not open for further replies.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.