小工具      在线工具  汉语词典  css  js  c++  java

Java应用cpu使用率过高问题排查(jstack实战模拟操作)

Java,java,linux 额外说明

收录于:42天前

1、将我们实际模拟所需的Jar包上传到linux服务器

scp /Users/sunww/Desktop/demo-0.0.1-SNAPSHOT.jar [email protected]:/httx/packjar

2.启动Jar包

java -jar demo-0.0.1-SNAPSHOT.jar

3.前端发起请求

卷曲 http://localhost:8080/admin/test?a=1

应用程序日志显示循环中打印的数据:

此时,本地计算机的风扇正在剧烈旋转,笔记本电脑很热。

4、使用top命令查看

从上面可以看出pid=5046的进程占用了94%的CPU,所以这个进程有问题。

5、使用top -p pid -H查找消耗CPU较多的进程的线程信息。

我们可以进一步使用如下命令查看5046进程中占用CPU最多的线程,如下:

顶部-p 5046 -H

上图清楚地表明,pid=5092的线程占用了92%的CPU,并在8080端口发出http请求。

6.使用jstack锁定有问题的代码类

首先将上面的pid=5092转换成十六进制

printf "%x \n" 5092

方法一:直接通过grep命令查找13e4

然后使用jstack 5046 | grep 13e4 -A 30 查找问题代码【专门搜索线程信息13e4】,如下图:

上图:可以看出HomeController类的第31行有问题。

方法二:输出5046的所有信息后,使用ctrl + f查找13e4的线程信息

我们还可以打印出5046的所有进程信息,使用ctrl + f找到13e4的线程信息,然后分析

[root@192 ~]# jstack 5046
2021-09-26 07:26:02
Full thread dump OpenJDK 64-Bit Server VM (25.242-b08 mixed mode):

"Attach Listener" #23 daemon prio=9 os_prio=0 tid=0x00007fa798001000 nid=0x1446 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"http-nio-8080-exec-1" #22 daemon prio=5 os_prio=0 tid=0x00007fa774030000 nid=0x13e4 runnable [0x00007fa7b019b000]
   java.lang.Thread.State: RUNNABLE
	at java.io.FileOutputStream.writeBytes(Native Method)
	at java.io.FileOutputStream.write(FileOutputStream.java:326)
	at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
	at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
	- locked <0x00000000e34422f8> (a java.io.BufferedOutputStream)
	at java.io.PrintStream.write(PrintStream.java:482)
	- locked <0x00000000e34422d8> (a java.io.PrintStream)
	at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
	at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291)
	at sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:104)
	- locked <0x00000000e3442418> (a java.io.OutputStreamWriter)
	at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:185)
	at java.io.PrintStream.write(PrintStream.java:527)
	- locked <0x00000000e34422d8> (a java.io.PrintStream)
	at java.io.PrintStream.print(PrintStream.java:611)
	at java.io.PrintStream.println(PrintStream.java:750)
	- locked <0x00000000e34422d8> (a java.io.PrintStream)
	at com.example.demo.HomeController.test(HomeController.java:31)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
	at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
	at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
	at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	- locked <0x00000000e45d7d70> (a org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)

"DestroyJavaVM" #21 prio=5 os_prio=0 tid=0x00007fa7c004b800 nid=0x13b7 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"http-nio-8080-AsyncTimeout" #19 daemon prio=5 os_prio=0 tid=0x00007fa7c1a98800 nid=0x13cc waiting on condition [0x00007fa7aa6f7000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
	at java.lang.Thread.sleep(Native Method)
	at org.apache.coyote.AbstractProtocol$AsyncTimeout.run(AbstractProtocol.java:1138)
	at java.lang.Thread.run(Thread.java:748)

"http-nio-8080-Acceptor-0" #18 daemon prio=5 os_prio=0 tid=0x00007fa7c0a28800 nid=0x13cb runnable [0x00007fa7aa7f8000]
   java.lang.Thread.State: RUNNABLE
	at sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method)
	at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:419)
	at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:247)
	- locked <0x00000000e45f1b78> (a java.lang.Object)
	at org.apache.tomcat.util.net.NioEndpoint$Acceptor.run(NioEndpoint.java:443)
	at java.lang.Thread.run(Thread.java:748)

"http-nio-8080-ClientPoller-1" #17 daemon prio=5 os_prio=0 tid=0x00007fa7c0a26800 nid=0x13ca runnable [0x00007fa7aa8f9000]
   java.lang.Thread.State: RUNNABLE
	at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
	at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
	at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:93)
	at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
	- locked <0x00000000e45dc1e8> (a sun.nio.ch.Util$3)
	- locked <0x00000000e45dc1d8> (a java.util.Collections$UnmodifiableSet)
	- locked <0x00000000e45dbfd0> (a sun.nio.ch.EPollSelectorImpl)
	at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
	at org.apache.tomcat.util.net.NioEndpoint$Poller.run(NioEndpoint.java:775)
	at java.lang.Thread.run(Thread.java:748)

"http-nio-8080-ClientPoller-0" #16 daemon prio=5 os_prio=0 tid=0x00007fa7c0b93800 nid=0x13c9 runnable [0x00007fa7aa9fa000]
   java.lang.Thread.State: RUNNABLE
	at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
	at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
	at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:93)
	at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
	- locked <0x00000000e45f93b0> (a sun.nio.ch.Util$3)
	- locked <0x00000000e45f93a0> (a java.util.Collections$UnmodifiableSet)
	- locked <0x00000000e45f9268> (a sun.nio.ch.EPollSelectorImpl)
	at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
	at org.apache.tomcat.util.net.NioEndpoint$Poller.run(NioEndpoint.java:775)
	at java.lang.Thread.run(Thread.java:748)

"NioBlockingSelector.BlockPoller-1" #15 daemon prio=5 os_prio=0 tid=0x00007fa7c129e800 nid=0x13c8 runnable [0x00007fa7aaafb000]
   java.lang.Thread.State: RUNNABLE
	at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
	at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
	at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:93)
	at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
	- locked <0x00000000e45d64f0> (a sun.nio.ch.Util$3)
	- locked <0x00000000e45d64e0> (a java.util.Collections$UnmodifiableSet)
	- locked <0x00000000e45d63a8> (a sun.nio.ch.EPollSelectorImpl)
	at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
	at org.apache.tomcat.util.net.NioBlockingSelector$BlockPoller.run(NioBlockingSelector.java:339)

"container-0" #14 prio=5 os_prio=0 tid=0x00007fa7c1997000 nid=0x13c7 waiting on condition [0x00007fa7aadfc000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
	at java.lang.Thread.sleep(Native Method)
	at org.apache.catalina.core.StandardServer.await(StandardServer.java:427)
	at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer$1.run(TomcatEmbeddedServletContainer.java:174)

"ContainerBackgroundProcessor[StandardEngine[Tomcat]]" #13 daemon prio=5 os_prio=0 tid=0x00007fa7c0a15000 nid=0x13c6 waiting on condition [0x00007fa7aaefd000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
	at java.lang.Thread.sleep(Native Method)
	at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1355)
	at java.lang.Thread.run(Thread.java:748)

"Service Thread" #7 daemon prio=9 os_prio=0 tid=0x00007fa7c01ad000 nid=0x13c0 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C1 CompilerThread1" #6 daemon prio=9 os_prio=0 tid=0x00007fa7c01a2800 nid=0x13bf waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007fa7c0193000 nid=0x13be waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" #4 daemon prio=9 os_prio=0 tid=0x00007fa7c0191000 nid=0x13bd runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007fa7c0166800 nid=0x13bc in Object.wait() [0x00007fa7b0ffe000]
   java.lang.Thread.State: WAITING (on object monitor)
	at java.lang.Object.wait(Native Method)
	- waiting on <0x00000000e35cbd10> (a java.lang.ref.ReferenceQueue$Lock)
	at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
	- locked <0x00000000e35cbd10> (a java.lang.ref.ReferenceQueue$Lock)
	at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
	at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216)

"Reference Handler" #2 daemon prio=10 os_prio=0 tid=0x00007fa7c0162000 nid=0x13bb in Object.wait() [0x00007fa7c4105000]
   java.lang.Thread.State: WAITING (on object monitor)
	at java.lang.Object.wait(Native Method)
	- waiting on <0x00000000e35cb908> (a java.lang.ref.Reference$Lock)
	at java.lang.Object.wait(Object.java:502)
	at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
	- locked <0x00000000e35cb908> (a java.lang.ref.Reference$Lock)
	at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)

"VM Thread" os_prio=0 tid=0x00007fa7c0158000 nid=0x13ba runnable 

"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007fa7c005e000 nid=0x13b8 runnable 

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007fa7c0060000 nid=0x13b9 runnable 

"VM Periodic Task Thread" os_prio=0 tid=0x00007fa7c01af800 nid=0x13c1 waiting on condition 

JNI global references: 246

7.修改问题代码

通过上面可以锁定HomeController类的println方法(System.out.println)有问题。检查代码,发现存在无限循环打印问题。修改代码后,问题解决。

. . .

相关推荐

额外说明

转:sql server 查询某个表的所有触发器名称

转载链接:http://blog.163.com/fox_tran/blog/static/20918020820121014724759/   查出所有用到某个表的SQL   select   *   from   sysobjects   where

额外说明

求最大公约数

  求两数的最大公约数的方法:   方法一: public int gcd(int a, int b) { int min=Math.min(a,b); while (min>0){ i

额外说明

【Unity3D进阶4-8】Unity3D 游戏框架

一、目录 【Unity3D从入门到进阶】文章目录及设置这个专栏的初衷 二、什么是游戏框架 框架(Framework)是一个框子——指其约束性,也是一个架子——指其支撑性。 IT语境中的框架,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构。在此结

额外说明

Spring,SpringMVC,Spring Boot中常见注解(持续更新)

在学习Java框架时,注解的学习是必不可少的,这篇文章就来记录一下这些框架中的常用注解 常用注解 一,spring 1.声明bean的注解 2.注入bean的注解 3.3.java配置类相关注解 4.切面(AOP)相关注解 二,Spring MVC 三,

额外说明

PyTorch深度学习实战(2)——PyTorch基础

PyTorch深度学习实战(2)——PyTorch基础 0. 前言 1. 搭建 PyTorch 环境 2. PyTorch 张量 2.1 张量初始化 2.2 张量运算 2.3 张量对象的自动梯度计算 3. PyTorch 张量相对于 NumPy 数组的优

额外说明

Qt下多线程的四种使用方法总结及代码示例

文章目录 前言 一、继承QThread,重写run()函数 二、继承QObject,使用moveToThread()函数 三、继承QRunnable,重写run()函数,使用QThreadPool线程池 四、使用QtConcurrent的run()函数

额外说明

Spark-Shell使用Scala的版本

文章目录 spark版本 spark-shell使用的scala版本 spark-core版本 2.4.4 2.11.12 spark-core_2.12 3.1.3 2.12.10 spark-core_2.12 3.2.0 2.12.15 spark

额外说明

自动化测试工具之 selenium

介绍 selenium-- 中文翻译是 “硒”; 这里说的 selenium 是一组自动化测试工具。 官方网址: http://docs.seleniumhq.org/ 这个站点地址看起来有点奇怪, 以docs 开始, 乍一看, 以为这是这个工具文档的地

额外说明

Java实训项目9:GUI学生信息管理系统 - 实现步骤 - 创建数据访问接口

文章目录 七、实现步骤 (五)创建数据访问接口 1、创建学校数据访问接口 2、创建状态数据访问接口 3、创建学生数据访问接口 4、创建用户数据访问接口 七、实现步骤 (五)创建数据访问接口 DAO: Data Acess Object 系统有四张表:t_

额外说明

死磕Lambda表达式(二):Lambda的使用

城市就是森林,每一个男人都是猎手,每一个女人都是陷阱。——《三体》 在哪使用Lambda表达式? 在上一篇文章(传送门)中介绍了Lambda表达式的基本语法,其中的举了一个Lambda表达式的例子,就是按照品牌给口罩列表进行排序: maskList.so

ads via 小工具