Use command lines on Mac to view system memory usage

Source: Internet
Author: User
Tags memory usage cpu usage python script

On Mac, you can use the graphical tool Activity Monitor to view the CPU and memory usage of the system. So how can I use the command line to view it?
For CPU usage, use the top command.
You can also use top for memory usage, but the complete command I commonly use is: top-l 1 | head-n 10 | grep PhysMem
However, I prefer the vm_stat command for memory usage.
Of course, the command output of vm_stat is not intuitive enough. I found a Python script on the Internet to parse and package the output of vm_stat and ps commands, then, the system memory usage is easy to read.

The command is as follows:

The code is as follows: Copy code
Jay @ Jay-Air :~ $ Top-l 1 | head-n 10 | grep PhysMem
PhysMem: 3192 M used (703 M wired), 83 M unused.
Jay @ Jay-Air :~ $ Vm_stat
Mach Virtual Memory Statistics: (page size of 4096 bytes)
Pages free: 15663.
Pages active: 345093.
Pages inactive: 293876.
Pages speculative: 6081.
Pages throttled: 0.
Pages wired down: 177821.
Pages purgeable: 82724.
"Translation faults": 339092568.
Pages copy-on-write: 6225304.
Pages zero filled: 216793000.
Pages reactivated: 139210195.
Pages purged: 7287795.
File-backed page: 54755.
Anonymous pages: 590295.
Pages stored in compressor: 1239304.
Pages occupied by compressor: 209538.
Decompressions: 40056861.
Compressions 48433378.
Pageins: 5894721.
Pageouts: 208954.
Swapins: 3541954.
Swapouts: 4157760.



Python script to get system memory usage: (https://github.com/smilejay/python/blob/master/py2014/mac_free.py)

The code is as follows: Copy code
#! /Usr/bin/python
'''
Created on Jun 1, 2014
 
@ Author: jay
'''
 
Import subprocess
Import re
 
# Get process info
Ps = subprocess. Popen (['ps', '-caxm', '-orss, comm'], stdout = subprocess. PIPE). communicate () [0]
Vm = subprocess. Popen (['vm _ stat'], stdout = subprocess. PIPE). communicate () [0]
 
# Iterate processes
ProcessLines = ps. split ('N ')
Sep = re. compile ('[s] + ')
RssTotal = 0 # kB
For row in range (1, len (processLines )):
RowText = processLines [row]. strip ()
RowElements = sep. split (rowText)
Try:
Rss = float (rowElements [0]) * 1024
Except t:
Rss = 0 # ignore...
RssTotal + = rss
 
# Process vm_stat
VmLines = vm. split ('N ')
Sep = re. compile (': [s] + ')
VmStats = {}
For row in range (1, len (vmLines)-2 ):
RowText = vmLines [row]. strip ()
RowElements = sep. split (rowText)
VmStats [(rowElements [0])] = int (rowElements [1]. strip ('.') * 4096
 
Print 'wired Memory: tt % d Mbit '% (vmStats ["Pages Wired down"]/1024/1024)
Print 'active Memory: tt % d MB' % (vmStats ["Pages Active"]/1024/1024)
Print 'inactive Memory: t % d Mbit '% (vmStats ["Pages Inactive"]/1024/1024)
Print 'free Memory: tt % d MB' % (vmStats ["Pages Free"]/1024/1024)
Print 'real Mem Total (ps): t %. 3f MB' % (rssTotal/1024/1024)



The output of this Python script is as follows:

The code is as follows: Copy code
Jay @ Jay-Air :~ /Workspace/python. git/py2014 $ python mac_free.py
Wired Memory: 665 MB
Active Memory: 1464 MB
Inactive Memory: 1110 MB
Free Memory: 77 MB
Real Mem Total (ps): 2431.688 MB



In addition, for the memory usage in Mac OS X, wired, active, inactive, free and other terms, find the following information:
In short, the [memory] Usage of Mac OS X is divided into four types: wired, active, inactive and free.
Wired is the core of the system and will never be evicted from the physical [memory] of the system.
Active indicates that the [memory] data is in use or has just been used.
Inactive indicates that the data in the [memory] is valid, but has not been used recently.
Free indicates that the data in the [memory] is invalid and the space can be used by the program at any time.
When the free [memory] is lower than a certain value (this value is determined by the size of your physical [memory]), the system uses inactive resources in the following order. First, if the inactive data has been recently called, the system will change its status to active and connect it to the logic address of the original active [memory, if inactive [memory] data has not been used recently, but has been changed but has not been modified in the corresponding virtual [memory] of the hard disk, the system modifies the virtual [memory] of the corresponding hard disk and releases the physical [memory] as free for the program. If the data in inactive [memory] is not changed after it is mapped to the hard disk, it is directly released as free. Finally, if the active [memory] is not used for a period of time, the status will be changed to inactive temporarily. Therefore, if your system has a small amount of free memeory and a large number of inactive memeory, it means that your [memory] is sufficient, and the system runs in the optimal state, as long as needed, the system will use them, so don't worry. On the contrary, if the free memory and inactive memory of the system are few, and there are many active memory, your [memory] is not enough. Of course, most of the [memory] on the server is free, and the system is not in the optimal state, because a lot of data needs to be called from the hard disk, but the speed is slow.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.