0

I have a system where we are constantly storing files into RAM (under /tmp/), processing them and then deleting them.

Some files we need to keep in RAM and other not.

My problem is that I want to report how much RAM is left that is useable to store files.

When I use "free" I may get a report that says:

free: 10000 (KB)

However when I run cat /proc/meminfo I get a different/more-meaningful story:

memfree:     10000 (KB)
Cached:     100000 (KB)
SwapCached:      0 (KB)
Active:      59000 (KB)
Inactive:    41000 (KB)

Ok, I have approximated/rounded the values for clarity.

From this post: linux-inactive-memory, I can see that "inactive" memory is usable, and therefore - as far as I am concerned - it is free.

So I should be able to report 10000 KB + 41000 KB of free (well, ok not "free" free, but usable) memory.

Is there a command I can use (or some other method) whereby I can report the total "usable" memory?

I believe the total usable, as far as I understand it, is "free" + "inactive".

Thanks

  • Do you have MemAvailable in /proc/meminfo? – Stephen Kitt Mar 27 '17 at 15:19
  • ah, no... but that does sound like what I want!! - I am using busybox (embedded) Linux, so its a bit cut-down. – code_fodder Mar 27 '17 at 16:50
  • It's not quite what you want, because it gives the amount of memory usable before you hit swap; combined with the amount of inactive memory it would solve your problem. It's provided by the kernel, so Busybox doesn't enter the equation; but you do need a recent enough kernel (3.14 or later). Alternatively you can use the formula in the answer to the linked question. – Stephen Kitt Mar 27 '17 at 16:57
  • Sorry, you're right, but the kernel is probably quite old I'll need to dig out the version), but anyway its not there :(. I did not see a formula in the linked question... which part do you mean? - thanks :) – code_fodder Mar 27 '17 at 17:04

1 Answers1

1

Since 3.14, the Linux kernel tracks how much memory is really available — i.e. allocateable without hitting swap, including reclaimable memory — in the MemAvailable entry in /proc/meminfo. This already includes reclaimable memory tracked as inactive, so there’s no need even to add that.

On older kernels you can use the formula given in the answer to How can I get the amount of available memory portably across distributions?:

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 
Stephen Kitt
  • 434,908
  • Ah - I thought you meant the formula in the link in my question. Ok, yes you can parse the /proc/meminfo file. But are you sure about adding the "Active(file)", as far as I have read (and can put the links if needed) you can't necessarily claim the "active" memory as "usable"... – code_fodder Mar 27 '17 at 18:05