2

I'm trying to get the amount of physical memory currently used under Linux. I've followed the answer at How to determine CPU and memory consumption from inside a process? It appeared to work fine on my computer, but when deployed to DigitalOcean 512Mb server it started reporting some weird values.

According to htop system was using around 129Mb ram out of 490Mb available. Also here's the result of free -m.

             total       used       free     shared    buffers     cached
Mem:           490        472         17          0        168        176
-/+ buffers/cache:        128        361
Swap:         1023          0       1023

Formula from the mentioned answer was (sysinfo.totalram - sysinfo.freeram) * sysinfo.mem_unit. It looks like I should also take the 'buffers' and 'cached' columns into account to get the correct value. So I wrote a simple test app to find out what's stored in the sysinfo structure.

#include <cstdio>
#include <sys/sysinfo.h>

int main()
{
        struct sysinfo memInfo;
        sysinfo (&memInfo);

        long long int physMemUsed = memInfo.totalram - memInfo.freeram;
        physMemUsed *= memInfo.mem_unit;

        printf("totalRam %lld\n", (long long int)memInfo.totalram * memInfo.mem_unit / 1024 / 1024);
        printf("freeRam %lld\n", (long long int)memInfo.freeram * memInfo.mem_unit / 1024 / 1024);
        printf("sharedRam %lld\n", (long long int)memInfo.sharedram * memInfo.mem_unit / 1024 / 1024);
        printf("bufferRam %lld\n", (long long int)memInfo.bufferram * memInfo.mem_unit / 1024 / 1024);
        printf("totalSwap %lld\n", (long long int)memInfo.totalswap * memInfo.mem_unit / 1024 / 1024);
        printf("freeSwap %lld\n", (long long int)memInfo.freeswap * memInfo.mem_unit / 1024 / 1024);
        printf("totalHigh %lld\n", (long long int)memInfo.totalhigh * memInfo.mem_unit / 1024 / 1024);
        printf("freeHigh %lld\n", (long long int)memInfo.freehigh * memInfo.mem_unit / 1024 / 1024);
}

And here's the result:

totalRam 490
freeRam 17
sharedRam 0
bufferRam 168
totalSwap 1023
freeSwap 1023
totalHigh 0
freeHigh 0

It looks like sysinfo.buffersram corresponds to 'buffers' column of free -m, but what's the 'cached' value? What's the proper way to get the amount of used physical memory?

Community
  • 1
  • 1
Sebastian Nowak
  • 5,607
  • 8
  • 67
  • 107
  • 1
    What exactly are you trying to achieve? Yes, the OS will fill all available memory with file-cache/buffers, which can be disposed of should there be something more useful to put there - but you don't really want your system to sit there with a load of unused memory when it could be used for caching files, do you? – Mats Petersson Jul 23 '15 at 07:38
  • I want to get the amount of free physical memory, as reported by htop. – Sebastian Nowak Jul 23 '15 at 07:44
  • Does this help: http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/fs/proc/meminfo.c. It's the source code to display cache system information.`cached = global_page_state(NR_FILE_PAGES) - total_swapcache_pages - i.bufferram;` – KillaBytes Jul 23 '15 at 07:44
  • [`cached` seems to be the sum of the `Cached` and `Slab` value in `/proc/meminfo`.](https://gitlab.com/procps-ng/procps/blob/v3.3.10/proc/sysinfo.c#L706) – cremno Jul 23 '15 at 07:58
  • `Cached` also includes all of `Shmem` and `tmpfs` usage, too. Note that those cannot be usually dropped in case more memory is needed so usable part of cached will be less than full `Cached`. See `MemAvailable` instead in `/proc/meminfo`. – Mikko Rantalainen Jul 26 '22 at 20:48

0 Answers0