Java服务器线程查看和线程优化

白色玫瑰 程序猿

时间: 2023-07-11 阅读: 1 字数:10700

{}
Linux最大线程数限制及当前线程数查询 1、总结系统限制有: /proc/sys/kernel/pid_max #查系统支持的最大线程数,一般会很大,相当于理论值 .../proc/sys/kernel/thread-max ...max_user_process(ulimit -u) #...

目录

Linux最大线程数限制及当前线程数查询

1、总结系统限制有:

/proc/sys/kernel/pid_max #查系统支持的最大线程数,一般会很大,相当于理论值

/proc/sys/kernel/thread-max

max_user_process(ulimit -u) #系统限制某用户下最多可以运行多少进程或线程

/proc/sys/vm/max_map_count

硬件内存大小

2、Java虚拟机本身限制:

-Xms #intial java heap size

-Xmx #maximum java heap size

-Xss #the stack size for each thread

3、查询当前某程序的线程或进程数

pstree -p ps -e | grep java | awk '{print $1}' | wc -l

pstree -p 3660 | wc -l

4、查询当前整个系统已用的线程或进程数

pstree -p | wc -l

1、 cat /proc/${pid}/status

2、pstree -p ${pid}

3、top -p ${pid} 再按H 或者直接输入 top -bH -d 3 -p ${pid}

top -H

手册中说:-H : Threads toggle

加上这个选项启动top,top一行显示一个线程。否则,它一行显示一个进程。

4、ps xH

手册中说:H Show threads as if they were processes

这样可以查看所有存在的线程。

5、ps -mp <PID>

手册中说:m Show threads after processes

这样可以查看一个进程起的线程数。

一台Java服务器能跑多少个线程?这个问题来自一次线上报警如下图,超过了我们的配置阈值。

图:京东自研UMP监控分析

打出jstack文件,通过IBM Thread and Monitor Dump Analyzer for Java工具查看如下:

图:IBM Thread and Monitor Dump Analyzer for Java

共计1661个线程,和监控数据得出的吻合。但这个数量应该是大了,我们都知道线程多了,就会有线程切换,带来性能开销。

当时就想到一台Java服务器到底可以跑多少个线程呢?跟什么有关系?现整理如下。

每个线程都有一个线程栈空间通过-Xss设置,查了一下我们服务器的关于jvm内存的配置

<table> <tbody> <tr style="border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:auto!important; background:none!important"> <td class="gutter" style="padding:20px 0px; line-height:1.6; font-family:'Lantinghei SC','Open Sans',Arial,'Hiragino Sans GB','Microsoft YaHei',微软雅黑,STHeiti,'WenQuanYi Micro Hei',SimSun,sans-serif; color:rgb(153,153,153); border-bottom-width:1px; border-bottom-style:solid; border-bottom-color:rgb(231,231,231); margin:0px!important; font-size:1em!important; border-top-width:0px!important; border-right-width:0px!important; border-left-width:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:auto!important; background:none!important">
1

2

3 </td> <td class="code" style="padding:20px 0px; line-height:1.6; font-family:'Lantinghei SC','Open Sans',Arial,'Hiragino Sans GB','Microsoft YaHei',微软雅黑,STHeiti,'WenQuanYi Micro Hei',SimSun,sans-serif; color:rgb(153,153,153); border-bottom-width:1px; border-bottom-style:solid; border-bottom-color:rgb(231,231,231); width:711px; margin:0px!important; font-size:1em!important; border-top-width:0px!important; border-right-width:0px!important; border-left-width:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; min-height:auto!important; background:none!important">
-Xms4096m
-Xmx4096m
-XX:MaxPermSize=1024m
</td> </tr> </tbody> </table>nn

只有这三个,并没有-Xss 和-XX:ThreadStackSize的配置,因此是走的默认值。

图:JVM的默认栈大小

可以通过如下命令打印输出默认值的大小,命令:

<table> <tbody> <tr style="border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:auto!important; background:none!important"> <td class="gutter" style="padding:20px 0px; line-height:1.6; font-family:'Lantinghei SC','Open Sans',Arial,'Hiragino Sans GB','Microsoft YaHei',微软雅黑,STHeiti,'WenQuanYi Micro Hei',SimSun,sans-serif; color:rgb(153,153,153); border-bottom-width:1px; border-bottom-style:solid; border-bottom-color:rgb(231,231,231); margin:0px!important; font-size:1em!important; border-top-width:0px!important; border-right-width:0px!important; border-left-width:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:auto!important; background:none!important">
1 </td> <td class="code" style="padding:20px 0px; line-height:1.6; font-family:'Lantinghei SC','Open Sans',Arial,'Hiragino Sans GB','Microsoft YaHei',微软雅黑,STHeiti,'WenQuanYi Micro Hei',SimSun,sans-serif; color:rgb(153,153,153); border-bottom-width:1px; border-bottom-style:solid; border-bottom-color:rgb(231,231,231); width:711px; margin:0px!important; font-size:1em!important; border-top-width:0px!important; border-right-width:0px!important; border-left-width:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; min-height:auto!important; background:none!important">
jinfo -flag ThreadStackSize
</td> </tr> </tbody> </table>nn

例如:

<table> <tbody> <tr style="border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:auto!important; background:none!important"> <td class="gutter" style="padding:20px 0px; line-height:1.6; font-family:'Lantinghei SC','Open Sans',Arial,'Hiragino Sans GB','Microsoft YaHei',微软雅黑,STHeiti,'WenQuanYi Micro Hei',SimSun,sans-serif; color:rgb(153,153,153); border-bottom-width:1px; border-bottom-style:solid; border-bottom-color:rgb(231,231,231); margin:0px!important; font-size:1em!important; border-top-width:0px!important; border-right-width:0px!important; border-left-width:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:auto!important; background:none!important">
1

2 </td> <td class="code" style="padding:20px 0px; line-height:1.6; font-family:'Lantinghei SC','Open Sans',Arial,'Hiragino Sans GB','Microsoft YaHei',微软雅黑,STHeiti,'WenQuanYi Micro Hei',SimSun,sans-serif; color:rgb(153,153,153); border-bottom-width:1px; border-bottom-style:solid; border-bottom-color:rgb(231,231,231); width:711px; margin:0px!important; font-size:1em!important; border-top-width:0px!important; border-right-width:0px!important; border-left-width:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; min-height:auto!important; background:none!important">
[root@host-192-168-202-229 ~] #jinfo -flag ThreadStackSize 1807
-XX:ThreadStackSize=1024
</td> </tr> </tbody> </table>nn

不考虑系统限制,可以通过如下公式计算,得出最大线程数量。

线程数量 =(机器本身可用内存 - JVM分配的堆内存)/ Xss的值,比如我们的容器本身大小是8G,堆大小是4096M,-Xss默认值,可以得出最大线程数量:4096个。

根据计算公式,得出如下结论:

结论1:jvm堆越大,系统创建的线程数量越小。

结论2:当-Xss的值越小,可生成线程数量越多。

我们知道操作系统分配给每个进程的内存大小是有限制的,比如32位的Windows是2G。因此操作系统对一个进程下的线程数量是有限制的,不能无限的增多。经验值:3000-5000左右(我没有验证)。

刚才说的是不考虑系统限制的情况,那如果考虑系统限制呢,主要跟以下几个参数有关系:

/proc/sys/kernel/pid_max 增大,线程数量增大,pid_max有最高值,超过之后不再改变,而且32,64位也不一样

/proc/sys/kernel/thread-max 系统可以生成最大线程数量

max_user_process(ulimit -u)centos系统上才有,没有具体研究

/proc/sys/vm/max_map_count 增大,数量增多

线程是非常宝贵的资源,我们要严格控制线程的数量,像上面我们的截图情况,显然线程数量过多。这个是跟我们自己配置了fixed大小的线程池有关系。京东有自己的rpc框架jsf,里面可以针对每个服务端口设置线程大小。

原文地址:https://blog.csdn.net/u011001084/article/details/75281761?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168904454516800225524999%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=168904454516800225524999&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-11-75281761-null-null.142^v88^control_2,239^v2^insert_chatgpt&utm_term=java%E4%BC%98%E5%8C%96

本文章网址:https://www.sjxi.cn/detil/b7f799f024134d96be4d0822fa928d46
最新评论
当前未登陆哦
登陆后才可评论哦

湘ICP备2021009447号

×

(穷逼博主)在线接单

QQ: 1164453243

邮箱: abcdsjx@126.com

前端项目代做
前后端分离
Python 爬虫脚本
Java 后台开发
各种脚本编写
服务器搭建
个人博客搭建
Web 应用开发
Chrome 插件编写
Bug 修复