Linux进程管理——查看内存的工具

Linux进程管理——查看内存的工具

一查看内存的工具vmstat


vmstat命令:虚拟内存信息
vmstat [options] [delay [count]]
vmstat 2 5

[root@centos72 ~]# vmstat 
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 610224   2076 279392    0    0    32     9  316   55  8 19 73  1  0
[root@centos72 ~]# rpm -q  vmstat
package vmstat is not installed
[root@centos72 ~]# type  vmstat 
vmstat is hashed (/usr/bin/vmstat)
[root@centos72 ~]# free
              total        used        free      shared  buff/cache   available
Mem:         997980      106532      609936        7832      281512      705116
Swap:       2097148           0     2097148

详细选项:
procs:
r:可运行(正运行或等待运行)进程的个数,和核心数有关
b:处于不可中断睡眠态的进程个数(被阻塞的队列的长度)
memory:
swpd: 交换内存的使用总量
free:空闲物理内存总量
buffer:用于buffer的内存总量
cache:用于cache的内存总量

注意buffer和cache的区别

swap:
si:从磁盘交换进内存的数据速率(kb/s)
so:从内存交换至磁盘的数据速率(kb/s)

查看帮助文档

   Swap
       si: Amount of memory swapped in from disk (/s).
       so: Amount of memory swapped to disk (/s).

io:
bi:从块设备读入数据到系统的速率(kb/s),也就是出磁盘进内存
bo: 保存数据至块设备的速率,也就是进磁盘出内存

 IO
       bi: Blocks received from a block device (blocks/s).
       bo: Blocks sent to a block device (blocks/s).

设置1秒执行一次

[root@centos72 ~]#   vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 758312   2076 134872    0    0    56     5   68   82  0  0 98  2  0
 0  0      0 758288   2076 134872    0    0     0     0   55   41  0  0 100  0  0
 0  0      0 758288   2076 134872    0    0     0     0   52   46  0  0 100  0  0
 0  0      0 758288   2076 134872    0    0     0     0   52   45  0  0 100  0  0
 0  0      0 758288   2076 134872    0    0     0     0   56   48  0  0 100  0  0
 0  0      0 758288   2076 134872    0    0     0     0   45   33  0  0 100  0  0
 0  0      0 758288   2076 134872    0    0     0     0   50   42  0  0 100  0  0

top命令也有一样的选项

top - 00:50:39 up 49 min,  2 users,  load average: 0.00, 0.01, 0.05
Tasks: 104 total,   1 running, 103 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 :   997980 total,   757148 free,   103336 used,   137496 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   733212 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                           
     1 root      20   0  193316   6412   4080 S   0.0  0.6   0:01.47 systemd                           
     2 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kthreadd                          
     3 root      20   0       0      0      0 S   0.0  0.0   0:00.02 ksoftirqd/0                       
     5 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.00 kworker/u256:0                    
     7 root      rt   0       0      0      0 S   0.0  0.0   0:00.04 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.33 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.01 watchdog/0                        
    12 root      rt   0       0      0      0 S   0.0  0.0   0:00.01 watchdog/1                        
    13 root      rt   0       0      0      0 S   0.0  0.0   0:00.00 migration/1                       
    14 root      20   0       0      0      0 S   0.0  0.0   0:00.01 ksoftirqd/1                       
    16 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/1:0H                      
    18 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kdevtmpfs  

system:
in: interrupts 中断速率,包括时钟
cs: context switch 进程切换速率,切换高不好

这和学习一样,要集中精力才会效率高

cpu:
us:Time spent running non-kernel code
sy: Time spent running kernel code
id: Time spent idle. Linux 2.5.41前,包括IO-wait time.
wa: Time spent waiting for IO. 2.5.41前,包括in idle.
st: Time stolen from a virtual machine. 2.6.11前, unknown.

开启一个进程,可以看到刚开始是出内存的多

因为内存空间满了就要里面的内容导到交换分区里面

[root@centos72 ~]#   vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 3  0      0 138112   2076 135496    0    0    15     1   40   38  0  0 99  0  0
 0  7 135944  48976      0  67688  128 227088  9540 227089 5203  777  0 10 14 76  0
 0  8 280072  48764      0  59576  224 63932  6664 63948 1314  244  0  3  0 97  0
 0  7 317960  48800      0  63960    8 26792   256 26792  646  241  0  1  0 99  0
 0  8 355080  64672      0  65012  276 37012  1348 37012 1428  218  0  3  7 91  0
 0  8 402696  64132      0  65596   96 47628   260 47628  210   60  0  2  0 99  0
 0 10 429832  49148      0  65448  120 27140   304 27140  248  101  0  1  0 99  0
 0 10 489224  48824      0  58192  128 91312  8772 91312 2527  384  0  5  0 95  0
 1  9 567296  72648      0  61268   96 46084   220 46084  355  164  0  1  0 99  0
 4  9 582152  48704      0  60316  280 14696   676 14696 12712  295  0 33  0 67  0
 4  9 590344  48880      0  61336   32 8456  1096  8456  102   50  0  9  0 91  0
 8  9 618240  50096      0  60216   52 28704   316 28704  902  134  0 11  0 89  0
14 10 651520  48860      0  61432   32 32524    32 32524 1077  193  0 14  0 86  0
 2 12 686080  48812      0  61740   32 34676   144 34676  702  140  0  9 62 29  0
 9  8 702720  49164      0  60936  176 16480   436 16480 4339   89  0 20  0 80  0
10  6 747776  48884      0  61076  256 45080   592 45080 4401  104  0 62  0 38  0
 9  7 764048  49992      0  60044  204 16836   584 16836 3782   69  0 25  0 75  0
12  7 838912  56412      0  61520  128 74664   296 74664  385   91  0 14  0 86  0
 2 10 866560  52044      0  63116  288 27408  1476 27408 4312  334  0 16  0 84  0

让bi值变大,那么就要从磁盘读取数据,也就是出磁盘进内存

比如从第1块硬盘读数据到垃圾箱里面

[root@centos72 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        50G  1.3G   49G   3% /
devtmpfs        477M     0  477M   0% /dev
tmpfs           488M     0  488M   0% /dev/shm
tmpfs           488M  7.6M  480M   2% /run
tmpfs           488M     0  488M   0% /sys/fs/cgroup
/dev/sda3        20G   33M   20G   1% /app
/dev/sda1      1014M  124M  891M  13% /boot
tmpfs            98M     0   98M   0% /run/user/0
[root@centos72 ~]# dd   if=/dev/sda    of=/dev/null  
[root@centos72 ~]#   vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0  49864 815124      0 108072    0   13     3    14   25   21  0  0 100  0  0
 0  0  49864 815124      0 108132    0    0     0     0   64   48  0  0 100  0  0
 0  0  49864 815124      0 108132    0    0     0     0   55   43  0  0 100  0  0
 0  0  49864 815124      0 108132    0    0     0     0   54   46  0  0 100  0  0
 1  0  49864 770112  44880 108124    0    0 44880     0  485  178  0 10 66 24  0
 0  1  49864 688412 126800 108104    0    0 81920     0  878  255  1 23 50 27  0
 1  0  49864 528276 286544 108496    0    0 159744     0 1229   63  1 43 50  7  0
 1  0  49864 376412 438096 108768    0    0 151552     0 1295   55  1 46 51  2  0
 1  0  49864 236780 577360 109164    0    0 139264     0 1226   65  1 37 49 13  0
 1  0  49864  72624 741200 109524    0    0 163840     0 1438   83  1 45 50  6  0
 0  1  49864  72048 774588  76588    0    0 282640   395 1588  206  6 37 48  9  0
 1  0  49864  62660 783960  76780    0    0 69632     0  424  129  2  7 50 41  0
 0  1  49864  77532 768896  76824    0    0 57344     0  381  237  1  5 50 44  0
 0  1  49864  73128 773868  76776    0    0 53248     0  362  195  2  5 50 44  0
 0  1  49864  68116 778744  76876    0    0 53248     0  373  182  1  6 50 43  0
 0  1  49864  81316 765416  76972    0    0 28672     0  225  128  1  3 50 47  0
 0  1  49864  77328 770300  76932    0    0 53248     0  334  164  1  6 50 43  0
 0  1  49864  80312 767308  77004    0    0 69632     2  549  377  2  7 51 41  0
 0  1  49864  65336 782208  77044    0    0 32768     0  205   79  1  3 50 46  0
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0  49864  61616 789040  75848    0    0 139264     0  731  144  3 14 50 34  0
 1  0  49864  73440 778604  76144    0    0 368640     0 1803  192  9 36 48  7  0
 1  0  49864  68620 781404  76724    0    0 355840     0 1620  127  9 33 48  9  0
 1  0  49864  64616 787500  76324    0    0 356864     0 1643  130  9 34 48  9  0
 1  0  49864  66344 785224  76788    0    0 282636     8 1469  110 10 35 50  5  0
 2  0  49864  77792 773020  77444    0    0 315392     0 1497   87 10 34 49  7  0
 1  0  49864  80512 769484  78296    0    0 311380     0 1573  133  9 37 51  3  0
 1  0  49864  66492 780540  78940    0    0 359424     0 1628   99 10 34 48  7  0
 1  0  49864  67068 784196  76760    0    0 353280     0 1608  126  9 34 49  9  0
 1  0  49864  81676 768808  77612    0    0 360456    20 1633  112  9 36 48  8  0
 0  1  49864  64044 785580  78316    0    0 344064     0 1554  112  8 33 49 10  0
 0  1  49864  69752 779612  78728    0    0 393216     0 1885  197 10 40 48  4  0
 1  0  49864  62060 787120  78864    0    0 397312     0 1909  212 10 37 48  5  0
 1  0  49864  64728 784528  78816    0    0 360448     0 1837  210  9 36 48  7  0
 1  0  49864  66900 782292  78908    0    0 372736     0 1839  215  9 37 48  7  0
 1  0  49864  69976 779204  79032    0    0 323584     0 1541  175  8 30 49 13  0
 1  0  49864  71012 778260  78928    0    0 376832     0 1838  239  9 36 48  7  0
 1  0  49864  68520 780676  78680    0    0 425984     0 2076  245 10 41 48  1  0
 1  0  49864  75060 774076  78792    0    0 331776     0 1723  225 10 34 48  9  0

增加bo值

[root@centos72 ~]# dd   if=/dev/sda    of=/f1
[root@centos72 ~]#   vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0  49864  73816 771396  83032    0   13   668    14   29   21  0  0 100  0  0
 0  0  49864  73692 771396  83008    0    0     0     0   65   51  0  0 100  0  0
 1  0  49864  80052 745316 102564    0    0 24436     0  311  134  0  7 80 13  0
 2  1  49864  74568 690624 162564    0    0 65536 76112  674  298  2 18 46 35  0
 7  1  49864  71944 680280 175740    0    0  8192     0  284   49  3 18 59 21  0
 8  0  49864  76776 611136 238508    0    0 59904  2048  706  143  2 24 52 21  0
 4  0  49864  64664 594468 267612    0    0 28672 30723  367   95  6 70 24  0  0
 3  1  49864  74676 576804 276384    0    0  9728 72016  168   44  0  9 63 28  0
 3  1  49864  76316 566944 284776    0    0  8192     0  136   37  3 18 26 54  0
11  1  49864  76316 566944 284776    0    0     0     0   90   13  0 100  0  0  0
 5  0  49864  75400 497952 354624    0    0 65536 10752  908  197  3 29 45 23  0
 7  1  49864  64288 487396 376180    0    0 32768 98996  269  119  5 75 20  0  0
 5  1  49864  77980 452188 398160    0    0 12288     0  443   65  2 16 60 23  0
 1  0  49864  62728 388992 476024    0    0 77824 28672 1011  159  4 52 39  4  0
 3  1  49864  72916 372744 482036    0    0  8192     0   78   34  6 19 56 19  0
 5  0  49864  64288 362128 498940    0    0 13824 16896  251   59  5 59 18 18  0
 6  0  49864  78640 332404 516352    0    0 18944 74536  221   49  2 21 42 35  0

-s: 显示内存的统计数据

[root@centos72 ~]#   vmstat -s
       997980 K total memory
        69504 K used memory
       387508 K active memory
       403720 K inactive memory
        81120 K free memory
       180292 K buffer memory
       667064 K swap cache
      2097148 K total swap
        49864 K used swap
      2047284 K free swap
         3626 non-nice user cpu ticks
            2 nice user cpu ticks
        15279 system cpu ticks
      7273227 idle cpu ticks
        17014 IO-wait cpu ticks
            0 IRQ cpu ticks
          496 softirq cpu ticks
            0 stolen cpu ticks
     50988602 pages paged in
      3212894 pages paged out
         5460 pages swapped in
       243937 pages swapped out
      2116104 interrupts
      1576029 CPU context switches
   1558281696 boot time
         1757 forks

定义显示次数

[root@centos72 ~]# vmstat  1  3
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0  48584  64156 403812 459696    0   13  1653    59   35   23  0  0 99  0  0
 2  0  48584  64308 404496 458928    0    0 376832     0 2437  243 10 37 48  6  0
 0  1  48584  63416 406468 457848    0    0 401408     0 2549  260  8 41 47  4  0
[root@centos72 ~]# vmstat  1  6
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  1  48584  69340 398596 459876    0   13  1682    59   35   23  0  0 99  0  0
 0  1  48584  69576 398320 459772    0    0 61440     0  579  257  2  6 50 42  0
 1  0  48584  64820 403060 459828    0    0 45056     0  427  166  1  5 50 45  0
 0  1  48584  61972 406012 459792    0    0 77824     0  649  244  2  8 50 40  0
 0  1  48584  63092 404844 459748    0    0 73728     0  618  278  2  7 50 41  0
 0  1  48584  73948 394792 458848    0    0 69632     0  588  236  1  7 50 42  0

二iostat:统计CPU和设备IO信息

[root@centos72 ~]# yum  repolist 
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
base                                                                             | 3.6 kB  00:00:00     
repo id                                           repo name                                       status
base                                              base                                            3,971
repolist: 3,971
[root@centos72 ~]# yum  whatprovides  iostat
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
sysstat-10.1.5-13.el7.x86_64 : Collection of performance monitoring tools for Linux
Repo        : base
Matched from:
Filename    : /usr/bin/iostat



[root@centos72 ~]# yum  install   sysstat  -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package sysstat.x86_64 0:10.1.5-13.el7 will be installed
--> Processing Dependency: libsensors.so.4()(64bit) for package: sysstat-10.1.5-13.el7.x86_64
--> Running transaction check
---> Package lm_sensors-libs.x86_64 0:3.4.0-4.20160601gitf9185e5.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================
 Package                  Arch            Version                                   Repository     Size
========================================================================================================
Installing:
 sysstat                  x86_64          10.1.5-13.el7                             base          310 k
Installing for dependencies:
 lm_sensors-libs          x86_64          3.4.0-4.20160601gitf9185e5.el7            base           41 k

Transaction Summary
========================================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 352 k
Installed size: 1.2 M
Downloading packages:
--------------------------------------------------------------------------------------------------------
Total                                                                   705 kB/s | 352 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : lm_sensors-libs-3.4.0-4.20160601gitf9185e5.el7.x86_64                                1/2 
  Installing : sysstat-10.1.5-13.el7.x86_64                                                         2/2 
  Verifying  : sysstat-10.1.5-13.el7.x86_64                                                         1/2 
  Verifying  : lm_sensors-libs-3.4.0-4.20160601gitf9185e5.el7.x86_64                                2/2 

Installed:
  sysstat.x86_64 0:10.1.5-13.el7                                                                        

Dependency Installed:
  lm_sensors-libs.x86_64 0:3.4.0-4.20160601gitf9185e5.el7                                               

Complete!

下面都是完整的单词,之前是简写

avg-cpu: %user %nice %system %iowait %steal %idle

tps  每秒钟处理的事务

kB_read/s  每秒钟从磁盘读取的字节数?

kB_wrtn/s 每秒钟从磁盘写入的字节数?

kB_read

kB_wrtn

[root@centos72 ~]# iostat 
Linux 3.10.0-862.el7.x86_64 (centos72.huawei.com)     05/20/2019     _x86_64_    (2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.05    0.00    0.22    0.25    0.00   99.49

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
scd0              0.01         0.08         0.00       2978          0
sda               3.11      1378.58        86.82   51100818    3218055
[root@centos65 ~]# iostat 
Linux 2.6.32-642.el6.x86_64 (centos65.jd.com)     05/20/2019     _x86_64_    (2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.00    0.00    0.04    0.08    0.00   99.87

Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
sda               0.29         6.47         1.34     238874      49352
scd0              0.00         0.01         0.00        528          0

从磁盘里读数据,并且设置时间间隔显示一次

[root@centos72 ~]# dd   if=/dev/sda    of=/dev/null
[root@centos72 ~]# iostat  1
Linux 3.10.0-862.el7.x86_64 (centos72.huawei.com)     05/20/2019     _x86_64_    (2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.05    0.00    0.23    0.26    0.00   99.46

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
scd0              0.01         0.08         0.00       2978          0
sda               3.25      1412.10       117.79   53159598    4434194

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           5.56    0.00   22.22   24.24    0.00   47.98

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
scd0              0.00         0.00         0.00          0          0
sda             470.00    240136.00         0.00     240136          0

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           1.01    0.00    4.55   44.44    0.00   50.00

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
scd0              0.00         0.00         0.00          0          0
sda              93.00     47616.00         0.00      47616          0

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           1.03    0.00    4.62   43.59    0.00   50.77

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
scd0              0.00         0.00         0.00          0          0
sda             128.00     65536.00         0.00      65536          0

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           1.53    0.00    5.10   43.37    0.00   50.00

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
scd0              0.00         0.00         0.00          0          0
sda             124.00     63488.00         0.00      63488          0

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           1.00    0.00    4.00   45.00    0.00   50.00

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
scd0              0.00         0.00         0.00          0          0
sda              87.00     44544.00         0.00      44544          0

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           1.02    0.00    4.57   44.16    0.00   50.25

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
scd0              0.00         0.00         0.00          0          0
sda              98.00     50176.00         0.00      50176          0

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           1.03    0.00    6.15   42.56    0.00   50.26

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
scd0              0.00         0.00         0.00          0          0
sda             132.00     67584.00         0.00      67584          0

此命令的作用就是可以查看磁盘访问的压力大小

定义显示次数

[root@centos72 ~]# iostat  1  4
Linux 3.10.0-862.el7.x86_64 (centos72.huawei.com)     05/20/2019     _x86_64_    (2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.08    0.00    0.35    0.38    0.00   99.19

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
scd0              0.01         0.08         0.00       2978          0
sda               5.76      2694.01       117.16  101963082    4434320

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
          10.05    0.00   40.70    3.52    0.00   45.73

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
scd0              0.00         0.00         0.00          0          0
sda             835.00    426092.00         0.00     426092          0

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           8.50    0.00   35.00    8.50    0.00   48.00

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
scd0              0.00         0.00         0.00          0          0
sda             697.03    356879.21         0.00     360448          0

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           9.60    0.00   35.86    7.07    0.00   47.47

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
scd0              0.00         0.00         0.00          0          0
sda             736.00    376832.00         0.00     376832          0

三pmap命令

pmap命令:进程对应的内存映射,也就是进程使用的内存空间
pmap [options] pid [...]
-x: 显示详细格式的信息

[root@centos72 ~]# dd   if=/dev/sda    of=/dev/null

调用了很多库,也是要占空间的

很多模块都可能使用到相同的库,相当于公共设施

[root@centos72 ~]# pstree -p
systemd(1)─┬─NetworkManager(557)─┬─{NetworkManager}(594)
           │                     └─{NetworkManager}(596)
           ├─VGAuthService(546)
           ├─agetty(573)
           ├─auditd(517)───{auditd}(518)
           ├─crond(567)
           ├─dbus-daemon(550)───{dbus-daemon}(556)
           ├─irqbalance(549)
           ├─master(873)─┬─pickup(1594)
           │             └─qmgr(875)
           ├─polkitd(540)─┬─{polkitd}(576)
           │              ├─{polkitd}(577)
           │              ├─{polkitd}(582)
           │              ├─{polkitd}(586)
           │              └─{polkitd}(587)
           ├─rsyslogd(791)─┬─{rsyslogd}(793)
           │               └─{rsyslogd}(794)
           ├─sshd(788)─┬─sshd(1076)───bash(1081)───dd(1729)
           │           └─sshd(1599)───bash(1603)───pstree(1743)
           ├─systemd-journal(356)
           ├─systemd-logind(543)
           ├─systemd-udevd(390)
           ├─tuned(789)─┬─{tuned}(1030)
           │            ├─{tuned}(1031)
           │            ├─{tuned}(1032)
           │            └─{tuned}(1046)
           └─vmtoolsd(547)───{vmtoolsd}(579)
[root@centos72 ~]# pmap  1729
1729:   dd if=/dev/sda of=/dev/null
0000000000400000     68K r-x-- dd
0000000000610000      4K r---- dd
0000000000611000      4K rw--- dd
0000000000de9000    132K rw---   [ anon ]
00007fd21a123000 103588K r---- locale-archive
00007fd22064c000   1804K r-x-- libc-2.17.so
00007fd22080f000   2044K ----- libc-2.17.so
00007fd220a0e000     16K r---- libc-2.17.so
00007fd220a12000      8K rw--- libc-2.17.so
00007fd220a14000     20K rw---   [ anon ]
00007fd220a19000    136K r-x-- ld-2.17.so
00007fd220c31000     12K rw---   [ anon ]
00007fd220c39000      4K rw---   [ anon ]
00007fd220c3a000      4K r---- ld-2.17.so
00007fd220c3b000      4K rw--- ld-2.17.so
00007fd220c3c000      4K rw---   [ anon ]
00007ffeeedc9000    132K rw---   [ stack ]
00007ffeeedf7000      8K r-x--   [ anon ]
ffffffffff600000      4K r-x--   [ anon ]
 total           107996K

另外一种实现:
cat /proc/PID/maps

实际上是看此文件的内容,但是这样看易读性差

做开发可能会涉及到内存泄漏问题

内存泄漏就是进程占用了内存之后一直不释放内存,占满了还会继续占用,直到所有的内存占满导致系统奔溃

所以运维和开发不分家,平时遇到了什么问题要和开发沟通

[root@centos72 ~]# ls /proc/1729/maps  -l
-r--r--r--. 1 root root 0 May 20 10:35 /proc/1729/maps
[root@centos72 ~]# cat  /proc/1729/maps
00400000-00411000 r-xp 00000000 08:02 21443                              /usr/bin/dd
00610000-00611000 r--p 00010000 08:02 21443                              /usr/bin/dd
00611000-00612000 rw-p 00011000 08:02 21443                              /usr/bin/dd
00de9000-00e0a000 rw-p 00000000 00:00 0                                  [heap]
7fd21a123000-7fd22064c000 r--p 00000000 08:02 228723                     /usr/lib/locale/locale-archive
7fd22064c000-7fd22080f000 r-xp 00000000 08:02 33600202                   /usr/lib64/libc-2.17.so
7fd22080f000-7fd220a0e000 ---p 001c3000 08:02 33600202                   /usr/lib64/libc-2.17.so
7fd220a0e000-7fd220a12000 r--p 001c2000 08:02 33600202                   /usr/lib64/libc-2.17.so
7fd220a12000-7fd220a14000 rw-p 001c6000 08:02 33600202                   /usr/lib64/libc-2.17.so
7fd220a14000-7fd220a19000 rw-p 00000000 00:00 0 
7fd220a19000-7fd220a3b000 r-xp 00000000 08:02 33600195                   /usr/lib64/ld-2.17.so
7fd220c31000-7fd220c34000 rw-p 00000000 00:00 0 
7fd220c39000-7fd220c3a000 rw-p 00000000 00:00 0 
7fd220c3a000-7fd220c3b000 r--p 00021000 08:02 33600195                   /usr/lib64/ld-2.17.so
7fd220c3b000-7fd220c3c000 rw-p 00022000 08:02 33600195                   /usr/lib64/ld-2.17.so
7fd220c3c000-7fd220c3d000 rw-p 00000000 00:00 0 
7ffeeedc9000-7ffeeedea000 rw-p 00000000 00:00 0                          [stack]
7ffeeedf7000-7ffeeedf9000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]

文件描述符表示进程打开了文件数量

如果数量不断增加说明有问题,因为内存空间是有限的,进程占用的资源是有限的

如果出现此问题那么就要找开发

[root@centos72 ~]# ls /proc/1800/fd
0  1  2
[root@centos72 ~]# 
[root@centos72 ~]# ls /proc/1800/fd
0  1  2
[root@centos72 ~]# ls /proc/1800/fd
0  1  2
[root@centos72 ~]# ls /proc/1800/fd
0  1  2
[root@centos72 ~]# ls /proc/1800/fd
0  1  2
[root@centos72 ~]# ls /proc/1800/fd
0  1  2
[root@centos72 ~]# ls /proc/1800/fd  -al
total 0
dr-x------. 2 root root  0 May 20 10:46 .
dr-xr-xr-x. 9 root root  0 May 20 10:45 ..
lr-x------. 1 root root 64 May 20 10:46 0 -> /dev/sda
l-wx------. 1 root root 64 May 20 10:46 1 -> /dev/null
lrwx------. 1 root root 64 May 20 10:46 2 -> /dev/pts/1

在另外一个终端打开vim进程

[root@centos72 ~]# vim  f1

如果是vim进程就会打开交换文件

如果正常退出此文件就没有了,异常退出就会有此文件要手动删除

[root@centos72 ~]# pstree -p
systemd(1)─┬─NetworkManager(557)─┬─{NetworkManager}(594)
           │                     └─{NetworkManager}(596)
           ├─VGAuthService(546)
           ├─agetty(573)
           ├─auditd(517)───{auditd}(518)
           ├─crond(567)
           ├─dbus-daemon(550)───{dbus-daemon}(556)
           ├─irqbalance(549)
           ├─master(873)─┬─pickup(1594)
           │             └─qmgr(875)
           ├─polkitd(540)─┬─{polkitd}(576)
           │              ├─{polkitd}(577)
           │              ├─{polkitd}(582)
           │              ├─{polkitd}(586)
           │              └─{polkitd}(587)
           ├─rsyslogd(791)─┬─{rsyslogd}(793)
           │               └─{rsyslogd}(794)
           ├─sshd(788)─┬─sshd(1076)───bash(1081)───vim(1840)
           │           └─sshd(1599)───bash(1603)───pstree(1841)
           ├─systemd-journal(356)
           ├─systemd-logind(543)
           ├─systemd-udevd(390)
           ├─tuned(789)─┬─{tuned}(1030)
           │            ├─{tuned}(1031)
           │            ├─{tuned}(1032)
           │            └─{tuned}(1046)
           └─vmtoolsd(547)───{vmtoolsd}(579)
[root@centos72 ~]# ls /proc/1840/fd
0  1  2  4
[root@centos72 ~]# ls /proc/1840/fd  -al
total 0
dr-x------. 2 root root  0 May 20 10:53 .
dr-xr-xr-x. 9 root root  0 May 20 10:53 ..
lrwx------. 1 root root 64 May 20 10:53 0 -> /dev/pts/1
lrwx------. 1 root root 64 May 20 10:53 1 -> /dev/pts/1
lrwx------. 1 root root 64 May 20 10:53 2 -> /dev/pts/1
lrwx------. 1 root root 64 May 20 10:53 4 -> /root/.f1.swp


作者:wang618
出处:https://www.cnblogs.com/wang618/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/wang618/p/11208442.html