17

How to get on MAC OS X using sysctl used and free memory ? Searched for sysctl -a | grep vm or sysctl -a | grep mem but didn't found anything relevant.

1

5 Answers 5

10

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.

3
  • vm_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 ?
    – kesrut
    Oct 7, 2010 at 19:46
  • 1
    Add 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.
    – Daniel Beck
    Oct 7, 2010 at 19:50
  • 3
    the grep doesn't seem required - on my MacBook Pro, sysctl hw.memsize also works and prints hw.memsize: 8589934592
    – ssc
    Sep 10, 2012 at 11:36
5

No grep is needed.

sysctl -n hw.memsize
4
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.

2
  • YMMV. On Sierra, top -l 1 | grep PhysMem: gives me PhysMem: 9674M used (1752M wired), 6701M unused. so I used top -l 1 | grep PhysMem: | awk '{print $6}'. Thanks!
    – pyb
    Dec 10, 2016 at 15:57
  • my result is PhysMem: 15G used (1815M wired, 2047M compressor), 265M unused. so $10 prints nothing. But this is the only answer that kind of gives the correct free memory size
    – phuclv
    Jan 16 at 9:59
0

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.

5
  • Probably for use in a script.
    – Daniel Beck
    Oct 7, 2010 at 17:55
  • 4
    Both usermem and physmem display useless values for systems over 2GB RAM. Both are probably bounded by a signed 32 bit integer.
    – Daniel Beck
    Oct 7, 2010 at 18:12
  • Maybe i can use top to get single output. After i get the output i will parse needed values.
    – kesrut
    Oct 7, 2010 at 19:18
  • Useless use of grep... sysctl hw.memsize=total, sysctl hw.physmem, sysctl hw.usermem
    – TrinitronX
    Dec 3, 2013 at 23:00
  • @DanielBeck is right, hw.memsize is what you probably want for total. See my comment here for more info
    – TrinitronX
    Dec 3, 2013 at 23:06
0

mac os x/ linux

$ sysctl -a | grep '^hw\.m'
hw.memsize: 8589934592
2
  • 1
    This looks like a minor tweak to existing answers. Probably more appropriate as a comment.
    – fixer1234
    Sep 26, 2016 at 4:13
  • the same as sysctl hw.memsize
    – muescha
    Nov 2, 2022 at 12:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .