[Reserved] Linux top command Detailed

Reprinted: https: //www.cnblogs.com/ronli/p/centos-top.html

Gitlab recently installed in the docker (centos host a virtual machine) was found with the running time, the virtual machine's memory continued to rise, a few hours after running memory has been ringing off the hook, putty remote processing in a state of suspended animation.
This time you need to view the memory process, found that more than 10 ruby processes running, the final positioning problems docker container.

Instructions to use:

View memory usage

$ free -m
$ top //shift+m按内存占用比排序

Purge memory (not always effective, and ultimately can only be forced to shut down the virtual machine 0_0)

# sync 
# echo 3 > /proc/sys/vm/drop_caches

command is often used to monitor the top of linux system conditions, such as cpu, memory usage, the following is the meaning of the data.

First line:
13:42:59 current system time
6 days, 9:29 system has been running for six days and six hours and 29 minutes (during which had not restart)
3 the Users currently has three user login system
load average: 3.06, 3.01, 1.79 load average three digits after each 1 minute, 5 minutes, 15 minutes load.
load average is the number of data every five seconds to check a dynamic process, and then calculate the value of a specific algorithm. If this number is divided by the number of logical CPU, the result is higher than 5 when he shows that the system is overloaded.

Second row: Tasks task (process)
system now processes a total of 131, of which there are three in operation, the 127 has a 0 in the dormant (sleep), stoped state, there are zombie state (zombie) is one.

Third row: cpu state
the percentage of space occupied by the user's CPU 10.6% us.
2.2% sy kernel space occupied by the percentage of the CPU.
0.0% ni changed the priority of the process takes CPU percentage
84.5% id idle CPU percentage of
2.5% wa IO wait percentage of CPU of
0.1% hi hardware interrupt (Hardware IRQ) occupancy percentage of CPU
0.0% si software interrupt (Software Interrupts) the percentage of CPU-
here CPU use ratio of the different concepts and windows, if you do not understand user space and kernel space, the need to enrich my knowledge.

Fourth row: the memory state
8300124k total amount of physical memory (8GB)
the total amount of memory used in 5979476k used (5.7 GB)
2320648k Free free amount of memory (2.2G)
455544k amount of cache memory buffers (434M)

Fifth row: for swap partition
8193108k total amount exchange zone (8GB)
the total amount used 41568k used exchange zone (40.6M)
the total amount of idle 8151540k free exchange zone (8GB)
total exchange zone 4217456k cached buffer (4GB)

To note here is that these data can not understand the concept of windows of memory, if Weiyi by windows server this way: the total amount of memory 8G only 530M of memory available. Linux memory management has its particularity, complex needs a book to illustrate the point, here it is a simple point and say that our traditional concepts (windows) are different.

The total amount of memory used in the fourth row (used) refers to the number of the memory system is now controlled by the kernel, the total amount of free memory (Free) is a kernel which has not been included in the number of control range. Into the kernel memory management are not necessarily in use, also includes used in the past can now be reused in the memory, the kernel does not return these can be re-used memory to go free, so free memory will be on linux less and less, but do not worry about it.

If the habit to calculate the number of available memory, there is a similar formula: cached buffers free + the fourth row of the fourth row and the fifth row, according to this formula this server's available memory: 2,320,648 + 455,544 +4217456 = 6.6GB.

For memory monitoring, where we in the top fifth line should always monitor swap swap partition used, if this value is constantly changing, indicating ongoing core data exchange and swap memory, which is a real memory is not enough.

The sixth line is blank

Seventh line of the following: the status of each process (task) monitoring
PID process the above mentioned id
the USER process owner
PR process priority
NI nice value. A negative value indicates high priority, low priority positive value indicates
the total amount of virtual memory used by the process VIRT, unit kb. The SWAP the RES + = VIRT
the RES used in the process, are not swapped out of physical memory size in kb. The DATA CODE + = the RES
the SHR shared memory size in kB
S process state. D = uninterruptible sleep state R = running S = sleep T = trace / stop Z = zombie process
% CPU last update to the current CPU time occupancy percentage
percentage of physical memory% MEM process using
CPU time TIME + process uses the total, unit 1/100 sec
cOMMAND process name (command name / command line)

Multi-core CPU to monitor U
in the base top view, the keyboard number 1, can monitor the status of each logical CPU:

Observation views, the CPU server has four logical, is actually a physical CPU.

Sorting process field
default upon entering top, the processes in accordance with the occupancy of the CPU to sort, in the process [01] in the top view mysqld process ID 3527 is in the first row (cpu occupancy 2%), the process ID is 26955 java process came in second (cpu occupied by 1%).
Can be changed by keyboard commands sort fields, such as MEM want to monitor what the process takes at most, I generally used as follows:

  1. Keystrokes B (open / close the highlight), top view change as follows:

We found that the process id of the process 20517 top is highlighted, and generally running state (Runing with) the process was only highlighted by closing or opening the tap y to highlight the effect of the operating mode of the process.

  1. Keystrokes x (opening / closing of the highlight sorted column), top view change as follows:

You can see, top by default sort column is% CPU.

  1. By shift +> or shift + <can be right or left to change the sort column, the figure is press shift +> renderings:

View has now been sorted according to the% MEM.

Change the course of the display field

  1. F tap key, top view into another, where the fields may display basic layout view:

Here are all processes that can be displayed in the top field base view, there and uppercase letters labeled fields are displayed, no and lowercase letters of the field is not displayed. To display the two fields in the CODE and DATA base view, r and s by tapping keys:

  1. Carriage Return base view, you can see more than two fields CODE and DATA:

Supplementary top command
top command is the preferred command system monitoring on Linux, but sometimes did not meet our requirements, monitoring is the smallest unit of top command of the process, so do the number of threads and number of connections less than the customer program, usually ps and can netstate two commands to supplement the top.

监控java线程数:
ps -eLf | grep java | wc -l
监控网络客户连接数:
netstat -n | grep tcp | grep 侦听端口 | wc -l
上面两个命令,可改动grep的参数,来达到更细致的监控要求。

在Linux系统一切都是文件的思想贯彻指导下,所有进程的运行状态都可以用文件来获取。系统根目录/proc中,每一个数字子目录的名字都是运行中的进程的PID,进入任一个进程目录,可通过其中文件或目录来观察进程的各项运行指标,例如task目录就是用来描述进程中线程的,因此也可以通过下面的方法获取某进程中运行中的线程数量(PID指的是进程ID):

ls /proc/PID/task | wc -l
在linux中还有一个命令pmap,来输出进程内存的状况,可以用来分析线程堆栈:
pmap PID

转载:https://www.cnblogs.com/ronli/p/centos-top.html

最近在docker(宿主机是centos虚拟机)里安装gitlab,发现随着时间的运行,虚拟机的内存持续走高,运行几个小时之后内存已经爆掉了,putty远程处理于假死状态。
这个时候就需要查看内存进程,发现有10多个ruby进程在运行,最终定位到docker容器的问题。

使用到的指令:

查看内存使用情况

$ free -m
$ top //shift+m按内存占用比排序

清理内存(效果不是很理想,最终只能强制关闭虚拟机0_0)

# sync 
# echo 3 > /proc/sys/vm/drop_caches

top命令经常用来监控linux的系统状况,比如cpu、内存的使用,下面是各个数据的含义。

第一行:
13:42:59 当前系统时间
6 days, 9:29 系统已经运行了6天6小时29分钟(在这期间没有重启过)
3 users 当前有3个用户登录系统
load average: 3.06,3.01, 1.79 load average后面的三个数分别是1分钟、5分钟、15分钟的负载情况。
load average数据是每隔5秒钟检查一次活跃的进程数,然后按特定算法计算出的数值。如果这个数除以逻辑 CPU的数量,结果高于5的时候就表明系统在超负荷运转了。

第二行: Tasks 任务(进程)
系统现在共有131个进程,其中处于运行中的有3个,127个在休眠(sleep),stoped状态的有0个,zombie状态(僵尸)的有1个。

第三行:cpu状态
10.6% us 用户空间占用CPU的百分比。
2.2% sy 内核空间占用CPU的百分比。
0.0% ni 改变过优先级的进程占用CPU的百分比
84.5% id 空闲CPU百分比
2.5% wa IO等待占用CPU的百分比
0.1% hi 硬中断(Hardware IRQ)占用CPU的百分比
0.0% si 软中断(Software Interrupts)占用CPU的百分比
在这里CPU的使用比率和windows概念不同,如果你不理解用户空间和内核空间,需要充充电了。

第四行:内存状态
8300124k total 物理内存总量(8GB)
5979476k used 使用中的内存总量(5.7GB)
2320648k free 空闲内存总量(2.2G)
455544k buffers 缓存的内存量 (434M)

第五行:swap交换分区
8193108k total 交换区总量(8GB)
41568k used 使用的交换区总量(40.6M)
8151540k free 空闲交换区总量(8GB)
4217456k cached 缓冲的交换区总量(4GB)

这里要说明的是不能用windows的内存概念理解这些数据,如果按windows的方式此台服务器危矣:8G的内存总量只剩下530M的可用内存。Linux的内存管理有其特殊性,复杂点需要一本书来说明,这里只是简单说点和我们传统概念(windows)的不同。

第四行中使用中的内存总量(used)指的是现在系统内核控制的内存数,空闲内存总量(free)是内核还未纳入其管控范围的数量。纳入内核管理的内存不见得都在使用中,还包括过去使用过的现在可以被重复利用的内存,内核并不把这些可被重新使用的内存交还到free中去,因此在linux上free内存会越来越少,但不用为此担心。

如果出于习惯去计算可用内存数,这里有个近似的计算公式:第四行的free + 第四行的buffers + 第五行的cached,按这个公式此台服务器的可用内存: 2320648+455544 +4217456 = 6.6GB。

对于内存监控,在top里我们要时刻监控第五行swap交换分区的used,如果这个数值在不断的变化,说明内核在不断进行内存和swap的数据交换,这是真正的内存不够用了。

第六行是空行

第七行以下:各进程(任务)的状态监控
PID 进程id
USER 进程所有者
PR 进程优先级
NI nice值。负值表示高优先级,正值表示低优先级
VIRT 进程使用的虚拟内存总量,单位kb。VIRT=SWAP+RES
RES 进程使用的、未被换出的物理内存大小,单位kb。RES=CODE+DATA
SHR 共享内存大小,单位kb
S 进程状态。D=不可中断的睡眠状态 R=运行 S=睡眠 T=跟踪/停止 Z=僵尸进程
%CPU 上次更新到现在的CPU时间占用百分比
%MEM 进程使用的物理内存百分比
TIME+ 进程使用的CPU时间总计,单位1/100秒
COMMAND 进程名称(命令名/命令行)

多U多核CPU监控
在top基本视图中,按键盘数字1,可监控每个逻辑CPU的状况:

观察视图,服务器有4个逻辑CPU,实际上是1个物理CPU。

进程字段排序
默认进入top时,各进程是按照CPU的占用量来排序的,在【top视图 01】中进程ID为3527的mysqld进程排在第一(cpu占用2%),进程ID为26955的java进程排在第二(cpu占用1%)。
可通过键盘指令来改变排序字段,比如想监控哪个进程占用MEM最多,我一般的使用方法如下:

  1. 敲击键盘b(打开/关闭加亮效果),top的视图变化如下:

我们发现进程id为20517的top进程被加亮了,一般为运行状态(runing)的进程才被加亮,可以通过敲击y键关闭或打开运行态进程的加亮效果。

  1. 敲击键盘x(打开/关闭排序列的加亮效果),top的视图变化如下:

可以看到,top默认的排序列是%CPU。

  1. 通过shift + >或shift + <可以向右或左改变排序列,下图是按一次shift + >的效果图:

视图现在已经按照%MEM来排序了。

改变进程显示字段

  1. 敲击f键,top进入另一个视图,在这里可以编排基本视图中的显示字段:

这里列出了所有可在top基本视图中显示的进程字段,有并且标注为大写字母的字段是可显示的,没有并且是小写字母的字段是不显示的。如果要在基本视图中显示CODE和DATA两个字段,可以通过敲击r和s键:

  1. 回车返回基本视图,可以看到多了CODE和DATA两个字段:

top命令的补充
top命令是Linux上进行系统监控的首选命令,但有时候却达不到我们的要求,top命令的监控最小单位是进程,所以看不到程序的线程数和客户连接数,通常可以ps和netstate两个命令来补充top的不足。

监控java线程数:
ps -eLf | grep java | wc -l
监控网络客户连接数:
netstat -n | grep tcp | grep 侦听端口 | wc -l
上面两个命令,可改动grep的参数,来达到更细致的监控要求。

在Linux系统一切都是文件的思想贯彻指导下,所有进程的运行状态都可以用文件来获取。系统根目录/proc中,每一个数字子目录的名字都是运行中的进程的PID,进入任一个进程目录,可通过其中文件或目录来观察进程的各项运行指标,例如task目录就是用来描述进程中线程的,因此也可以通过下面的方法获取某进程中运行中的线程数量(PID指的是进程ID):

ls /proc/PID/task | wc -l
在linux中还有一个命令pmap,来输出进程内存的状况,可以用来分析线程堆栈:
pmap PID

Guess you like

Origin www.cnblogs.com/chenyangqit/p/11578177.html