First, the question: I need to find how much total memory is in use, give or take a megabyte, on a Linux system - CentOS but any general solution should work. I'm thinking I want to parse ps aux somehow, but adding up VSZ RSS doesn't work.

Now, for the "why are you handicapping yourself" explanation, if you care...

I'm inside an OpenVZ container. Normally, free -m or cat /proc/meminfo works fine, but the host OS has a bug. The provider doesn't want to take all the containers down to patch, so I have to live with it for a while. Unfortunately, if I reboot the VPS, I have to open a ticket to have the counters reset, otherwise it looks to me like my VPS has 10GB of RAM, an that includes meminfo and free. This week, naturally, I'm doing various tests and rebooting.

The VPS really only has 1GB/2GB burst. If all is well, I see 2048M available and free -m output is normal.

I really need to find a solution that shows the correct memory in use without relying on proc...if such a thing is possible.

  • 1
    By memory, you mean physical memory, right? But what precisely do you mean by "in use"? – David Schwartz Mar 6 '12 at 0:26
  • If the host/OS has a bug, what's to say any other means will be correct? I'm any sane metric would run off of /proc/meminfo (or vz/xen provided data in /proc) – thinice Mar 6 '12 at 0:33
  • Find a new provider. – EEAA Mar 6 '12 at 2:13

I'm sorry, I think you're out of luck here. If the host has a bug that prevents you from seeing the correct amount of memory then I don't think there is a way to work around it. If your kernel can not determine the amount of memory then what can?

Perhaps you could fool your kernel with some kernel parameter that there is only 2 GB free? In case you run a 32 bits system you could get a kernel that only allocates 1 GB. If my memory serves me correctly you could configure a 32 bits kernel to allocate either up to 1 GB (I think it's closer to 750MB), 4 GB or 64 GB (bigmem), the latter comes with a performance penalty. And you won't actually be able to allocate more than 4 GB per process.

See: http://www.kernel.org/doc/Documentation/kernel-parameters.txt

mem=nn[KMG] [KNL,BOOT] Force usage of a specific amount of memory
        Amount of memory to be used when the kernel is not able
        to see the whole system memory or for test.

See if you can find any other parameters you could use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.