博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java CPU 100% 排查
阅读量:7283 次
发布时间:2019-06-30

本文共 2108 字,大约阅读时间需要 7 分钟。

一个应用占用CPU很高,除了确实是计算密集型应用之外,通常原因都是出现了死循环。

(友情提示:本博文章欢迎转载,但请注明出处:hankchen,

以我们最近出现的一个实际故障为例,介绍怎么定位和解决这类问题。

根据top命令,发现PID为28555的Java进程占用CPU高达200%,出现故障。

通过ps aux | grep PID命令,可以进一步确定是tomcat进程出现了问题。但是,怎么定位到具体线程或者代码呢?

首先显示线程列表:

ps -mp pid -o THREAD,tid,time

找到了耗时最高的线程28802,占用CPU时间快两个小时了!

其次将需要的线程ID转换为16进制格式:

printf "%x\n" tid

最后打印线程的堆栈信息:

jstack pid |grep tid -A 30

找到出现问题的代码了!

现在来分析下具体的代码:ShortSocketIO.readBytes(ShortSocketIO.java:106)

ShortSocketIO是应用封装的一个用短连接Socket通信的工具类。readBytes函数的代码如下:

public byte[] readBytes(int length) throws IOException {

    if ((this.socket == null) || (!this.socket.isConnected())) {

        throw new IOException("++++ attempting to read from closed socket");

    }

    byte[] result = null;

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    if (this.recIndex >= length) {

           bos.write(this.recBuf, 0, length);

           byte[] newBuf = new byte[this.recBufSize];

           if (this.recIndex > length) {

               System.arraycopy(this.recBuf, length, newBuf, 0, this.recIndex - length);

           }

           this.recBuf = newBuf;

           this.recIndex -= length;

    } else {

           int totalread = length;

           if (this.recIndex > 0) {

                totalread -= this.recIndex;

                bos.write(this.recBuf, 0, this.recIndex);

                this.recBuf = new byte[this.recBufSize];

                this.recIndex = 0;

    }

    int readCount = 0;

    while (totalread > 0) {

         if ((readCount = this.in.read(this.recBuf)) > 0) {

                if (totalread > readCount) {

                      bos.write(this.recBuf, 0, readCount);

                      this.recBuf = new byte[this.recBufSize];

                      this.recIndex = 0;

               } else {

                     bos.write(this.recBuf, 0, totalread);

                     byte[] newBuf = new byte[this.recBufSize];

                     System.arraycopy(this.recBuf, totalread, newBuf, 0, readCount - totalread);

                     this.recBuf = newBuf;

                     this.recIndex = (readCount - totalread);

             }

             totalread -= readCount;

        }

   }

}

问题就出在标红的代码部分。如果this.in.read()返回的数据小于等于0时,循环就一直进行下去了。而这种情况在网络拥塞的时候是可能发生的。

至于具体怎么修改就看业务逻辑应该怎么对待这种特殊情况了。

 

最后,总结下排查CPU故障的方法和技巧有哪些:

1、top命令:Linux命令。可以查看实时的CPU使用情况。也可以查看最近一段时间的CPU使用情况。

2、PS命令:Linux命令。强大的进程状态监控命令。可以查看进程以及进程中线程的当前CPU使用情况。属于当前状态的采样数据。

3、jstack:Java提供的命令。可以查看某个进程的当前线程栈运行情况。根据这个命令的输出可以定位某个进程的所有线程的当前运行状态、运行代码,以及是否死锁等等。

4、pstack:Linux命令。可以查看某个进程的当前线程栈运行情况。

转载于:https://www.cnblogs.com/maohuidong/p/9313729.html

你可能感兴趣的文章
Struts2拦截器
查看>>
推荐电影 梦工厂经典电影列表 1996-2012
查看>>
oc js 调用 函数调用栈
查看>>
ZOJ3951 : Independent Set
查看>>
jQuery的$.extend和$.fn.extend作用及区别
查看>>
[C#] async 的三大返回类型
查看>>
数据结构之---C语言实现图的邻接表存储表示
查看>>
Node.js 把图片流送到客户端
查看>>
Android P 功能和 API
查看>>
libuv 中文编程指南(一)序言
查看>>
三步解决EntityFramework Code First中的MissingMethodException错误
查看>>
关于在UITableViewController页面添加UINavigationBar的方法
查看>>
php --with-mysql=mysqlnd
查看>>
登录(ajax提交数据和后台校验)
查看>>
谷歌中国的第一款产品“猜画小歌”
查看>>
HTTP 错误 500.19 - Internal Server Error
查看>>
序列起始值修改
查看>>
蓝点中文_Linux2.0 实验三 用户组管理
查看>>
php表单在提交之后再后退,表单的内容默认是被清空的
查看>>
重写方法Android中的HttpsURLConnection连接
查看>>