Java怎么获取线程状态及堆栈信息
发表于:2024-10-01 作者:千家信息网编辑
千家信息网最后更新 2024年10月01日,本篇内容介绍了"Java怎么获取线程状态及堆栈信息"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!基本
千家信息网最后更新 2024年10月01日Java怎么获取线程状态及堆栈信息
本篇内容介绍了"Java怎么获取线程状态及堆栈信息"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
基本概念
出现内存泄漏或者运行缓慢场景,有时候无法直接从业务日志看出问题时候,需要分析jvm内存和线程堆栈
线程堆栈信息主要记录jvm线程在某时刻线程执行情况,分析线程状态可以跟踪到程序出问题的地方内存堆栈信息主要记录jvm堆中在某时刻对象使用情况,主要用于跟踪是哪个对象占用了太多的空间,从而跟踪导致内存泄漏的地方
跟踪线程信息
查看当前线程数量
actuator
1.x
http://host:port/metrics/threads //当前进程的线程数http://host:port/metrics/threads.daemon //当前进程后台驻留线程数http://host:port/metrics/threads.peak //当前进程线程数峰值
2.x
http://host:port/actuator/metrics/jvm.threads.daemon //当前进程后台驻留线程数http://host:port/actuator/metrics/jvm.threads.live //当前进程的线程数http://host:port/actuator/metrics/jvm.threads.peak //当前进程线程数峰值
hystrix 线程状态
如果接入了turbine可以直接通过turbine查看整个集群状态
当集群较大的时候,单纯想看hystrix线程池状态,可以单独从hystrix监控统计类里面获取
http://host:port/sys/hystrix/threads
源码如下:
import com.alibaba.fastjson.JSON;import com.netflix.hystrix.HystrixThreadPoolMetrics;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.jmx.export.annotation.ManagedResource;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;import java.util.stream.Collectors;/** * @author yugj * @date 19/5/5 22:17. */@RestController@RequestMapping(path = "/sys/hystrix")@ManagedResource(description = "hystrix Endpoint")@EnableSchedulingpublic class HystrixThreadPoolEndpoint { private boolean showStats = false; private static final Logger log = LoggerFactory.getLogger(HystrixThreadPoolEndpoint.class); @GetMapping(value = "/threads") public ListthreadStats() { return HystrixThreadPoolMetrics.getInstances().stream().map((m) -> { final HystrixThreadStats stats = new HystrixThreadStats(); stats.poolName = m.getThreadPoolKey().name(); stats.cumulativeExecuted = m.getCumulativeCountThreadsExecuted(); stats.currentActiveCount = m.getCurrentActiveCount().intValue(); stats.currentCompletedCount = m.getCurrentCompletedTaskCount().intValue(); stats.currentCorePoolSize = m.getCurrentCorePoolSize().intValue(); stats.currentLargestPoolSize = m.getCurrentLargestPoolSize().intValue(); stats.currentMaxPoolSize = m.getCurrentMaximumPoolSize().intValue(); stats.currentPoolSize = m.getCurrentPoolSize().intValue(); stats.currentQueueSize = m.getCurrentQueueSize().intValue(); stats.currentTaskCount = m.getCurrentTaskCount().intValue(); return stats; }).collect(Collectors.toList()); } @GetMapping(value = "/setShowStats") public String setShowStats(Boolean showStats) { if (showStats != null) { this.showStats = showStats; } return "this.show stats:" + this.showStats; } @Scheduled(fixedRate = 5000) public void showStats() { if (showStats) { List statsList = threadStats(); log.info("thread stats :{}", JSON.toJSONString(statsList)); } } class HystrixThreadStats { private String poolName; private Long cumulativeExecuted; private Integer currentActiveCount; private Integer currentCompletedCount; private Integer currentCorePoolSize; private Integer currentLargestPoolSize; private Integer currentMaxPoolSize; private Integer currentPoolSize; private Integer currentQueueSize; private Integer currentTaskCount; public String getPoolName() { return poolName; } public void setPoolName(String poolName) { this.poolName = poolName; } public Long getCumulativeExecuted() { return cumulativeExecuted; } public void setCumulativeExecuted(Long cumulativeExecuted) { this.cumulativeExecuted = cumulativeExecuted; } public Integer getCurrentActiveCount() { return currentActiveCount; } public void setCurrentActiveCount(Integer currentActiveCount) { this.currentActiveCount = currentActiveCount; } public Integer getCurrentCompletedCount() { return currentCompletedCount; } public void setCurrentCompletedCount(Integer currentCompletedCount) { this.currentCompletedCount = currentCompletedCount; } public Integer getCurrentCorePoolSize() { return currentCorePoolSize; } public void setCurrentCorePoolSize(Integer currentCorePoolSize) { this.currentCorePoolSize = currentCorePoolSize; } public Integer getCurrentLargestPoolSize() { return currentLargestPoolSize; } public void setCurrentLargestPoolSize(Integer currentLargestPoolSize) { this.currentLargestPoolSize = currentLargestPoolSize; } public Integer getCurrentMaxPoolSize() { return currentMaxPoolSize; } public void setCurrentMaxPoolSize(Integer currentMaxPoolSize) { this.currentMaxPoolSize = currentMaxPoolSize; } public Integer getCurrentPoolSize() { return currentPoolSize; } public void setCurrentPoolSize(Integer currentPoolSize) { this.currentPoolSize = currentPoolSize; } public Integer getCurrentQueueSize() { return currentQueueSize; } public void setCurrentQueueSize(Integer currentQueueSize) { this.currentQueueSize = currentQueueSize; } public Integer getCurrentTaskCount() { return currentTaskCount; } public void setCurrentTaskCount(Integer currentTaskCount) { this.currentTaskCount = currentTaskCount; } }}
linux
ps huH p {pid}|wc -l
jstack生成线程堆栈
当服务cup飙升或者出问题需要从主机层面定位时候,使用top -c 命令查看对应哪个进程占用了过高资源
找到资源占用高的进程
明确需要定位的进程通过如下命令找到对应的进程id
ps aux|grep {application alias}
可以通过如下命令定位具体高load线程:
查询进程具体哪个线程占用高loadtop -Hp {进程pid}thread id为十六进制格式转十六进制值printf %x {线程pid}指定特定行数堆栈信息jstack {进程id}|grep -A 200 {线程id}
接下来通过jstack导出对应的线程堆栈
jstack 对应参数如下
-m to print both java and native frames (mixed mode)
-l long listing. Prints additional information about locks
服务器线程相对较多,文件大小较大,一般不会考虑在服务器看,另外这样查也会导致忽略了一些统计信息
通过如下命令导出文件,下载到本地查
jstack -l {pid} >> {dump-file-path}
docker环境涉及一些权限,需要进入docker执行,docker里面进程id根据实际情况,一般会联系运维操作
"Java怎么获取线程状态及堆栈信息"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
线程
进程
堆栈
信息
状态
内存
命令
情况
跟踪
时候
问题
定位
服务
较大
接下来
内容
十六进制
后台
地方
实际
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
武汉市晟骐网络技术有限公司
小翼管家连接不上服务器
三星服务器内存16g和32g
软件开发工程师其他要求
数据库dql操作实验
数据库的发展趋势包括
机场服务器可以自己搭建
如何使用联合国商品数据库
创建数据库文件的sql语句
网络安全快板配乐
直接访问数据库从库
网络安全属于公安专业
三级数据库总分
高桥北的服务器
从规范出发的软件开发
公交公司网络安全应急方案
上海智能化软件开发服务介绍
洛阳市网络安全防护研发中心
数据库业务
高科技推动互联网发展
滨江ne七软件开发招聘
服务器rebuild
额济纳旗网络安全委员会
应用数据库模式与内模式
数据库技术运用总结
数据库字段安全吗
elk数据库数值型转换成字符型
卧龙区网络安全
数据库缩写命名规则
青少年关于网络安全的事例