linux查看系统内存使用情况
free命令
可以显示系统的内存使用情况
[root@ypcvm ~]# free total used free shared buff/cache available mem: 9383776 294096 8829236 11972 260444 8837612 swap: 2097148 0 2097148
默认单位是:kb
第一列:
mem
: 表示物理内存swap
:表示硬盘上交换分区的使用情况total
: 总内存used
:已使用的内存free
:空闲的内存shared
:共享内存buff
/cache
:缓冲区和缓存available
:可用内存
free:空闲的内存和available:可用内存的区别
free
内存:这是系统中完全未被使用的内存数量available
内存:它更接近于可用内存,系统中可以立即分配给进程的内存数量,它包括free内存和一部分缓存的内存
free -h命令
以可读的格式显示信息,如kb、mb、gb等
[root@ypcvm ~]# free -h total used free shared buff/cache available mem: 8.9g 289m 8.4g 11m 254m 8.4g swap: 2.0g 0b 2.0g [root@ypcvm ~]#
free -t命令
最后一行显示总计行
[root@ypcvm ~]# free -t total used free shared buff/cache available mem: 9383776 296324 8826800 11964 260652 8835300 swap: 2097148 0 2097148 total: 11480924 296324 10923948
total
:总计行
top命令
实时地显示进程的状态,可以通过按上下箭头来翻滚查看需要查找的进程信息
也可以使虚拟机处于连接状态,不自动断开
[root@ypcvm ~]# top top - 14:36:29 up 1:03, 2 users, load average: 0.03, 0.04, 0.05 tasks: 110 total, 2 running, 108 sleeping, 0 stopped, 0 zombie %cpu(s): 0.0 us, 0.3 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st kib mem : 9383776 total, 8691232 free, 377512 used, 315032 buff/cache kib swap: 2097148 total, 2097148 free, 0 used. 8734152 avail mem pid user pr ni virt res shr s %cpu %mem time+ command 1 root 20 0 46092 6660 4176 s 0.0 0.1 0:01.49 systemd 2 root 20 0 0 0 0 s 0.0 0.0 0:00.00 kthreadd 4 root 0 -20 0 0 0 s 0.0 0.0 0:00.00 kworker/0:0h 6 root 20 0 0 0 0 s 0.0 0.0 0:00.20 ksoftirqd/0 7 root rt 0 0 0 0 s 0.0 0.0 0:00.00 migration/0 8 root 20 0 0 0 0 s 0.0 0.0 0:00.00 rcu_bh 9 root 20 0 0 0 0 s 0.0 0.0 0:00.57 rcu_sched 10 root 0 -20 0 0 0 s 0.0 0.0 0:00.00 lru-add-drain 11 root rt 0 0 0 0 s 0.0 0.0 0:00.03 watchdog/0 13 root 20 0 0 0 0 s 0.0 0.0 0:00.00 kdevtmpfs 14 root 0 -20 0 0 0 s 0.0 0.0 0:00.00 netns 15 root 20 0 0 0 0 s 0.0 0.0 0:00.00 khungtaskd 16 root 0 -20 0 0 0 s 0.0 0.0 0:00.00 writeback 17 root 0 -20 0 0 0 s 0.0 0.0 0:00.00 kintegrityd 18 root 0 -20 0 0 0 s 0.0 0.0 0:00.00 bioset 19 root 0 -20 0 0 0 s 0.0 0.0 0:00.00 bioset 20 root 0 -20 0 0 0 s 0.0 0.0 0:00.00 bioset 21 root 0 -20 0 0 0 s 0.0 0.0 0:00.00 kblockd 22 root 0 -20 0 0 0 s 0.0 0.0 0:00.00 md 23 root 0 -20 0 0 0 s 0.0 0.0 0:00.00 edac-poller 24 root 0 -20 0 0 0 s 0.0 0.0 0:00.00 watchdogd 30 root 20 0 0 0 0 s 0.0 0.0 0:00.00 kswapd0 31 root 25 5 0 0 0 s 0.0 0.0 0:00.00 ksmd 1524 root 20 0 1129720 59660 22144 s 0.0 0.6 0:01.11 dockerd
列出了一部分 下面还有
第二行:进程统计信息
110 total, 2 running, 108 sleeping, 0 stopped, 0 zombie
系统现在共有110个进程,其中处于运行中的有2个,108个在休眠,停止状态的有0个,僵尸状态的有0个
- 14:36:29:系统当前时间
- up 1:03:系统运行时间
2 users:登录系统的用户数 root 以及 还有一个自己建立的用户
- load average: 0.03, 0.04, 0.05:系统负载信息,平均1分钟、5分钟、15分钟的平均负载
- 0.0 us:用户空间占cpu百分比
- 0.3 sy:内核空间占cpu百分比
- 0.0 ni:改变过优先级的进程占cpu百分比
- 0.0 wa:io等待占cpu的比
- pid: 进程的id (如docker进程的id)
- user: 进程所有者
- pr:进程优先级,值越小,优先级越高
- ni:负值表示高优先级 正值表示低优先级
- virt:(virtual memory usage)虚拟内存,进程“需要的”虚拟内存大小”
- res: ( resident memory usage)常驻内存,进程当前使用的内存大小
- shr: (shared memory )共享内存,除了自身进程的共享内存,也包括其他进程的共享内存
- s列:状态列 s:睡眠状态 r:运行状态 d:不可中断的睡眠状态 z:僵尸进程
- %cpu : 上次更新到现在的cpu时间占用百分比
- %mem :进程使用的物理内存百分比
- time+ : 进程使用的cpu时间总计
- command :进程名称
计算某个进程所占的物理内存大小:res-shr
在top命令的运行界面中的一些内部命令
来控制top命令结果的显示方式
- 按下n:以 pid 的大小的顺序排列表示进程列表(默认从大到小)
- 按下p:以 cpu 占用率大小的顺序排列进程列表(默认从大到小)
- 按下m:以物理内存占用率大小的顺序排列进程列表(默认从大到小)
- 按下q/ctrl c:退出top命令
- 按下t:按照时间排序
- 确定目前的排序列并高亮显示: x(没有反应的话 先按b 再按x) 再按x退出
- 改变排序的列: shift + < / shift + >
- 高亮显示运行中的进程:y (没有反应的话 先按b 再按y)
- 切换列表中的数值单位:e
- 切换物理内存 交换区内存的数值单位:e
top -c:完整的显示命令行
command
:会显示完整
[root@ypcvm ~]# top -c top - 14:58:00 up 1:24, 2 users, load average: 0.06, 0.04, 0.05 tasks: 108 total, 2 running, 106 sleeping, 0 stopped, 0 zombie %cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st kib mem : 9383776 total, 8693936 free, 374796 used, 315044 buff/cache kib swap: 2097148 total, 2097148 free, 0 used. 8736872 avail mem pid user pr ni virt res shr s %cpu %mem time+ command 968 root 20 0 305284 6380 5004 s 0.3 0.1 0:07.63 /usr/bin/vmtoolsd 1966 root 20 0 0 0 0 r 0.3 0.0 0:00.53 [kworker/0:2] 1 root 20 0 46092 6660 4176 s 0.0 0.1 0:01.53 /usr/lib/systemd/systemd --switched-root --syste+ 2 root 20 0 0 0 0 s 0.0 0.0 0:00.00 [kthreadd] 4 root 0 -20 0 0 0 s 0.0 0.0 0:00.00 [kworker/0:0h] 6 root 20 0 0 0 0 s 0.0 0.0 0:00.27 [ksoftirqd/0] 7 root rt 0 0 0 0 s 0.0 0.0 0:00.00 [migration/0] 8 root 20 0 0 0 0 s 0.0 0.0 0:00.00 [rcu_bh] 9 root 20 0 0 0 0 s 0.0 0.0 0:00.60 [rcu_sched] 10 root 0 -20 0 0 0 s 0.0 0.0 0:00.00 [lru-add-drain] 11 root rt 0 0 0 0 s 0.0 0.0 0:00.04 [watchdog/0] 13 root 20 0 0 0 0 s 0.0 0.0 0:00.00 [kdevtmpfs] 14 root 0 -20 0 0 0 s 0.0 0.0 0:00.00 [netns] 15 root 20 0 0 0 0 s 0.0 0.0 0:00.00 [khungtaskd] 16 root 0 -20 0 0 0 s 0.0 0.0 0:00.00 [writeback] 17 root 0 -20 0 0 0 s 0.0 0.0 0:00.00 [kintegrityd] 18 root 0 -20 0 0 0 s 0.0 0.0 0:00.00 [bioset] 19 root 0 -20 0 0 0 s 0.0 0.0 0:00.00 [bioset] 20 root 0 -20 0 0 0 s 0.0 0.0 0:00.00 [bioset] 21 root 0 -20 0 0 0 s 0.0 0.0 0:00.00 [kblockd] 22 root 0 -20 0 0 0 s 0.0 0.0 0:00.00 [md] 23 root 0 -20 0 0 0 s 0.0 0.0 0:00.00 [edac-poller] 24 root 0 -20 0 0 0 s 0.0 0.0 0:00.00 [watchdogd] 30 root 20 0 0 0 0 s 0.0 0.0 0:00.00 [kswapd0] 31 root 25 5 0 0 0 s 0.0 0.0 0:00.00 [ksmd] 32 root 39 19 0 0 0 s 0.0 0.0 0:00.10 [khugepaged] 33 root 0 -20 0 0 0 s 0.0 0.0 0:00.00 [crypto] 1524 root 20 0 1129720 59660 22144 s 0.0 0.6 0:01.20 /usr/bin/dockerd -h fd:// --containerd=/run/containerd/containerd.sock -h tcp://0.0.0.0:2375 -h unix://var+
top -p 进程id :只显示指定进程id的进程
已知docker的进程id为1524
ps -ef命令查看某个进程的id
[root@ypcvm ~]# ps -ef|grep docker root 1524 1 0 13:33 ? 00:00:01 /usr/bin/dockerd -h fd:// --containerd=/run/containerd/containerd.sock -h tcp://0.0.0.0:2375 -h unix://var/run/docker.sock root 1991 1912 0 15:04 pts/1 00:00:00 grep --color=auto docker
top -p 1524
[root@ypcvm ~]# top -p 1524 top - 15:03:33 up 1:30, 2 users, load average: 0.01, 0.03, 0.05 tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie %cpu(s): 0.0 us, 0.3 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st kib mem : 9383776 total, 8693748 free, 374968 used, 315060 buff/cache kib swap: 2097148 total, 2097148 free, 0 used. 8736692 avail mem pid user pr ni virt res shr s %cpu %mem time+ command 1524 root 20 0 1129720 59660 22144 s 0.0 0.6 0:01.21 dockerd
top -h -p pid:显示指定进程中的线程情况
pid user pr ni virt res shr s %cpu %mem time+ command 1486 root 20 0 1137916 60260 22552 s 0.0 0.6 0:00.36 dockerd 1523 root 20 0 1137916 60260 22552 s 0.0 0.6 0:00.07 dockerd 1524 root 20 0 1137916 60260 22552 s 0.0 0.6 0:00.01 dockerd 1525 root 20 0 1137916 60260 22552 s 0.0 0.6 0:00.00 dockerd 1529 root 20 0 1137916 60260 22552 s 0.0 0.6 0:00.21 dockerd 1530 root 20 0 1137916 60260 22552 s 0.0 0.6 0:00.12 dockerd 1531 root 20 0 1137916 60260 22552 s 0.0 0.6 0:00.14 dockerd 1532 root 20 0 1137916 60260 22552 s 0.0 0.6 0:00.00 dockerd 1533 root 20 0 1137916 60260 22552 s 0.0 0.6 0:00.00 dockerd 1534 root 20 0 1137916 60260 22552 s 0.0 0.6 0:00.00 dockerd 1535 root 20 0 1137916 60260 22552 s 0.0 0.6 0:00.00 dockerd 1536 root 20 0 1137916 60260 22552 s 0.0 0.6 0:00.00 dockerd
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论