Mac OS X sysctl get total and free memory size
Solution 1
You don't.
Well, except for total memory size, which you could have found with sysctl -a | grep mem
(hw.memsize = 4294967296
on my machine).
vm_stat
displays the same information as Activity Monitor.app
does, you just need to multiply the value you want by page size. Both are provided in the output.
Solution 2
No grep is needed.
sysctl -n hw.memsize
Solution 3
top -l 1 | grep PhysMem: | awk '{print $10}'
top -l 1
runs top
in logging mode (e.g. file output, not display/terminal output) for one iteration, then quits. The grep
/awk
filters for the free memory value in top
output.
Solution 4
mac os x/ linux
$ sysctl -a | grep '^hw\.m'
hw.memsize: 8589934592
Solution 5
Why not use the top
command to view your memory usage in realtime?
UPDATE:
You can use the following commands:
sysctl -a | grep hw.usermem
sysctl -a | grep hw.physmem
For a complete listing type man sysctl in terminal.
Related videos on Youtube
kesrut
Updated on September 17, 2022Comments
-
kesrut almost 2 years
How to get on MAC OS X using sysctl used and free memory ? Searched for
sysctl -a | grep vm
orsysctl -a | grep mem
but didn't found anything relevant.-
Admin over 8 yearsAlso see Is there a Mac OS X Terminal version of the “free” command in Linux systems? on the Apple Stack Exchange.
-
-
HikeMike over 13 yearsProbably for use in a script.
-
HikeMike over 13 yearsBoth
usermem
andphysmem
display useless values for systems over 2GB RAM. Both are probably bounded by a signed 32 bit integer. -
kesrut over 13 yearsMaybe i can use top to get single output. After i get the output i will parse needed values.
-
kesrut over 13 yearsvm_stat: pages free: 12054 .. 12054 * 4096 / 1024 / 1024 = 47.08 and Activity monitor shows: 103 MB grab.by/6KSD VM_stats output: pastebin.com/BJcxPswv Why i get wrong values ?
-
HikeMike over 13 yearsAdd
Pages speculative
to the free ones. You might have noticed that free+active+inactive+wired down don't add to the total size displayed in Activity Monitor. -
Nallasamy Nss over 10 years@DanielBeck is right,
hw.memsize
is what you probably want for total. See my comment here for more info -
fixer1234 almost 8 yearsThis looks like a minor tweak to existing answers. Probably more appropriate as a comment.
-
pyb over 7 yearsYMMV. On Sierra,
top -l 1 | grep PhysMem:
gives mePhysMem: 9674M used (1752M wired), 6701M unused.
so I usedtop -l 1 | grep PhysMem: | awk '{print $6}'
. Thanks!