关注微信公众号:CodingTechWork,一起学习进步。

介绍

  在linux上跑程序时,常常遇到程序卡顿,内存消耗大,这时候,我们需要使用free查看内存使用情况及top内存消耗排行,当然,我们还可以继续使用一些其他的JVM监控命令如jstat等命令进行java程序的性能评估。

free命令

free命令介绍

  free命令是显示系统内存的使用情况,一般包括了物理内存、交换内存以及内核缓冲区内存。

free命令使用

[linux@001]$ free --helpUsage: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 -btotal        used        free      shared  buff/cache   available
Mem:    16657858560  9096298496  5962731520    42442752  1598828544  7198982144
Swap:             0           0           0
[linux@001]$ free -ktotal        used        free      shared  buff/cache   available
Mem:       16267440     8884816     5821264       41448     1561360     7028544
Swap:             0           0           0
[linux@001]$ free -mtotal        used        free      shared  buff/cache   available
Mem:          15886        8757        5612          40        1516        6782
Swap:             0           0           0
[linux@001]$ free -gtotal        used        free      shared  buff/cache   available
Mem:             15           8           5           0           1           6
Swap:             0           0           0
[linux@001]$ free -htotal        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 -htotal        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 -htotal        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 10total        used        free      shared  buff/cache   available
Mem:       16267440     8910116     5798884       41448     1558440     7003268
Swap:             0           0           0total        used        free      shared  buff/cache   available
Mem:       16267440     8912052     5796892       41448     1558496     7001336
Swap:             0           0           0

free命令的参数说明

  • Mem:内存的使用情况。
  • Swap:交换空间的使用情况,是磁盘的一块区域,可以是swap分区也可以是swap文件,当系统物理内存使用比较紧张时,Linux会将内存中不常被访问的数据保存到Swap中,若需要访问该内容,会加载到内存中,即换出和换入的意思。
  • total:物理内存总数。
  • used:已使用内存大小。
  • free:可使用内存大小,即未被真正使用过的物理内存大小。
  • shared:多个进程可共享内存大小。
  • buff/cache:磁盘缓存大小。
  • available:可提供的内存大小,区别于free列,如果没有足够的free内存可用,会从buffer/cache回收内存来满足应用程序的需求,所以available=free+buffer+cache

top命令

top命令介绍

  top命令是用于监控Linux系统状况,可以通过该命令实时查看系统各个进程的资源占用状况。相当于Windows系统的任务管理器。

top命令使用

[linux@001]$ top -helpprocps-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:指定两次频幕刷新频率,即时间间隔。如top -d 4,指定更新周期为4秒。
  • p:指定监控进程id来监控某个进程状态。如top -p 1201,显示进程号为1201的进程信息、cpu、内存占用等信息。
  • q:无延时刷新,若有超级用户权限,则top会以高优先级运行。
  • c:显示整个命令行。如top -c,显示每个进程的完整命令。
  • S:指定累计模式。如top -S,以累计模式显示程序信息。
  • s:使top命令在安全模式中运行。如top -s,不能交互使用s键位,会报unavailable in secure mode提示。
  • n:指定更新的次数。如top -n 2只更新2次,就退出top程序。
  • b:批处理模式显示程序信息。如top -b,以批处理模式显示程序信息。
    top界面程序交互常用按键说明:
  1. c键位:切换显示命令名称和完整的命令行。
  2. P键位:根据CPU使用百分比大小进行排序。
  3. M键位:根据内存mem占有率大小进行排序。
  4. n键位:设置在进程列表中显示进程的数量。
  5. T键位:根据时间/累计时间进行排序。
  6. s键位:改变画面更新频率,输入两次刷新之间的延迟时间,以秒为单位。
  7. S键位:切换到累计模式;开关。
  8. i键位:忽略闲置和僵尸进程;开关。
  9. h键位:显示帮助画面。
    10.按u键位:输入指定的user,就会显示所有该user的程序进程。
  10. q键位:退出top查看程序。
  11. f键位:编排视图显示字段。

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数字键,进行多核展示

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命令的参数说明

统计信息区的前5行是系统整体的系统信息。
1)第1行是任务队列信息,这个类似于uptime命令的执行结果:

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

2)第2、3行是进程和cpu的统计信息。
tasks:进程任务数:

  • total:表示进程总数;
  • running:表示正在运行的进程数;
  • sleeping:表示睡眠的进程数;
  • stopped:表示停止的进程数;
  • zombie:表示僵尸进程数;

%Cpu(s):cpu使用情况,多核展示也可以通过按1数字,就会变成%Cpu0%Cpu1%Cpu2… …

  • us:user,time running un-niced user processes 用户空间占用cpu百分比;
  • sy:system,time running kernel processes 内核空间占用cpu百分比;
  • ni: nice , time running niced user processes 用户进程空间内改变过优先级的进程占用cpu百分比;
  • id:idle, time idel processes 空闲cpu百分比;
  • wa:IO-wait ,time waiting for I/O completion 等待输入输出cpu时间百分比;
  • 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虚拟机占用百分比;
    我们也可以通过cat /proc/cpuinfo查看cpu信息:

    3)第5、6行是内存信息
    Mem:物理内存
  • total:物理内存总量。
  • free:物理空闲内存量。
  • used:物理已使用内存量。
  • buff/cache:物理内核缓存内存量。
    Swap:交换区
  • total:交换区总量。
  • free:交换区空闲量。
  • used:交换区使用量。
  • avail Mem:交换区可用量。

4)第7行是各进程的监控

  • 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:进程命令名称/命令行。

jstat命令

jstat命令介绍

  首先得明确一下JVM中的堆内存的概念,堆内存=年轻代+年老代+永久代+元数据,而年轻代=Eden区+两个Survivor区(From及To)
  一般常用jstat -gc PID查看某个进程的堆内存使用情况。

jstat命令使用

使用语法格式
jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]
参数解释:

  • option 常见的都是使用-gcutil查看gc情况。
  • vmid : VM的进程号,即当前运行的java进程号。
  • interval : 间隔时间,单位为秒或毫秒
  • count:打印次数,如果缺省则打印无数次。

用的比较多的是:jstat -gc PIDjstat -gcutil PID
Option 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命令示例


jstat -gc常用的参数说明

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命令可以用于对堆进行立项分析,检查对象创建、数量及对象所占内存大小。一般使用jmap -heap PID用于打印堆使用情况。

jmap命令使用

jmap [ option ] pid:连接进程号
jmap [ option ] executable core:连接核心文件
jmap [ option ] [server-id@]remote-hostname-or-IP:连接远程debug服务的主机名或ip

[linux@001]$ jmap -h
Usage:jmap [option] <pid>(to connect to running process)jmap [option] <executable <core>(to connect to a core file)jmap [option] [server_id@]<remote server IP or hostname>(to connect to remote debug server)where <option> is one of:<none>               to print same info as Solaris pmap-heap                to print java heap summary-histo[:live]        to print histogram of java object heap; if the "live"suboption is specified, only count live objects-clstats             to print class loader statistics-finalizerinfo       to print information on objects awaiting finalization-dump:<dump-options> to dump java heap in hprof binary formatdump-options:live         dump only live objects; if not specified,all objects in the heap are dumped.format=b     binary formatfile=<file>  dump heap to <file>Example: jmap -dump:live,format=b,file=heap.bin <pid>-F                   force. Use with -dump:<dump-options> <pid> or -histoto force a heap dump or histogram when <pid> does notrespond. The "live" suboption is not supportedin this mode.-h | -help           to print this help message-J<flag>             to pass <flag> directly to the runtime system

jmap命令示例

[linux@001]$ netstat -npl | grep 8889
(Not all processes could be identified, non-owned process infowill 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-b01using thread-local object allocation.
Parallel GC with 8 thread(s)Heap Configuration: #堆配置,即JVM参数配置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) #元数据空间大小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区capacity = 1342177280 (1280.0MB)used     = 970477176 (925.519157409668MB)free     = 371700104 (354.48084259033203MB)72.30618417263031% used
From Space: #from区,即survivor1区capacity = 23068672 (22.0MB)used     = 22904896 (21.84381103515625MB)free     = 163776 (0.15618896484375MB)99.29005015980114% used
To Space: #to区,即survivor2区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% used31742 interned Strings occupying 3385416 bytes.

其他

ps监控java线程数

ps -eLf | grep java | wc -l

ps监控进程详情

ps -ef | grep PID

netstat监控网络tcp客户连接数

netstat -n | grep tcp | grep 程序端口号| wc -l

pmap分析线程堆栈

pmap PID

top查看进程线程

top -Hp PID

参考
jstat官方命令说明
jmap官方命令说明

Linux—JVM等性能调优监控相关推荐

  1. 2020年薪30W的Java程序员都要求熟悉JVM与性能调优!

    前言 作为Java程序员,你有没有被JVM伤害过?面试的时候是否碰到过对JVM的灵魂拷问? 一.JVM 内存区域划分 1.程序计数器(线程私有) 程序计数器(Program Counter Regis ...

  2. Tomcat 和 JVM 的性能调优总结

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试资料 来源:http://rrd.me/enKbC Tomcat性能调优 ...

  3. JVM解读-性能调优实例

    2019独角兽企业重金招聘Python工程师标准>>> JVM性能调优 1 堆设置调优 年轻代大小选择 响应时间优先的应用:尽可能设大,直到接近系统的最低响应时间限制(根据实际情况选 ...

  4. jvm maxgcpausemillis 默认值_Tomcat和JVM的性能调优总结

    Tomcat性能调优: 找到Tomcat根目录下的conf目录,修改server.xml文件的内容.对于这部分的调优,我所了解到的就是无非设置一下Tomcat服务器的最大并发数和Tomcat初始化时创 ...

  5. 第5课:实战演示jvm三大性能调优参数:-xms -xmx -xss

    内容: 1.实战演示-Xss性能调优参数 2.实战演示-Xms.-Xmx性能调优参数 一.实战演示-Xss性能调优参数 Java每个线程的Stack大小.JDK5.0以后每个线程堆栈大小为1M,以前每 ...

  6. 基于Linux的WebSphere性能调优与故障诊断

    一.关于was数据源等问题的配置 (1)关于was数据源连接池的最大.最小配置多大合适?怎样去计算? (2)关于JVM的配置,64位系统,64位WAS,最值小和最大配置多大最优?怎样去计算? (3)应 ...

  7. zz温故知新:Tomcat调优JVM内存性能调优

    Java性能优化原则:代码运算性能.内存回收.应用配置(影响Java程序主要原因是垃圾回收,下面会重点介绍这方面) 代码层优化:避免过多循环嵌套.调用和复杂逻辑. Tomcat调优主要内容如下: 1. ...

  8. 【JVM】JVM参数性能调优 -Xms -Xmx -Xmn -XX:NewRatio -XX:SurvivorRatio

    原文链接: http://unixboy.iteye.com/blog/174173/ 上图来自:https://blog.csdn.net/jakeswang/article/details/105 ...

  9. 【JVM与性能调优】与Java性能调优相关的JDK命令行工具大盘点

    一. jps 命令 1.1 jps介绍 jps(JVM Process Status Tool) 列出Java进程,显示指定系统内所有的HotSpot虚拟机进程. 查询Linux系统当前所有java进 ...

最新文章

  1. mysql 第一天_MySQL第一天
  2. Java 求分段函数g(x)的值
  3. Wireshark 跟踪TCP流
  4. pl0源码(可在delphi7中运行)
  5. 怎样才能找出哪个 CPU 内核正在运行该进程?(一)
  6. laravel 淘宝 NPM 镜像
  7. 实验4-2-2 求e的近似值 (15 分)
  8. 安装新硬盘-再次冲击Ubuntu之server篇
  9. Vue的单页应用中如何引用单独的样式文件
  10. linux上openmp测试
  11. php 时间转换yyyymmdd,YYYYMMDD字符串日期转换为日期时间值
  12. android 蓝牙耳机 找不到驱动程序,bluetooth外围设备找不到驱动程序现象的解决方法介绍...
  13. CS:APP第六章知识总结(内存、缓存、locality)
  14. 如何快速定位出一个IP地址的归属地?——二分查找变体
  15. 隔离式DC/DC高压模块5V12V24V转50V110V250V300V380V600V1100V短路保护直流升压可调开关控制电源模块
  16. Android大赛首轮获奖作品解析
  17. 2020,那些惊艳我的产品迭代
  18. getter方法的作用 vuex_vuex Getter
  19. 【JVM】JVM之执行引擎
  20. 深入理解OpenStack Neutron之---3 Neutron的资源模型---读书笔记

热门文章

  1. OpenCV辅助对象(help objects)(4)_Exception
  2. HTML5新特性基础学习笔记上
  3. 关于java中next,nextInt,nextLine的区别
  4. ubuntu中遇到jupyter出现问题ValueError: signal only works in main thread ERROR:tornado.application:Except
  5. jsp数据 foreach 循环遍历 (含效果图)- 代码篇
  6. linux 关闭自动升级,开启关闭Centos的自动更新(转)
  7. java游戏西门大官人_valueOf()方法的使用
  8. pads铺铜不能开启drp_PADS中常见问题解决方案
  9. 增删改查java代码_程序员:听说你正在为天天写增删改查代码而烦恼
  10. easyui-super-theme后台模板