linux里查看最耗CPU的线程

1、top后按c查看最耗cpu的进程,得到pid

2、top -Hp pid 查看该进程里的线程资源使用情况,找到最耗资源的线程的pid

3、jstack pid来查看进程的各个线程栈,注意这里的pid是第一步中进程的pid,不是第二步得到的线程id

4、将第二步得到的pid转成16进制之后在线程栈信息里查找nid等于pid16进制的,就找到最耗资源的线程的栈信息

 

话不多说了,先来看代码吧

public class Test{public static void main(String args[]){for(int i=0;i<10;i++){new Thread(){public void run(){try{Thread.sleep(100000);}catch(Exception e){}}}.start();}Thread t=new Thread(){public void run(){int i=0;while(true){i=(i++)/100;}}};t.setName("Busiest Thread");t.start();}
}

这个例子里新创建了11个线程,其中10个线程没干什么事,主要是sleep,另外有一个线程在循环里一直跑着,可以想象这个线程是这个进程里最耗cpu的线程了,那怎么把这个线程给抓出来呢?

首先我们可以通过top -Hp <pid>来看这个进程里所有线程的cpu消耗情况,得到类似下面的数据

$ top -Hp 18207
top - 19:11:43 up 573 days,  2:43,  2 users,  load average: 3.03, 3.03, 3.02
Tasks:  44 total,   1 running,  43 sleeping,   0 stopped,   0 zombie
Cpu(s): 18.8%us,  0.0%sy,  0.0%ni, 81.1%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  99191752k total, 98683576k used,   508176k free,   128248k buffers
Swap:  1999864k total,   191064k used,  1808800k free, 17413760k cachedPID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
18250 admin     20   0 26.1g  28m  10m R 99.9  0.0   0:19.50 java Test
18207 admin     20   0 26.1g  28m  10m S  0.0  0.0   0:00.00 java Test
18208 admin     20   0 26.1g  28m  10m S  0.0  0.0   0:00.09 java Test
18209 admin     20   0 26.1g  28m  10m S  0.0  0.0   0:00.00 java Test
18210 admin     20   0 26.1g  28m  10m S  0.0  0.0   0:00.00 java Test
18211 admin     20   0 26.1g  28m  10m S  0.0  0.0   0:00.00 java Test
18212 admin     20   0 26.1g  28m  10m S  0.0  0.0   0:00.00 java Test
18213 admin     20   0 26.1g  28m  10m S  0.0  0.0   0:00.00 java Test
18214 admin     20   0 26.1g  28m  10m S  0.0  0.0   0:00.00 java Test
18215 admin     20   0 26.1g  28m  10m S  0.0  0.0   0:00.00 java Test
18216 admin     20   0 26.1g  28m  10m S  0.0  0.0   0:00.00 java Test
18217 admin     20   0 26.1g  28m  10m S  0.0  0.0   0:00.00 java Test
18218 admin     20   0 26.1g  28m  10m S  0.0  0.0   0:00.00 java Test
18219 admin     20   0 26.1g  28m  10m S  0.0  0.0   0:00.00 java Test
18220 admin     20   0 26.1g  28m  10m S  0.0  0.0   0:00.00 java Test
18221 admin     20   0 26.1g  28m  10m S  0.0  0.0   0:00.00 java Test
18222 admin     20   0 26.1g  28m  10m S  0.0  0.0   0:00.00 java Test
18223 admin     20   0 26.1g  28m  10m S  0.0  0.0   0:00.00 java Test
18224 admin     20   0 26.1g  28m  10m S  0.0  0.0   0:00.00 java Test
18225 admin     20   0 26.1g  28m  10m S  0.0  0.0   0:00.00 java Test
18226 admin     20   0 26.1g  28m  10m S  0.0  0.0   0:00.00 java Test
18227 admin     20   0 26.1g  28m  10m S  0.0  0.0   0:00.00 java Test

拿到这个结果之后,我们可以看到cpu最高的线程是pid为18250的线程,占了99.8%:

 PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
18250 admin     20   0 26.1g  28m  10m R 99.9  0.0   0:19.50 java Test

接着我们可以通过jstack <pid>的输出来看各个线程栈:

$ jstack 18207
2016-03-30 19:12:23
Full thread dump OpenJDK 64-Bit Server VM (25.66-b60 mixed mode):"Attach Listener" #30 daemon prio=9 os_prio=0 tid=0x00007fb90be13000 nid=0x47d7 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE"DestroyJavaVM" #29 prio=5 os_prio=0 tid=0x00007fb96245b800 nid=0x4720 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE"Busiest Thread" #28 prio=5 os_prio=0 tid=0x00007fb91498d000 nid=0x474a runnable [0x00007fb9065fe000]
   java.lang.Thread.State: RUNNABLEat Test$2.run(Test.java:18)"Thread-9" #27 prio=5 os_prio=0 tid=0x00007fb91498c800 nid=0x4749 waiting on condition [0x00007fb906bfe000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)at java.lang.Thread.sleep(Native Method)at Test$1.run(Test.java:9)"Thread-8" #26 prio=5 os_prio=0 tid=0x00007fb91498b800 nid=0x4748 waiting on condition [0x00007fb906ffe000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)at java.lang.Thread.sleep(Native Method)at Test$1.run(Test.java:9)"Thread-7" #25 prio=5 os_prio=0 tid=0x00007fb91498b000 nid=0x4747 waiting on condition [0x00007fb9073fe000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)at java.lang.Thread.sleep(Native Method)at Test$1.run(Test.java:9)"Thread-6" #24 prio=5 os_prio=0 tid=0x00007fb91498a000 nid=0x4746 waiting on condition [0x00007fb9077fe000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)at java.lang.Thread.sleep(Native Method)at Test$1.run(Test.java:9)
...

上面的线程栈我们注意到nid的值其实就是线程ID,它是十六进制的,我们将消耗cpu最高的线程18250,转成十六进制0X47A,然后从上面的线程栈里找到nid=0X47A的线程,其栈为:

"Busiest Thread" #28 prio=5 os_prio=0 tid=0x00007fb91498d000 nid=0x474a runnable [0x00007fb9065fe000]
   java.lang.Thread.State: RUNNABLEat Test$2.run(Test.java:18)

即将最耗cpu的线程找出来了,是Businest Thread

转载于:https://www.cnblogs.com/yifanSJ/p/9764965.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/279220.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vlc传输_如何使用VLC通过网络流式传输视频和音乐

vlc传输VLC includes a fairly easy-to-use streaming feature that can stream music and videos over a local network or the Internet. You can tune into the stream using VLC or other media players. VLC包括一个相当易于使用的流媒体功能&#xff0c;可以通过本地网络…

python实现异步的几种方式_终于搞明白了,异步Python比同步Python究竟快在哪里?...

大家好&#xff0c;你是否听人们说过&#xff0c;异步 Python 代码比“普通(或同步)Python 代码更快&#xff1f; 果真是那样吗&#xff1f;同步和异步是什么意思&#xff1f;Web 应用程序通常要处理许多请求&#xff0c;这些请求在短时间内来自不同的客户端。为避免处理延迟&a…

您可能没有注意到的7个Ubuntu File Manager功能

The Nautilus file manager included with Ubuntu includes some useful features you may not notice unless you go looking for them. You can create saved searches, mount remote file systems, use tabs in your file manager, and more. Ubuntu随附的Nautilus文件管理器…

P3174 [HAOI2009]毛毛虫(树形dp)

P3174 [HAOI2009]毛毛虫 题目描述 对于一棵树&#xff0c;我们可以将某条链和与该链相连的边抽出来&#xff0c;看上去就象成一个毛毛虫&#xff0c;点数越多&#xff0c;毛毛虫就越大。例如下图左边的树&#xff08;图 1 &#xff09;抽出一部分就变成了右边的一个毛毛虫了&am…

wdcp mysql密码_WDCP提示无法连接mysql及创建站点提示mysql密码不正确

一、wdcp系统访问提示无法连接mysql1、可能是mysql服务没启动&#xff0c;首先ssh登陆服务器&#xff0c;然后执行service mysqld restart重启mysql再访问试下&#xff0c;如果无法启动&#xff0c;先用df -lh查看下home分区有没有挂载&#xff0c;如果没有挂载尝试先重启&…

applecare多少钱?_否,AppleCare +无法覆盖丢失或被盗的iPhone

applecare多少钱?Losing your iPhone or getting it stolen is pretty common these days, but it’s important to know that while AppleCare covers accidental damage, it doesn’t cover a lost or stolen iPhone. 如今&#xff0c;丢失iPhone或使其被盗很普遍&#xff0…

10以内数的组成分解图_大班数学教案《10以内数的组成》

大班数学教案《10以内数的组成》作为一名教学工作者&#xff0c;时常需要编写教案&#xff0c;借助教案可以让教学工作更科学化。那么什么样的教案才是好的呢&#xff1f;以下是小编收集整理的大班数学教案《10以内数的组成》&#xff0c;希望能够帮助到大家。大班数学教案《10…

HDFS文件目录操作代码

分布式文件系统HDFS中对文件/目录的相关操作代码&#xff0c;整理了一下&#xff0c;大概包括以下部分&#xff1a; 文件夹的新建、删除、重命名文件夹中子文件和目录的统计文件的新建及显示文件内容文件在local和remote间的相互复制定位文件在HDFS中的位置&#xff0c;以及副本…

craigslist_如何设置Craigslist警报(用于电子邮件或SMS)

craigslistWhether you’re looking for apartments or used gadgets on Craigslist, you don’t have to keep checking the website. You can stay on top of things by getting notified when new posts go up that match your searches. 无论您是在Craigslist上寻找公寓还是…

merge r语言daframe_R语言总结

R语言总结数据框操作(plyr包)辅助小函数1 splat函数&#xff1a;作用&#xff1a;把原函数中多个参数打包为一个list作为参数&#xff0c;然后输出新的函数。也就是说本来某个函数需要输入多个参数&#xff0c;现在套上splat后&#xff0c;只要输入一个参数list就可以了&#x…

Django模板语言中的自定义方法filter过滤器实现web网页的瀑布流

模板语言自定义方法介绍 自定义方法注意事项 Django中有simple_tag 和 filter 两种自定义方法,之前也提到过,需要注意的是 扩展目录名称必须是templatetagstemplatetags中的自定义标签和过滤器必须依赖于一个django app&#xff0c;也就是说&#xff0c;自定义标签和过滤器是绑…

dsp怪胎_2012年6月最佳怪胎文章

dsp怪胎This past month we covered topics such as why you only have to wipe a disk once to erase it, what RSS is and how you can benefit from using it, how websites are tracking you online, and more. Join us as we look back at the best articles for June. 在…

mysql 回退查询_MYSQL数据库表排序规则不一致导致联表查询,索引不起作用问题...

Mysql数据库表排序规则不一致导致联表查询&#xff0c;索引不起作用问题表更描述: 将mysql数据库中的worktask表添加ishaspic字段。具体操作&#xff1a;(1)数据库worktask表新添是否有图片字段ishaspic&#xff1b;新添字段时&#xff0c;报错[SQL] alter table WorkTask add …

如何在Ubuntu上查看和写入系统日志文件

Linux logs a large amount of events to the disk, where they’re mostly stored in the /var/log directory in plain text. Most log entries go through the system logging daemon, syslogd, and are written to the system log. Linux将大量事件记录到磁盘上&#xff0c…

[转]table中设置tr行间距

原文地址:https://blog.csdn.net/itmyhome1990/article/details/50475616 CSS border-collapse 属性设置表格的边框是否被合并为一个单一的边框 值描述separate默认值。边框会被分开。不会忽略 border-spacing 和 empty-cells 属性。collapse如果可能&#xff0c;边框会合并为一…

向Ubuntu提供反馈的5种方法

Ubuntu, like many other Linux distributions, is a community-developed operating system. In addition to getting involved and submitting patches, there are a variety of ways you can provide useful feedback and suggest features to Ubuntu. 与许多其他Linux发行版…

Tomcat 发布项目 conf/Catalina/localhost 配置 及数据源配置

本文介绍通过在tomcat的conf/Catalina/localhost目录下添加配置文件&#xff0c;来发布项目。因为这样对 tomcat 的入侵性最小&#xff0c;只需要新增一个配置文件&#xff0c;不需要修改原有配置&#xff1b;而且支持动态解析&#xff0c;修改完代码直接生效(修改配置除外)。在…

Centos7 中文乱码

1. 安装中文库 yum groupinstall "fonts" 2. 检查是否有中文语言包 locale -a 3. 查看当前系统语言环境 locale 解析如下 LANG:当前系统的语言LC_CTYPE&#xff1a;语言符号及其分类LC_NUMERIC&#xff1a;数字LC_COLLATE&#xff1a;比较和排序习惯LC_TIME&#xff…

pkpm板按弹性计算还是塑性_双向板按弹性方法还是按塑性方法计算

双向板按弹性方法还是按塑性方法计算茅老师您好&#xff01;想请教您个问题&#xff0c;PKPM计算双向板时一般都是按弹性算吧&#xff0c;可我去年刚进设计院的时候有一个项目是按塑性算的&#xff0c;这样影响大不大啊&#xff0c;支座与跨中弯矩比值系数取得默认的1.8&#x…

chrome自动退出的原因_Chrome 70将让用户选择退出新的自动登录功能

chrome自动退出的原因An upcoming Chrome option allows users to log into Google accounts without logging into the browser. The change was prompted by a backlash among users and privacy advocates. 即将推出的Chrome选项允许用户无需登录浏览器即可登录Google帐户。…