Linux中与内存相关的命令

adtxl
2021-12-14 / 0 评论 / 928 阅读 / 正在检测是否收录...

1. free

free是显示的当前内存的使用
使用方法如下:

usage: free [-bkmgt]

Display the total, free and used amount of physical memory and swap space.

-bkmgt    Output units (default is bytes)
-h    Human readable (K=1024)

如:

tv_e1_dtmb:/ # free -m
                total        used        free      shared     buffers
Mem:             1292        1109         183           3           7
-/+ buffers/cache:           1101         190
Swap:              99           0          99

第一部分Mem行:
total 内存总数: 1292M (表示物理内存1.2G)
used 已经使用的内存数: 1109M
free 空闲的内存数: 183M
shared 被共享使用的物理内存大小: 3M
buffers Buffer 设备数据缓存内存数: 7M
关系:total(1292M) = used(1109M) + free(183M)

第二部分(-/+ buffers/cache):
(-buffers/cache) used内存数:1101M (指的第一部分Mem行中的used - buffers - cached)
// 注:我这个Android平台free命令未显示cached内存
(+buffers/cache) free内存数: 190M (指的第一部分Mem行中的free + buffers + cached)

Linux为了提高磁盘和内存存取效率, 除了对dentry进行缓存(用于VFS,加速文件路 径名到inode的转换), 还采取了两种主要Cache方式:Buffer Cache和Page Cache。前者针对磁盘块的读写,后者针对文件inode的读写。这些Cache能有效缩短了 I/O系统调用(比如read,write,getdents)的时间。

page cache和buffer cache最大的差别在于:page cache是对文件数据的缓存;buffer cache是对设备数据的缓存。两者在实现上差别不是很大,都是采用radix树进行管理。

2. pmap

查看进程的内存分布情况

usage: pmap [-xq] [pids...]

Report the memory map of a process or processes.

-x    Show the extended format(显示扩展格式)
-q    Do not display some header/footer lines

如:

VSS- Virtual Set Size 虚拟耗用内存(包含共享库占用的内存)
RSS- Resident Set Size 实际使用物理内存(包含共享库占用的内存)
PSS- Proportional Set Size 实际使用的物理内存(比例分配共享库占用的内存),计算Oracle数据库使用的实际物理内存建议使用这列
USS- Unique Set Size 进程独自占用的物理内存(不包含共享库占用的内存),suse linux无此列
一般来说内存占用大小有如下规律:VSS >= RSS >= PSS >= USS
Dirty: 脏页的字节数(包括共享和私有的)(KB)
SWAP:swap使用情况
PERM:权限,r = read w = write x = execute s = shared p = private (copy on write)

tv_e1_dtmb:/ # pmap -x 1867
1867: /vendor/bin/hw/android.hardware.thermal@2.0-service.xxxx
Address    Kbytes     PSS   Dirty    Swap  Mode  Mapping
092e1000      20      18       0       0 r----  android.hardware.thermal@2.0-service.eswin
092e6000      60      58       0       0 r-x--  android.hardware.thermal@2.0-service.eswin
092f5000       4       4       4       0 r----  android.hardware.thermal@2.0-service.eswin
092f6000       4       4       4       0 rw---  android.hardware.thermal@2.0-service.eswin
f1480000    1792      40      40       0 rw---  [anon:scudo:primary]
f1641000    1016       0       0       0 r----  hwbinder
f173f000       4       0       0       0 -----    [anon]
f1740000    1020       8       8       0 rw---  [anon:stack_and_tls:1899]
......
f2a9c000       4       4       4       0 rw---  [anon:arc4random data]
f2a9d000       8       8       8       0 rw---  [anon:System property context nodes]
f2b6e000      24      12      12       0 rw---  [anon:.bss]
ff7d2000     132      32      32       0 rw---  [stack]
ffff0000       4       0       0       0 r-x--  [vectors]
--------  ------  ------  ------  ------
total      21232    1105     672       0

3. vmstat

打印虚拟内存使用状态

usage: vmstat [-n] [DELAY [COUNT]]

Print virtual memory statistics, repeating each DELAY seconds, COUNT times.
(With no DELAY, prints one line. With no COUNT, repeats until killed.)

Show processes running and blocked, kilobytes swapped, free, buffered, and
cached, kilobytes swapped in and out per second, file disk blocks input and
output per second, interrupts and context switches per second, percent
of CPU time spent running user code, system code, idle, and awaiting I/O.
First line is since system started, later lines are since last line.

-n    Display the header only once

如,每隔2s取一次数据,重复10次

tv_e1_dtmb:/ # vmstat 2 10
procs ------------memory------------ ----swap--- -----io---- ---system-- ----cpu----
 r  b    swpd    free   buff   cache    si    so    bi    bo    in    cs us sy id wa
 2  0     512  236568   8760  500808     0     1    54     4     0  9266  8 27 65  0
 1  0     512  236316   8760  500808     0     0     0     0     0 22871  8 27 65  0
 1  0     512  236348   8760  500808     0     0     0     0     0 22235  8 27 65  0
 1  0     512  236300   8760  500808     0     0     0     0     0 23909  8 27 65  0
 1  0     512  236300   8760  500808     0     0     0     0     0 20895  8 26 66  0
 1  0     512  236324   8760  500808     0     0     0     0     0 20645  7 26 67  0
 1  0     512  236024   8760  500808     0     0     0     0     0 20285  7 28 65  0
 1  0     512  235796   8760  500808     0     0     0     0     0 20230  8 27 66  0
 1  0     512  235756   8760  500808     0     0     0     0     0 21273  9 26 65  0
 2  0     512  235772   8760  500808     0     0     0     0     0 25651 10 25 65  0

抓取的信息分为6个模块,

procs:进程状态

字段字段说明
r处于Runnable状态的进程数量
b处于不可中断睡眠状态的进程数量

memory:内存信息

字段字段说明
swpd已用虚拟内存
free空闲内存
buff用于缓冲区的内存
cache用于缓存的内存

swap:交换区

字段字段说明
si每秒从交换区写到内存的大小
so每秒写入交换区的内存大小

io: io读写信息
现在的Linux版本块的大小为1024bytes

字段字段说明
bi每秒读取的块数
bo每秒写入的块数

system:系统信息

字段字段说明
in每秒中断数,包括时钟中断
cs每秒上下文切换次数

cpu:cpu详细信息
这些是总 CPU 时间的百分比

字段字段说明
us用户态进程的CPU使用率
sy内核桃进程的CPU使用率
id空闲CPU百分比
wa等待IO的CPU使用率
st从虚拟机偷取的CPU百分比
0

评论 (0)

取消