java 服务器cpu占用过高解决方案

项目上线后运行一段时间,突然发现cpu 8个逻辑核心都占用100%,心情很紧张,然后就在网上找了一些解决方法,具体如下: 
       1.查找哪些进程在耗cpu  
       进入服务器,top 命令看一下,发现进程6633占用了800% 
        [root@3server ~]# top 
       2.把进程的栈dump到文件里,以便后面的分析
        [root@3server ~]# jstack 6633 > cpu1128.log 
       3.看看这个进程里面哪些线程在占用cpu 
        [root@3server ~]# top -p 6633 -H 
       一大片占用cpu很高的线程,选一个最高的吧,PID=5159 
       4.接着要看刚才dump出来的cpu日志了,里面会有6633这个进程下面每个线程的栈信息,但是是十六进制显示的,所以先把5159转换成16进制 
        [root@3server ~]# printf "%0x\n" 5159 
       [root@3server ~]# 1427 
       5.在cpu日志里找PID=1427的线程 

       [root@3server ~]# vi cpu1128.log

  cpu1128.log         6.分析原因
            看日志,很明显是org.hibernate.exception.ExceptionUtils.getCauseUsingWellKnowTypes(...)这里“卡住”了。这个线程是"RUNABLE"状态的,难道在这里发生了死循环?后来花了很多时间在debug状态下把这个问题重现了(Hibernate 3.3.1.GA版本,通过hessian调用远程服务器报SQLGrammarException异常,就会出现这个问题)。跟踪到hibernate源码里发现了问题:

             

Java代码   收藏代码
  1. public static int getThrowableCount(Throwable throwable) {  
  2.         int count = 0;  
  3.         while ( throwable != null ) {  
  4.             count++;  
  5.             throwable = ExceptionUtils.getCause( throwable );  
  6.         }  
  7.         return count;  
  8. }  

         这个方法里,throwable和它的cause引用的同一个SQLGrammarException对象,导致在while这里产生了死循环。这肯定是hibernate的bug了。于是把hibernate升级到3.3.2.GA(原来是3.3.1.GA,不敢升太多)问题解决。



结合另一篇文章分析:https://www.jianshu.com/p/3667157d63bb

jstack 14063 |grep 36f1 -A 30  14063指的是java进程

猜你喜欢

转载自blog.csdn.net/qq_22978533/article/details/80367913