<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          手把手教你搞定Linux中程序的性能監(jiān)控

          共 10418字,需瀏覽 21分鐘

           ·

          2020-07-14 09:15

          介紹

          ??在linux上跑程序時(shí),常常遇到程序卡頓,內(nèi)存消耗大,這時(shí)候,我們需要使用free查看內(nèi)存使用情況及top內(nèi)存消耗排行,當(dāng)然,我們還可以繼續(xù)使用一些其他的JVM監(jiān)控命令如jstat等命令進(jìn)行java程序的性能評(píng)估。

          free命令

          free命令介紹

          ??free命令是顯示系統(tǒng)內(nèi)存的使用情況,一般包括了物理內(nèi)存、交換內(nèi)存以及內(nèi)核緩沖區(qū)內(nèi)存。

          free命令使用

          [linux@001]$ free --help

          Usage:
          free [options]

          Options:
          -b, --bytes show output in bytes
          -k, --kilo show output in kilobytes
          -m, --mega show output in megabytes
          -g, --giga show output in gigabytes
          --tera show output in terabytes
          -h, --human show human-readable output
          --si use powers of 1000 not 1024
          -l, --lohi show detailed low and high memory statistics
          -t, --total show total for RAM + swap
          -s N, --seconds N repeat printing every N seconds
          -c N, --count N repeat printing N times, then exit
          -w, --wide wide output

          --help display this help and exit
          -V, --version output version information and exit

          free命令示例

          [linux@001]$ free -b
          total used free shared buff/cache available
          Mem: 16657858560 9096298496 5962731520 42442752 1598828544 7198982144
          Swap: 0 0 0
          [linux@001]$ free -k
          total used free shared buff/cache available
          Mem: 16267440 8884816 5821264 41448 1561360 7028544
          Swap: 0 0 0
          [linux@001]$ free -m
          total used free shared buff/cache available
          Mem: 15886 8757 5612 40 1516 6782
          Swap: 0 0 0
          [linux@001]$ free -g
          total used free shared buff/cache available
          Mem: 15 8 5 0 1 6
          Swap: 0 0 0
          [linux@001]$ free -h
          total used free shared buff/cache available
          Mem: 15G 8.6G 5.5G 40M 1.5G 6.6G
          Swap: 0B 0B 0B
          [linux@001]$ free -l -h
          total used free shared buff/cache available
          Mem: 15G 8.5G 5.6G 40M 1.5G 6.7G
          Low: 15G 9G 5.6G
          High: 0B 0B 0B
          Swap: 0B 0B 0B
          [linux@001]$ free -t -h
          total used free shared buff/cache available
          Mem: 15G 8.5G 5.6G 40M 1.5G 6.7G
          Swap: 0B 0B 0B
          Total: 15G 8.5G 5.6G
          [linux@001]$ free -s 10
          total used free shared buff/cache available
          Mem: 16267440 8910116 5798884 41448 1558440 7003268
          Swap: 0 0 0

          total used free shared buff/cache available
          Mem: 16267440 8912052 5796892 41448 1558496 7001336
          Swap: 0 0 0

          free命令的參數(shù)說(shuō)明

          • Mem:內(nèi)存的使用情況。
          • Swap:交換空間的使用情況,是磁盤的一塊區(qū)域,可以是swap分區(qū)也可以是swap文件,當(dāng)系統(tǒng)物理內(nèi)存使用比較緊張時(shí),Linux會(huì)將內(nèi)存中不常被訪問(wèn)的數(shù)據(jù)保存到Swap中,若需要訪問(wèn)該內(nèi)容,會(huì)加載到內(nèi)存中,即換出和換入的意思。
          • total:物理內(nèi)存總數(shù)。
          • used:已使用內(nèi)存大小。
          • free:可使用內(nèi)存大小,即未被真正使用過(guò)的物理內(nèi)存大小。
          • shared:多個(gè)進(jìn)程可共享內(nèi)存大小。
          • buff/cache:磁盤緩存大小。
          • available:可提供的內(nèi)存大小,區(qū)別于free列,如果沒(méi)有足夠的free內(nèi)存可用,會(huì)從buffer/cache回收內(nèi)存來(lái)滿足應(yīng)用程序的需求,所以available=free+buffer+cache
          top命令

          top命令介紹

          ??top命令是用于監(jiān)控Linux系統(tǒng)狀況,可以通過(guò)該命令實(shí)時(shí)查看系統(tǒng)各個(gè)進(jìn)程的資源占用狀況。相當(dāng)于Windows系統(tǒng)的任務(wù)管理器。

          top命令使用

          [linux@001]$ top -help
          procps-ng version 3.3.10
          Usage:
          top -hv | -bcHiOSs -d secs -n max -u|U user -p pid(s) -o field -w [cols]

          使用格式:top [-] [d] [p] [q] [c] [b] [S] [s] [n]

          • d:指定兩次頻幕刷新頻率,即時(shí)間間隔。如top -d 4,指定更新周期為4秒。
          • p:指定監(jiān)控進(jìn)程id來(lái)監(jiān)控某個(gè)進(jìn)程狀態(tài)。如top -p 1201,顯示進(jìn)程號(hào)為1201的進(jìn)程信息、cpu、內(nèi)存占用等信息。
          • q:無(wú)延時(shí)刷新,若有超級(jí)用戶權(quán)限,則top會(huì)以高優(yōu)先級(jí)運(yùn)行。
          • c:顯示整個(gè)命令行。如top -c,顯示每個(gè)進(jìn)程的完整命令。
          • S:指定累計(jì)模式。如top -S,以累計(jì)模式顯示程序信息。
          • s:使top命令在安全模式中運(yùn)行。如top -s,不能交互使用s鍵位,會(huì)報(bào)unavailable in secure mode提示。
          • n:指定更新的次數(shù)。如top -n 2只更新2次,就退出top程序。
          • b:批處理模式顯示程序信息。如top -b,以批處理模式顯示程序信息。top界面程序交互常用按鍵說(shuō)明:
          1. c鍵位:切換顯示命令名稱和完整的命令行。
          2. P鍵位:根據(jù)CPU使用百分比大小進(jìn)行排序。
          3. M鍵位:根據(jù)內(nèi)存mem占有率大小進(jìn)行排序。
          4. n鍵位:設(shè)置在進(jìn)程列表中顯示進(jìn)程的數(shù)量。
          5. T鍵位:根據(jù)時(shí)間/累計(jì)時(shí)間進(jìn)行排序。
          6. s鍵位:改變畫面更新頻率,輸入兩次刷新之間的延遲時(shí)間,以秒為單位。
          7. S鍵位:切換到累計(jì)模式;開關(guān)。
          8. i鍵位:忽略閑置和僵尸進(jìn)程;開關(guān)。
          9. h鍵位:顯示幫助畫面。10.按u鍵位:輸入指定的user,就會(huì)顯示所有該user的程序進(jìn)程。
          10. q鍵位:退出top查看程序。
          11. f鍵位:編排視圖顯示字段。23b6878062f50f1e8a35f6b3ee9a8521.webp

          top命令示例

          top - 09:48:58 up 27 days, 23:42,  6 users,  load average: 0.10, 0.27, 0.37
          Tasks: 163 total, 1 running, 160 sleeping, 2 stopped, 0 zombie
          %Cpu(s): 0.6 us, 1.2 sy, 0.0 ni, 98.1 id, 0.1 wa, 0.0 hi, 0.0 si, 0.0 st
          KiB Mem : 16267440 total, 6819976 free, 8980392 used, 467072 buff/cache
          KiB Swap: 0 total, 0 free, 0 used. 6944052 avail Mem

          PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
          1926 root 20 0 1020672 3584 0 S 0.3 0.0 71:41.44 vmsec-linux
          2501 bdoc 20 0 8421520 569828 3876 S 0.3 3.5 26:53.14 java
          7899 root 20 0 148656 6044 4368 S 0.3 0.0 0:13.34 sshd
          12912 root 20 0 0 0 0 S 0.3 0.0 3:03.64 kworker/0:2
          31032 root 20 0 0 0 0 S 0.3 0.0 0:43.88 kworker/4:2
          1 root 20 0 191196 3132 1428 S 0.0 0.0 0:33.66 systemd
          2 root 20 0 0 0 0 S 0.0 0.0 0:00.38 kthreadd
          3 root 20 0 0 0 0 S 0.0 0.0 0:04.44 ksoftirqd/0
          5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
          7 root rt 0 0 0 0 S 0.0 0.0 0:43.99 migration/0

          若按下1數(shù)字鍵,進(jìn)行多核展示

          top - 10:35:29 up 28 days, 29 min,  6 users,  load average: 0.09, 0.15, 0.25
          Tasks: 159 total, 1 running, 156 sleeping, 2 stopped, 0 zombie
          %Cpu0 : 1.3 us, 1.3 sy, 0.0 ni, 97.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
          %Cpu1 : 0.7 us, 1.3 sy, 0.0 ni, 98.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
          %Cpu2 : 1.0 us, 1.0 sy, 0.0 ni, 98.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
          %Cpu3 : 0.7 us, 1.3 sy, 0.0 ni, 98.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
          %Cpu4 : 0.7 us, 0.7 sy, 0.0 ni, 98.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
          %Cpu5 : 0.7 us, 1.0 sy, 0.0 ni, 96.0 id, 2.3 wa, 0.0 hi, 0.0 si, 0.0 st
          %Cpu6 : 1.0 us, 1.7 sy, 0.0 ni, 97.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
          %Cpu7 : 1.0 us, 1.0 sy, 0.0 ni, 98.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
          KiB Mem : 16267440 total, 6706632 free, 8955152 used, 605656 buff/cache
          KiB Swap: 0 total, 0 free, 0 used. 6968016 avail Mem

          PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
          7899 root 20 0 148656 6048 4368 S 0.7 0.0 0:24.99 sshd
          1926 root 20 0 1020672 3584 0 S 0.3 0.0 71:46.39 vmsec-linux
          2726 mysql 20 0 4795092 849484 4984 S 0.3 5.2 1835:23 mysqld
          4363 bdoc 20 0 8445056 1.898g 14052 S 0.3 12.2 1:53.14 java
          8185 bdoc 20 0 10.126g 1.471g 12908 S 0.3 9.5 35:39.03 java
          8520 root 20 0 158684 3028 1568 S 0.3 0.0 0:14.86 top
          25718 bdoc 20 0 157708 2248 1568 R 0.3 0.0 0:00.17 top

          top命令的參數(shù)說(shuō)明

          統(tǒng)計(jì)信息區(qū)的前5行是系統(tǒng)整體的系統(tǒng)信息。

          1)第1行是任務(wù)隊(duì)列信息,這個(gè)類似于uptime命令的執(zhí)行結(jié)果:

          [linux@001]$ uptime
          09:50:06 up 27 days, 23:43, 6 users, load average: 1.13, 0.47, 0.43

          2)第2、3行是進(jìn)程和cpu的統(tǒng)計(jì)信息。tasks:進(jìn)程任務(wù)數(shù):

          • total:表示進(jìn)程總數(shù);
          • running:表示正在運(yùn)行的進(jìn)程數(shù);
          • sleeping:表示睡眠的進(jìn)程數(shù);
          • stopped:表示停止的進(jìn)程數(shù);
          • zombie:表示僵尸進(jìn)程數(shù);

          %Cpu(s):cpu使用情況,多核展示也可以通過(guò)按1數(shù)字,就會(huì)變成%Cpu0,%Cpu1,%Cpu2... ...

          • us:user,time running un-niced user processes 用戶空間占用cpu百分比;
          • sy:system,time running kernel processes 內(nèi)核空間占用cpu百分比;
          • ni:nice , time running niced user processes 用戶進(jìn)程空間內(nèi)改變過(guò)優(yōu)先級(jí)的進(jìn)程占用cpu百分比;
          • id:idle, time idel processes 空閑cpu百分比;
          • wa:IO-wait ,time waiting for I/O completion 等待輸入輸出cpu時(shí)間百分比;
          • hi:hardware interrupts,time spent servicing hardware interrupts 硬件cpu中斷占用百分比;
          • si:software interrupts,time spent servicing software interrupts 軟中斷占用百分比;
          • st:time stolen from this vm by the hypervisor虛擬機(jī)占用百分比;我們也可以通過(guò)cat /proc/cpuinfo查看cpu信息:e0201b7d1ee9c60d0d4ae1977d503996.webp

          3)第5、6行是內(nèi)存信息Mem:物理內(nèi)存

          • total:物理內(nèi)存總量。
          • free:物理空閑內(nèi)存量。
          • used:物理已使用內(nèi)存量。
          • buff/cache:物理內(nèi)核緩存內(nèi)存量。Swap:交換區(qū)
          • total:交換區(qū)總量。
          • free:交換區(qū)空閑量。
          • used:交換區(qū)使用量。
          • avail Mem:交換區(qū)可用量。

          4)第7行是各進(jìn)程的監(jiān)控

          • PID:進(jìn)程id。
          • USER:進(jìn)程所有者。
          • PR:進(jìn)程優(yōu)先級(jí)。
          • NI:nice值,負(fù)值表示高優(yōu)先級(jí),正值表示低優(yōu)先級(jí)。
          • VIRT:進(jìn)程使用的虛擬內(nèi)存總量,單位是kb,VIRT=SWAP+RES。
          • RES:進(jìn)程使用的、未被換出的物理內(nèi)存大小,單位kb,RES=CODE+DATA。
          • SHR:共享內(nèi)存大小,單位kb。
          • S:進(jìn)程狀態(tài)。D:不可終端的睡眠狀態(tài);R:運(yùn)行狀態(tài);S:睡眠狀態(tài);T:跟蹤/停止?fàn)顟B(tài);Z:僵尸進(jìn)程狀態(tài)。
          • %CPU:上次更新到現(xiàn)在的CPU時(shí)間占用百分比。
          • %MEM:進(jìn)程使用的物理內(nèi)存百分比。
          • TIME+:進(jìn)程使用的CPU時(shí)間總長(zhǎng),單位1/100秒。
          • COMMAND:進(jìn)程命令名稱/命令行。
          jstat命令

          jstat命令介紹

          ??首先得明確一下JVM中的堆內(nèi)存的概念,堆內(nèi)存=年輕代+年老代+永久代+元數(shù)據(jù),而年輕代=Eden區(qū)+兩個(gè)Survivor區(qū)(From及To)

          ??一般常用jstat -gc PID查看某個(gè)進(jìn)程的堆內(nèi)存使用情況。

          jstat命令使用

          使用語(yǔ)法格式jstat -參數(shù)解釋:

          • option 常見的都是使用-gcutil查看gc情況。
          • vmid :VM的進(jìn)程號(hào),即當(dāng)前運(yùn)行的java進(jìn)程號(hào)。
          • interval : 間隔時(shí)間,單位為秒或毫秒
          • count:打印次數(shù),如果缺省則打印無(wú)數(shù)次。

          用的比較多的是:jstat -gc PIDjstat -gcutil PIDOption Displays...

          • -class: Statistics on the behavior of the class loader.
          • -compiler: Statistics of the behavior of the HotSpot Just-in-Time compiler.
          • -gc: Statistics of the behavior of the garbage collected heap.
          • -gccapacity: Statistics of the capacities of the generations and their corresponding spaces.
          • -gccause: Summary of garbage collection statistics (same as -gcutil), with the cause of the last and current (if applicable) garbage collection events.
          • -gcnew: Statistics of the behavior of the new generation.
          • -gcnewcapacity: Statistics of the sizes of the new generations and its corresponding spaces.
          • -gcold: Statistics of the behavior of the old and permanent generations.
          • -gcoldcapacity: Statistics of the sizes of the old generation.
          • -gcpermcapacity: Statistics of the sizes of the permanent generation.
          • -gcutil: Summary of garbage collection statistics. printcompilation HotSpot compilation method statistics.

          jstat命令示例

          4b64c8fe02d9b12ad0d5b769183c132d.webpd891e6741f2d1242942cda96608236b8.webp

          jstat -gc常用的參數(shù)說(shuō)明

          Column Description

          • S0C: Current survivor space 0 capacity (KB).
          • S1C: Current survivor space 1 capacity (KB).
          • S0U: Survivor space 0 utilization (KB).
          • S1U: Survivor space 1 utilization (KB).
          • EC: Current eden space capacity (KB).
          • EU: Eden space utilization (KB).
          • OC: Current old space capacity (KB).
          • OU: Old space utilization (KB).
          • PC: Current permanent space capacity (KB).
          • PU: Permanent space utilization (KB).
          • YGC: Number of young generation GC Events.
          • YGCT: Young generation garbage collection time.
          • FGC: Number of full GC events.常用
          • FGCT: Full garbage collection time. 常用
          • GCT: Total garbage collection time.常用
          jmap命令

          jmap命令介紹

          ??jmap命令可以用于對(duì)堆進(jìn)行立項(xiàng)分析,檢查對(duì)象創(chuàng)建、數(shù)量及對(duì)象所占內(nèi)存大小。一般使用jmap -heap PID用于打印堆使用情況。

          jmap命令使用

          jmap [ option ] pid:連接進(jìn)程號(hào)jmap [ option ] executable core:連接核心文件jmap [ option ] [server-id@]remote-hostname-or-IP:連接遠(yuǎn)程debug服務(wù)的主機(jī)名或ip

          [linux@001]$ jmap -h
          Usage:
          jmap [option]
          (to connect to running process)
          jmap [option]
          (to connect to a core file)
          jmap [option] [server_id@]
          (to connect to remote debug server)

          where

          jmap命令示例

          [linux@001]$ netstat -npl | grep 8889
          (Not all processes could be identified, non-owned process info
          will not be shown, you would have to be root to see it all.)
          tcp6 0 0 :::8889 :::* LISTEN 7253/java
          [linux@001]$ jmap -heap 7253
          Attaching to process ID 7253, please wait...
          Debugger attached successfully.
          Server compiler detected.
          JVM version is 25.65-b01

          using thread-local object allocation.
          Parallel GC with 8 thread(s)

          Heap Configuration: #堆配置,即JVM參數(shù)配置
          MinHeapFreeRatio = 0 #最小堆使用比例
          MaxHeapFreeRatio = 100 #最大堆使用比例
          MaxHeapSize = 4164943872 (3972.0MB) #最大堆空間大小
          NewSize = 87031808 (83.0MB) #新生代分配大小
          MaxNewSize = 1388314624 (1324.0MB) #最大新生代可分配大小
          OldSize = 175112192 (167.0MB) #老年代大小
          NewRatio = 2 #新生代比例
          SurvivorRatio = 8 #新生代和survivor比例
          MetaspaceSize = 21807104 (20.796875MB) #元數(shù)據(jù)空間大小
          CompressedClassSpaceSize = 1073741824 (1024.0MB) #指針空間大小
          MaxMetaspaceSize = 17592186044415 MB #MaxMetaspaceSize=Compressed Class Space Size + Metaspace area (excluding the Compressed Class Space) Size
          G1HeapRegionSize = 0 (0.0MB) #G1中的region大小

          Heap Usage:
          PS Young Generation
          Eden Space: #eden區(qū)
          capacity = 1342177280 (1280.0MB)
          used = 970477176 (925.519157409668MB)
          free = 371700104 (354.48084259033203MB)
          72.30618417263031% used
          From Space: #from區(qū),即survivor1區(qū)
          capacity = 23068672 (22.0MB)
          used = 22904896 (21.84381103515625MB)
          free = 163776 (0.15618896484375MB)
          99.29005015980114% used
          To Space: #to區(qū),即survivor2區(qū)
          capacity = 23068672 (22.0MB)
          used = 0 (0.0MB)
          free = 23068672 (22.0MB)
          0.0% used
          PS Old Generation #老年代使用情況
          capacity = 176685056 (168.5MB)
          used = 52151696 (49.73573303222656MB)
          free = 124533360 (118.76426696777344MB)
          29.5167555087398% used

          31742 interned Strings occupying 3385416 bytes.
          其他

          ps監(jiān)控java線程數(shù)

          ps -eLf | grep java | wc -l

          ps監(jiān)控進(jìn)程詳情

          ps -ef | grep PID9151cae4e10b8f2017eccc77b7ce51e9.webp

          netstat監(jiān)控tcp連接數(shù)

          netstat -n | grep tcp | grep 程序端口號(hào)| wc -l

          pmap分析線程堆棧

          pmap PID229ebabc4553adc2f366c47d314d8cf1.webp

          top查看進(jìn)程線程

          top -Hp PID2755372bfb3d1853ccc0df9be4677b59.webp

          參考

          jstat官方命令說(shuō)明

          jmap官方命令說(shuō)明


          覺(jué)得文章直擊靈魂,歡迎點(diǎn)贊、在看轉(zhuǎn)發(fā)

          瀏覽 38
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  伊人69 | 多人p| 日产无码久久久久久久久精英 | 日本熟妇HD | 久久久久久电影成人电影 |