哪个Java线程在占用CPU? [英] Which Java thread is hogging the CPU?

查看:94
本文介绍了哪个Java线程在占用CPU?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您的Java程序占用了100%的CPU.它有50个线程.您需要查找哪个线程有罪.我没有找到可以提供帮助的工具.当前,我使用以下非常耗时的例程:

Let's say your Java program is taking 100% CPU. It has 50 threads. You need to find which thread is guilty. I have not found a tool that can help. Currently I use the following very time consuming routine:

  1. 运行jstack <pid>,其中pid是Java进程的进程ID.找到它的简单方法是运行JDK中包含的另一个实用程序-jps.最好将jstack的输出重定向到文件.
  2. 搜索可运行"线程.跳过那些在套接字上等待的对象(由于某些原因,它们仍被标记为可运行).
  3. 重复步骤1和2两次,看看是否可以找到图案.
  1. Run jstack <pid>, where pid is the process id of a Java process. The easy way to find it is to run another utility included in the JDK - jps. It is better to redirect jstack's output to a file.
  2. Search for "runnable" threads. Skip those that wait on a socket (for some reason they are still marked runnable).
  3. Repeat steps 1 and 2 a couple of times and see if you can locate a pattern.

或者,您可以在Eclipse中附加到Java进程并尝试逐个挂起线程,直到遇到占用CPU的线程.在单CPU机器上,您可能需要首先降低Java进程的优先级才能移动.即便如此,由于超时,Eclipse通常仍无法连接到正在运行的进程.

Alternatively, you could attach to a Java process in Eclipse and try to suspend threads one by one, until you hit the one that hogs CPU. On a one-CPU machine, you might need to first reduce the Java process's priority to be able to move around. Even then, Eclipse often isn't able to attach to a running process due to a timeout.

我希望Sun的visualvm工具能够做到这一点.

I would have expected Sun's visualvm tool to do this.

有人知道更好的方法吗?

Does anybody know of a better way?

推荐答案

尝试查看

Try looking at the Hot Thread Detector plugin for visual VM -- it uses the ThreadMXBean API to take multiple CPU consumption samples to find the most active threads. It's based on a command-line equivalent from Bruce Chapman which might also be useful.

这篇关于哪个Java线程在占用CPU?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆