How can I get CPU count and total RAM from the OS X command line?
3 Answers
scorp@antani-mac:~$ hwprefs cpu_count
2
scorp@antani-mac:~$ hwprefs memory_size
4.00 GB
-
16
hwprefs
doesn't seem to be part of the default OS X install (I think it's in Xcode, or maybe the CHUD tools). Feb 16, 2010 at 4:50 -
11
-
2
-
2
-
You can get this from the system_profiler
tool:
system_profiler SPHardwareDataType | grep " Memory:"
system_profiler SPHardwareDataType | grep Cores:
system_profiler SPHardwareDataType | grep Processors:
or, if you want to go low-level, use sysctl
:
sysctl hw.memsize
sysctl hw.ncpu
Or to capture the values in a script (credit: @bleater):
mem_size=$(sysctl -n hw.memsize)
cpus_virtual=$(sysctl -n hw.ncpu)
btw, there are a bunch of other interesting things you can get from sysctl
. Try:
sysctl -a | grep cpu
to see a few of them
-
-
3To capture the output of sysctl in a script, use the -n option, e.g. sh/bash script: CPUS_VIRTUAL=`sysctl -n hw.ncpu`– bleaterOct 8, 2013 at 2:51
-
-
4Fun fact:
sysctl
is available from the terminal in recovery mode, while many other tools are not (a coworker needed to know how much ram was in a laptop that is locked with file vault)– kbyrdSep 16, 2019 at 15:27 -
1Great answer. Sidenote: I definitely LOL'd when I added the
-h
flag to thesysctl
command expecting to give me "human‑readable" output. It simply added commas… lol Which I guess works fine! I just expected KB, MB, GB, etc. to show up. Then again, it should probably be GB in the vast majority of cases… The trouble comes when counting digits to learn the order of magnitude of GBs you have I guess.– aaiezzaMar 19, 2020 at 15:57
The following works in OS X Lion:
$ /usr/sbin/system_profiler SPHardwareDataType
Hardware:
Hardware Overview:
Model Name: iMac
Model Identifier: iMac7,1
Processor Name: Intel Core 2 Duo
Processor Speed: 2.4 GHz
Number of Processors: 1
Total Number of Cores: 2
L2 Cache: 4 MB
Memory: 4 GB
Bus Speed: 800 MHz