项目上线后运行一段时间,突然发现cpu 8个逻辑核心都占用100%,心情很紧张,然后就在网上找了一些解决方法,具体如下: 

       1.查找哪些进程在耗cpu  
       进入服务器,top 命令看一下,发现进程6633占用了800% 
       [root@server ~]# top 
       2.把进程的栈dump到文件里,以便后面的分析
       [root@server ~]# jstack 23812 >> java.txt

       3.看看这个进程里面哪些线程在占用cpu 
       [root@server ~]# top -p 23812 -H 
       一大片占用cpu很高的线程,选一个最高的吧,PID=15362
       4.接着要看刚才dump出来的java.txt日志了,里面会有23812这个进程下面每个线程的栈信息,但是是十六进制显示的,所以先把5159转换成16进制 
       [root@server ~]# printf %0x 15362
       [root@server ~]# 3c02
       5.在cpu日志里找PID=3c02的线程 

       [root@server ~]# vi java.txt

         6.分析原因

[root@localhost ~]# grep "3c02" java.txt 

"JS executor for com.gargoylesoftware.htmlunit.WebClient@4e96f35e" #76858 daemon prio=5 os_prio=0 tid=0x00007f390000b800 nid=0x3c02 runnable [0x00007f3ae13db000]

"JS executor for com.gargoylesoftware.htmlunit.WebClient@4e96f35e" #76858 daemon prio=5 os_prio=0 tid=0x00007f390000b800 nid=0x3c02 runnable [0x00007f3ae13db000]

"JS executor for com.gargoylesoftware.htmlunit.WebClient@4e96f35e" #76858 daemon prio=5 os_prio=0 tid=0x00007f390000b800 nid=0x3c02 runnab

   java.lang.Thread.State: RUNNABLE

        at java.util.ArrayList.<init>(ArrayList.java:177)

        at com.gargoylesoftware.htmlunit.html.HtmlPage.safeGetAttributeListeners(HtmlPage.java:2222)

        - locked <0x00000005cd622798> (a java.lang.String)

        at com.gargoylesoftware.htmlunit.html.HtmlPage.fireHtmlAttributeReplaced(HtmlPage.java:2198)

        at com.gargoylesoftware.htmlunit.html.HtmlElement.setAttributeNS(HtmlElement.java:213)

        at com.gargoylesoftware.htmlunit.html.DomElement.setAttribute(DomElement.java:296)

        at com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleDeclaration.writeToElement(CSSStyleDeclaration.java:652)

        at com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleDeclaration.replaceStyleAttribute(CSSStyleDeclaration.java:576)

        at com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleDeclaration.setStyleAttribute(CSSStyleDeclaration.java:550)

        at com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleDeclaration.setZIndex(CSSStyleDeclaration.java:3956)

        at sun.reflect.GeneratedMethodAccessor135.invoke(Unknown Source)

        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

        at java.lang.reflect.Method.invoke(Method.java:497)

        at net.sourceforge.htmlunit.corejs.javascript.MemberBox.invoke(MemberBox.java:120)

        at net.sourceforge.htmlunit.corejs.javascript.ScriptableObject$GetterSlot.setValue(ScriptableObject.java:295)

        at net.sourceforge.htmlunit.corejs.javascript.ScriptableObject$RelinkedSlot.setValue(ScriptableObject.java:368)

        at net.sourceforge.htmlunit.corejs.javascript.ScriptableObject.putImpl(ScriptableObject.java:2796)

        at net.sourceforge.htmlunit.corejs.javascript.ScriptableObject.put(ScriptableObject.java:521)

        at com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleDeclaration.put(CSSStyleDeclaration.java:2407)

        at net.sourceforge.htmlunit.corejs.javascript.ScriptableObject.putProperty(ScriptableObject.java:2479)

        at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.setObjectProp(ScriptRuntime.java:1574)

        at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.setObjectProp(ScriptRuntime.java:1569)

        at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1253)

        at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:798)

        at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:105)

        at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:411)

        at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:309)

        at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3057)

        at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.exec(InterpretedFunction.java:115)

        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$3.doRun(JavaScriptEngine.java:566)

        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:674)

        - locked <0x00000005cd2f5a08> (a com.gargoylesoftware.htmlunit.html.HtmlPage)

        at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:620)

        at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:513)

        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:575)

        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:550)

        at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptIfPossible(HtmlPage.java:971)

        at com.gargoylesoftware.htmlunit.javascript.background.JavaScriptStringJob.runJavaScript(JavaScriptStringJob.java:50)

        at com.gargoylesoftware.htmlunit.javascript.background.JavaScriptExecutionJob.run(JavaScriptExecutionJob.java:102)

        at com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManagerImpl.runSingleJob(JavaScriptJobManagerImpl.java:328)

        at com.gargoylesoftware.htmlunit.javascript.background.DefaultJavaScriptExecutor.run(DefaultJavaScriptExecutor.java:162)

        at java.lang.Thread.run(Thread.java:745)

找到问题所在让java开发排查