How can I get CPU count and total RAM from the OS X command line?
How to get CPU count and total RAM from the OS X command line
mac-osx
Related Solutions
The most basic of answer? No. The OS handles memory management for the user - giving a process as much memory as it requires as it requests it and doesn't provide a whole lot in terms of being able to modify how the system performs. It doesn't help a lot with trying to figure out if memory utilization is part of the equation as to why the Linux servers are outperforming the Mac servers.
Because of the lack of "hackability" in the virtual memory system your best options are to evaluate the performance of the virtual memory to see if it is truly the reason you're having performance issues - the most basic is to use vm_stat
or use follow it live by using vm_stat [number of seconds]
to see how many page outs are occuring. Moving unused pages from the RAM to the hard drive has a much lower cost than to retrieve the pages back from the hard drive to your RAM. A page in OS X terms refers to 4kb of data.
Additionally, 32 bit processes (eg. Illustrator) can not access more than 4GB so it's theoretically possible to have 2GB free and have a process run out of memory if you had 6GB installed.
On both your desktop and the servers I would run vm_stat 2
and check the last column - especially when you experience the multiple second delay when switching to say Illustrator.
To find out how large the swap actually is you can either check the swap size in Activity Monitor or run du -shc /var/vm/swap* | tail -n 1
.
Using du -sh /var/vm
has one caveat: on laptops that have the hibernate mode (anything for the past 4 years or newer) there may be a sleepimage
roughly the size of how much memory you have installed.
Mac OS X Memory Jargon:
Wired : This refers to kernel code and such. Memory that should not ever be moved out of the RAM. Also know as resident memory.
Shared : Memory that is shared between two or more processes. Both processes would show this amount of memory so it can be a bit misleading as to how much memory is actually in use.
Real : This is the "real" memory usage for an application as reported by task_info() - a rough count of the number of physical pages that the current process has. (RSIZE)
Private : This is memory that a process is using solely on it's own that is used in Resident memory. (RPRVT)
Virtual : The total amount of address space in the process that's mapped to anything - whether that's an arbitrarily large space for variables or anything - it does not equate to actual VM use. (VSIZE)
Active : Memory currently labelled as active and is used RAM.
Inactive : "Inactive memory is no longer being used and has been cached to disk. It will remain in RAM until another application needs the space. Leaving this information in RAM is to your advantage if you (or a client of your computer) come back to it later." - Mac OS X Help
Free : The amount of RAM actually available without any data.
The best documentation I know of (and have been able to find in followup research) is Apple's own Managing Memory article on their developer website.
Other worthwhile sources: Darwin-dev mailing list: [1], [2] and an old article on MacOSXHints. Additionally Mike Ash has posted a good layman's introduction on his blog
It may be possible (I have not tried it out) to disable the virtual memory system by running sudo launchctl unload /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist
and then restarting so that dynamic_pager (the program that actually does the virtual memory allocation) doesn't start up. However because the OS assumes it has virtual memory you may find yourself running into issues very, very quickly. To load it back up run sudo launchctl load /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist
(if necessary from single user mode)
Lots of people have already mentioned system_profiler
, so I'll just list some other commands I'd recommend for "looking around" a Mac OS X system:
top -u -s5
My favorite command for seeing what's going on. Shows processes sorted by CPU usage, updated every 5 seconds (I find the default of 1 second to be too fast), as well as load average, physical & VM stats, etc. Replace -u
with -orsize
to sort by resident memory size instead.
sysctl vm.swapusage
VM stats -- see man sysctl for other options, or just try -a and look through the whole list.
sw_vers
Shows the OS version and build
softwareupdate -l
Polls an Apple server for a list of relevant software updates. -i -a
will download and install them. Note that it will not restart the computer for updates that require it; instead they will be marked with "[restart]" in the list, and you'll need to restart manually after installing them (but be wary of firing off updates that require a reboot when you don't have physical access to the computer -- If something goes wrong you could be in trouble).
systemsetup
networksetup
Command-line access to the general and network settings normally accessed by the System Preferences GUI app. Useful, but have the most amazingly (and painfully) verbose options I've ever seen. (I mean, -getdisablekeyboardwhenenclosurelockisengaged
? Really?)
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart
Command-line control for the remote management service (Apple Remote Desktop), which doubles as a VNC server if you need GUI access. The trick is that to make it work with a generic VNC client, you have to enable "legacy"-style authentication, with something like:
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -configure -clientopt -setvnclegacy -vnclegacy yes -setvncpw -vncpw PWGoesHere -restart -agent
(Or, if it's not already running, use -activate instead of -restart -agent)
Best Answer