1. Use the free command: $free -m free -m parameter is in MB
As shown below:
[root@linuxzgf ~]# free -m
total used free shared buffers cached
Mem: 7982 6811 1171 0 350 5114
-/+ buffers/cache: 1346 6636
Swap: 16935 11 16924
But, do you know which one is the amount of remaining memory?
It is very likely to be considered as 1171 under free
But this is not the case.
The correct number should actually read -/+ buffers/cache: this line
-/+ buffers/cache: 1346 6636
In this example, the application only uses 1346MB of memory, and 6636MB of free memory can be used.
Some simple calculation methods:
物理已用内存 = 实际已用内存 - 缓冲 - 缓存 = 6811M - 350M - 5114M
物理空闲内存 = 总物理内存 - 实际已用内存 + 缓冲 + 缓存
应用程序可用空闲内存 = 总物理内存 - 实际已用内存
应用程序已用内存 = 实际已用内存 - 缓冲 - 缓存
2. The top command
top - 02:53:32 up 16 days, 6:34, 17 users, load average: 0.24, 0.21, 0.24
Tasks: 481 total, 3 running, 474 sleeping, 0 stopped, 4 zombie
Cpu(s): 10.3%us, 1.8%sy, 0.0%ni, 86.6%id, 0.5%wa, 0.2%hi, 0.6%si, 0.0%st
Mem: 4042764k total, 4001096k used, 41668k free, 383536k buffers
Swap: 2104472k total, 7900k used, 2096572k free, 1557040k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
32497 jacky 20 0 669m 222m 31m R 10 5.6 29:27.62 firefox
4788 yiuwing 20 0 257m 18m 13m S 5 0.5 5:42.44 konsole
5657 Liuxiaof 20 0 585m 159m 30m S 4 4.0 5:25.06 firefox
4455 xiefc 20 0 542m 124m 30m R 4 3.1 7:23.03 firefox
6188 Liuxiaof 20 0 191m 17m 13m S 4 0.5 0:01.16 konsole
The first five lines in the statistical information area are the overall statistical information of the system. The first line is the task queue information, which is the same as the execution result of the uptime command. Its content is as follows:
01:06:48 当前时间
up 1:22 系统运行 时间,格式为时:分
1 user 当前登录用户 数
load average: 0.06, 0.60, 0.48 系统负载 ,即任务队列的平均长度。
三个数值分别为 1分钟、5分钟、15分钟前到现在的平均值。
The second and third lines are process and CPU information. When there are multiple CPUs, these contents may exceed two lines. The content is as follows:
Tasks: 29 total 进程总数
1 running 正在运行的进程数
28 sleeping 睡眠的进程数
0 stopped 停止的进程数
0 zombie 僵尸进程数
Cpu(s): 0.3% us 用户空间占用CPU百分比
1.0% sy 内核 空间占用CPU百分比
0.0% ni 用户进程空间内改变过优先级的进程占用CPU百分比
98.7% id 空闲CPU百分比
0.0% wa 等待输入输出的CPU时间百分比
0.0% hi
0.0% si
The last two lines are memory information. The content is as follows:
Mem: 191272k total 物理内存总量
173656k used 使用的物理内存总量
17616k free 空闲内存总量
22052k buffers 用作内核缓存的内存量
Swap: 192772k total 交换区总量
0k used 使用的交换区总量
192772k free 空闲交换区总量
123988k cached 缓冲的交换区总量。
内存中的内容被换出到交换区,而后又被换入到内存,但使用过的交换区尚未被覆盖,
该数值即为这些内容已存在于内存中 的交换区的大小。
相应的内存再次被换出时可不必再对交换区写入。
The detailed information of each process is displayed below the statistical information area of the process information area. First, let's understand the meaning of each column.
列名 含义
PID 进程id
PPID 父进程id
RUSER Real user name
UID 进程所有者的用户id
USER 进程所有者的用户名
GROUP 进程所有者的组名
TTY 启动进程的终端名。不是从终端启动的进程则显示为 ?
PR 优先级
NI nice值。负值表示高优先级,正值表示低优先级
P 最后使用的CPU,仅在多CPU环境 下有意义
%CPU 上次更新到现在的CPU时间占用百分比
TIME 进程使用的CPU时间总计,单位秒
TIME+ 进程使用的CPU时间总计,单位1/100秒
%MEM 进程使用的物理内存 百分比
VIRT 进程使用的虚拟内存总量,单位kb。VIRT=SWAP+RES
SWAP 进程使用的虚拟内存中,被换出的大小,单位kb。
RES 进程使用的、未被换出的物理内存大小,单位kb。RES=CODE+DATA
CODE 可执行代码占用的物理 内存大小,单位kb
DATA 可执行代码以外的部分(数据 段+栈)占用的物理 内存大小,单位kb
SHR 共享内存大小,单位kb
nFLT 页面错误次数
nDRT 最后一次写入到现在,被修改过的页面数。
S 进程状态。
D =不可中断的睡眠状态
R =运行
S =睡眠
T =跟踪/停止
Z =僵尸进程
COMMAND 命令名/命令行
WCHAN 若该进程在睡眠,则显示睡眠中的系统函数名
Flags 任务标志,参考 sched.h
By default, only the important PID, USER, PR, NI, VIRT, RES, SHR, S, %CPU, %MEM, TIME+, COMMAND columns are displayed. You can use the following shortcut keys to change the display content:
按 f 键可以选择显示的内容。按 f 键之后会显示列的列表,按 a-z 即可显示或隐藏对应的列,最后按回车键确定。
按 o 键可以改变列的显示顺序。按小写的 a-z 可以将相应的列向右移动,而大写的 A-Z 可以将相应的列向左移动。最后按回车键确定。
按大写的 F 或 O 键,然后按 a-z 可以将进程按照相应的列进行排序。而大写的 R 键可以将当前的排序倒转。
top命令使用过程中,还可以使用一些交互的命令来完成其它参数的功能。这些命令是通过快捷键启动的。
<空格>:立刻刷新。
P:根据CPU使用大小进行排序。
T:根据时间、累计时间排序。
q:退出top命令。
m:切换显示内存信息。
t:切换显示进程和CPU状态信息。
c:切换显示命令名称和完整命令行。
M:根据使用内存大小进行排序。
W:将当前设置写入~/.toprc文件中。这是写top配置文件的推荐方法。
可以看到,top命令是一个功能十分强大的监控系统的工具,对于系统管理员而言尤其重要。但是,它的缺点是会消耗很多系统资源。
Linux provides us with a very convenient method. The /proc directory provides us with all the information. In fact, tools such as top also use this to obtain the corresponding information:
/proc/meminfo 机器的内存使用信息
/proc/pid/maps pid为进程号,显示当前进程所占用的虚拟地址。
/proc/pid/statm 进程所占用的内存
#查看CPU个数
cat /proc/cpuinfo | grep "physical id" | uniq | wc -l
#查看CPU核数
cat /proc/cpuinfo | grep "cpu cores" | uniq
#查看CPU型号
cat /proc/cpuinfo | grep 'model name' |uniq
#查看USB设备
cat /proc/bus/usb/devices
#查看其它设备
cat /proc/bus/input/devices
#查看中断信息
cat /proc/interrupts