How can I get the amount of available memory portably across distributions?

terdon

The standard files/tools that report memory seem to have different formats on different Linux distributions. For example, on Arch and Ubuntu.

  • Arch

    $ free
                  total        used        free      shared  buff/cache   available
    Mem:        8169312     3870392     2648348       97884     1650572     4110336
    Swap:      16777212      389588    16387624
    
    
    $ head /proc/meminfo 
    MemTotal:        8169312 kB
    MemFree:         2625668 kB
    MemAvailable:    4088520 kB
    Buffers:          239688 kB
    Cached:          1224520 kB
    SwapCached:        17452 kB
    Active:          4074548 kB
    Inactive:        1035716 kB
    Active(anon):    3247948 kB
    Inactive(anon):   497684 kB
    
  • Ubuntu

    $ free
                 total       used       free     shared    buffers     cached
    Mem:      80642828   69076080   11566748    3063796     150688   58358264
    -/+ buffers/cache:   10567128   70075700
    Swap:     20971516    5828472   15143044
    
    
    $ head /proc/meminfo 
    MemTotal:       80642828 kB
    MemFree:        11565936 kB
    Buffers:          150688 kB
    Cached:         58358264 kB
    SwapCached:      2173912 kB
    Active:         27305364 kB
    Inactive:       40004480 kB
    Active(anon):    7584320 kB
    Inactive(anon):  4280400 kB
    Active(file):   19721044 kB
    

So, how can I portably (across Linux distros only) and reliably get the amount of memory—excluding swap—that is available for my software to use at a particular time? Presumably that's what's shown as "available" and "MemAvailable" in the output of free and cat /proc/meminfo in Arch but how do I get the same in Ubuntu or another distribution?

Stephen Kitt

MemAvailable is included in /proc/meminfo since version 3.14 of the kernel; it was added by commit 34e431b0a. That's the determining factor in the output variations you show. The commit message indicates how to estimate available memory without MemAvailable:

Currently, the amount of memory that is available for a new workload, without pushing the system into swap, can be estimated from MemFree, Active(file), Inactive(file), and SReclaimable, as well as the "low" watermarks from /proc/zoneinfo.

The low watermarks are the level beneath which the system will swap. So in the absence of MemAvailable you can at least add up the values given for MemFree, Active(file), Inactive(file) and SReclaimable (whichever are present in /proc/meminfo), and subtract the low watermarks from /proc/zoneinfo. The latter also lists the number of free pages per zone, that might be useful as a comparison...

The complete algorithm is given in the patch to meminfo.c and seems reasonably easy to adapt:

  • sum the low watermarks across all zones;
  • take the identified free memory (MemFree);
  • subtract the low watermark (we need to avoid touching that to avoid swapping);
  • add the amount of memory we can use from the page cache (sum of Active(file) and Inactive(file)): that's the amount of memory used by the page cache, minus either half the page cache, or the low watermark, whichever is smaller;
  • add the amount of memory we can reclaim (SReclaimable), following the same algorithm.

So, putting all this together, you can get the memory available for a new process with:

awk -v low=$(grep low /proc/zoneinfo | awk '{k+=$2}END{print k}') \
 '{a[$1]=$2}
  END{ 
   print a["MemFree:"]+a["Active(file):"]+a["Inactive(file):"]+a["SReclaimable:"]-(12*low); 
  }' /proc/meminfo 

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How can I multiply 64 bit operands and get 128 bit result portably?

来自分类Dev

How can I *only* get the number of bytes available on a disk in bash?

来自分类Dev

How to measure total amount of memory used by userspace processes in Linux?

来自分类Dev

How can I get the application path in C?

来自分类Dev

How can I get this Django template to render?

来自分类Dev

How to get all available models in Magento?

来自分类Dev

How can I get a list of PHP errors occurred in a page?

来自分类Dev

How can I get the Mercurial Changeset ID from a revision number?

来自分类Dev

How can I get path to current twig template dynamically?

来自分类Dev

How can I get the current weekday in Rust using the Chrono crate?

来自分类Dev

How can i get the fileinfo of all files in a folder with GetFile()?

来自分类Dev

How can I get the original resource in ngResource with a responseError interceptor?

来自分类Dev

How can i get a single widget(li) JSON in gridster?

来自分类Dev

How can I get around this annoying WiX issue?

来自分类Dev

How can I get last inserted id using Hibernate

来自分类Dev

How can I get a UTC timestamp in SQL400 on iSeries?

来自分类Dev

How can I get list of users of a specific domain?

来自分类Dev

How can I get 3D object dimensions in ThreeJS?

来自分类Dev

How can i get the value of a variable through the Super keyword?

来自分类Dev

How can I get a detailed log of django csrf failure?

来自分类Dev

Quicksort in C with huge amount of data / memory

来自分类Dev

How can I get the x/y location of a touch relative to the entire view when I touch a button?

来自分类Dev

How to get list of names of available bluetooth devices in ios swift?

来自分类Dev

What can I use to monitor memory used by a single program?

来自分类Dev

What can I use to monitor memory used by a single program?

来自分类Dev

How am I using copytree wrong and how can I fix it so it doesn't get caught in a huge loop?

来自分类Dev

How can I get the creation date of folder in Ambari HDFS with Linux commands?

来自分类Dev

How can I get audio output from Google Glass to a 3.5mm headphone jack?

来自分类Dev

How can i use a rangeByScore get result withing SPRING-DATA-REDIS

Related 相关文章

  1. 1

    How can I multiply 64 bit operands and get 128 bit result portably?

  2. 2

    How can I *only* get the number of bytes available on a disk in bash?

  3. 3

    How to measure total amount of memory used by userspace processes in Linux?

  4. 4

    How can I get the application path in C?

  5. 5

    How can I get this Django template to render?

  6. 6

    How to get all available models in Magento?

  7. 7

    How can I get a list of PHP errors occurred in a page?

  8. 8

    How can I get the Mercurial Changeset ID from a revision number?

  9. 9

    How can I get path to current twig template dynamically?

  10. 10

    How can I get the current weekday in Rust using the Chrono crate?

  11. 11

    How can i get the fileinfo of all files in a folder with GetFile()?

  12. 12

    How can I get the original resource in ngResource with a responseError interceptor?

  13. 13

    How can i get a single widget(li) JSON in gridster?

  14. 14

    How can I get around this annoying WiX issue?

  15. 15

    How can I get last inserted id using Hibernate

  16. 16

    How can I get a UTC timestamp in SQL400 on iSeries?

  17. 17

    How can I get list of users of a specific domain?

  18. 18

    How can I get 3D object dimensions in ThreeJS?

  19. 19

    How can i get the value of a variable through the Super keyword?

  20. 20

    How can I get a detailed log of django csrf failure?

  21. 21

    Quicksort in C with huge amount of data / memory

  22. 22

    How can I get the x/y location of a touch relative to the entire view when I touch a button?

  23. 23

    How to get list of names of available bluetooth devices in ios swift?

  24. 24

    What can I use to monitor memory used by a single program?

  25. 25

    What can I use to monitor memory used by a single program?

  26. 26

    How am I using copytree wrong and how can I fix it so it doesn't get caught in a huge loop?

  27. 27

    How can I get the creation date of folder in Ambari HDFS with Linux commands?

  28. 28

    How can I get audio output from Google Glass to a 3.5mm headphone jack?

  29. 29

    How can i use a rangeByScore get result withing SPRING-DATA-REDIS

热门标签

归档