following command returns available memory in kilobytes
cat /proc/meminfo | grep MemFree | awk '{ print $2 }'
can some one suggest single command to get the available memory in gb?
following command returns available memory in kilobytes
cat /proc/meminfo | grep MemFree | awk '{ print $2 }'
can some one suggest single command to get the available memory in gb?
Just a slight modification to your own magical incantation:
awk '/MemFree/ { printf "%.3f \n", $2/1024/1024 }' /proc/meminfo
P.S.: Dear OP, if you find yourself invoking grep & awk in one line you're most likely doing it wrong ;} ... Same with invoking cat on a single file; that's hardly ever warranted.
Most simple is the following :
free -h
Following is the output Screenshot :
More details :
DESCRIPTION
free - displays the total amount of free and used physical and swap mem‐
ory in the system, as well as the buffers and caches used by the ker‐
nel. The information is gathered by parsing /proc/meminfo. The dis‐
played columns are:
total Total installed memory (MemTotal and SwapTotal in /proc/meminfo)
used Used memory (calculated as total - free - buffers - cache)
free Unused memory (MemFree and SwapFree in /proc/meminfo)
shared Memory used (mostly) by tmpfs (Shmem in /proc/meminfo, available
on kernels 2.6.32, displayed as zero if not available)
buffers
Memory used by kernel buffers (Buffers in /proc/meminfo)
cache Memory used by the page cache and slabs (Cached and Slab in
/proc/meminfo)
buff/cache
Sum of buffers and cache
available
Estimation of how much memory is available for starting new
applications, without swapping. Unlike the data provided by the
cache or free fields, this field takes into account page cache
and also that not all reclaimable memory slabs will be reclaimed
due to items being in use (MemAvailable in /proc/meminfo, avail‐
able on kernels 3.14, emulated on kernels 2.6.27+, otherwise the
same as free)
freemem_in_gb () {
read -r _ freemem _ <<< "$(grep --fixed-strings 'MemFree' /proc/meminfo)"
bc <<< "scale=3;${freemem}/1024/1024"
}
Please notice that scale=3
can be changed to some other value, for a better precision.
So, for example one could write a function that will take a precision argument, like so:
freemem_in_gb () {
prec=$1;
read -r _ freemem _ <<< "$(grep --fixed-strings 'MemFree' /proc/meminfo)"
bc <<< "scale=${prec:-3};${freemem}/1024/1024"
}
Which will take (or use 3 as a default value) and pass a precision argument to bc
's scale
option
Usage example:
$ freemem_in_gb
5.524
$ freemem_in_gb 7
5.5115814
EDIT Thanks for @Stephen P and @Etan Reisner for leaving a comment and improving this answer. Code edited accordingly.
grep
's long option --fixed-strings
is used purposely instead of -F
or fgrep
for explanatory reasons.
cat /proc/meminfo | grep MemFree
and your grep 'MemFree' < /proc/meminfo
can be done as fgrep MemFree /proc/meminfo
using more efficient "fixed" (no regex) fgrep, no redirection, and fewer processes spawned.
Jan 22, 2016 at 2:11
bc
detail I wasn't familiar with at play.
Jan 22, 2016 at 2:40
4^10
feels cleaner than /1024/1024
, but that's just me
Sep 4, 2022 at 22:30
Yet another way:
expr $(sed -n '/^MemTotal:/ s/[^[:digit:]]//gp' /proc/meminfo) / 1024 / 1024
Also a bit shorter:
expr $(sed -n '/^MemTotal:/ s/[^0-9]//gp' /proc/meminfo) / 1024 / 1024
And if you like bc and precision that much:
bc <<< "scale=2; $(sed -n '/^MemTotal:/ s/[^[:digit:]]//gp' /proc/meminfo) / 1024 / 1024 "
If you have python, you can do it this way:
To get total available memory:
python -c "import os;print(int(round(os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES') / 1024.0**3)))"
In this example, I used round
to round to the nearest GB. You can make it into a shell function like so:
get_mem(){
MEM=$(python -c "import os;print(int(round(os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES') / 1024.0**3)))")
echo $MEM
}
To get free and used memory check out psutil
here.
Whilst I agree that dividing by 1024 should be more correct, I find on my various cloud and physical servers, this gives neater output:
free -m | awk '/^Mem:/{printf("%.1fGb\n",$2/1000)}'