---
title: "Java项目服务器cpu占用100%解决办法"
author: "此用户被哥注册"
date: "2014-12-07T13:35:22Z"
tags: []
categories: ["互联网"]
slug: "java-cpu-100"
draft: false
lastmod: "2017-09-04T14:32:23Z"
id: 2163941
url: "https://googlebye.iteye.com/blog/2163941"
---

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

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

   ![cpu1128.log](http://dl2.iteye.com/upload/attachment/0104/1548/9780f08d-984a-36b2-a66b-a8d0fd5c930d.jpg)         6.分析原因  
            看日志，很明显是org.hibernate.exception.ExceptionUtils.getCauseUsingWellKnowTypes(...)这里出问题。这个线程是"RUNABLE"状态的，难道在这里发生了死循环?后来花了很多时间在debug状态下把这个问题重现了(Hibernate 3.3.1.GA版本，通过hessian调用远程服务器报SQLGrammarException异常，就会出现这个问题)。跟踪到hibernate源码里发现了问题:

             

```
public static int getThrowableCount(Throwable throwable) {
		int count = 0;
		while ( throwable != null ) {
			count++;
			throwable = ExceptionUtils.getCause( throwable );
		}
		return count;
}
```

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