vzfree – Checking Memory Usage inside OpenVZ VE

Gee. It has been a while since I’ve last written here, and it will be a while again for my next post as I will be on holidays from next Monday (visiting families in Hong Kong and Taiwan). Let me share one small utility program that I wrote quite a while ago.

We all know that the Linux command free(1) is pretty useless inside an OpenVZ VE, even those with meminfo virtualised. So I basically wrote this little util to grab the data from the dreadful user_beancounters and format them into something useful. It’s written in C and only depends on libc so it’s pretty light weight. It also does a little bit of analysis instead of just dumping the data, which I will explain later on.

Download and Install

It should be pretty straight forward.

  • Download vzfree-0.1.tgz (1.5kb)
  • Unpack the tarball and change into the directory
  • Type “make && sudo make install

The binary should then be installed to /usr/local/bin/vzfree. Note that as bean counters file is only readable by root in OpenVZ, you need to either (1) run vzfree as root, or (2) chmod u+s vzfree. Just a disclaimer — I am a rusty C coder and I am distributing this program as it is. No warranty. I am certainly not responsible if this simple program somehow got exploited and let h4x0rs pwn3d your VPS 🙂

Usage

I am running vzfree on my VPSLink VPS (256MB OpenVZ).

# vzfree
             Total     Used     Free
Kernel:     49.83M    3.57M   46.26M
Allocate:  250.00M  103.91M  146.09M (256M Guaranteed)
Commit:    147.93M   53.07M   94.86M (47.6% of Allocated)
Swap:                 0.03M          (0.1% of Committed)

So how do they make sense?

  • Kernel is the “unswappable memory allocated by the operating system kernel”. Each process will continue a bit of kernel memory.

  • Allocate shows the maximum amount that your processes can allocate (via malloc(3) calls, i.e. the burstable memory), and how much has already been allocated. It also shows the guaranteed amount (i.e. the guaranteed memory).

    You can see that on VPSLink, the burstable amount is actually less than the guaranteed amount and it’s a feature instead of a bug according to them to prevent overselling.

  • Commit shows the actual memory pages that have been used. On Linux it’s possible to over-commit your memory as what you have allocated might not be the same as what you have used. I have discussed them here, and the amount under “Used” is the sum of physical pages + swapped pages.

    The amount under “Total” is actually the out-of-memory kill guarantee, i.e. if the host node is running out of memory, processes in your VE might be killed if Used > Total. In fact it is possible to commit more pages than OOM kill guarantee (so “Free” actually becomes negative). Watch out for that if you are on a server that’s massively oversold.

    The last number also shows the ratio between “allocated” and “committed”. You’ll find this number very low if you are running apps such as Java, MySQL, etc who allocate big chunks of memory but don’t use all of them at once.

  • Swap is my guess on how many pages have been swapped on the physical node. Bean counts tell you how many RAM pages are used so it should be possible to work out the proximity of swap from other values. If the percentage of committed memory have been swapped is high, then you know your host is overselling.

Hopefully this little util is making more sense out of memory information from the bean counters.