当前位置:网站首页>too many open files解决方案
too many open files解决方案
2022-07-03 08:38:00 【Clovemeo】

测试环境在运行一段时间后出现了too many open files,导致一个定时上报redis的任务频繁失败。
linux默认为1024,可通过ulimit -n命令进行数量调整。
例:ulimit -n 4096
非root用户最大只能设置到4096,需要更多的话需要root权限。
执行命令ps -ef | grep java,查出进程id:13945。

执行lsof -p 13945
发现有大量的文件句柄没有释放。怀疑是组内小伙伴遍历文件时没有close导致。
于是从代码中寻找蛛丝马迹。
发现原因是delete方法里使用了DirectoryStream并没有close。
java8里的Files.newDirectoryStream(p),通常都使用try(Files.newDirectoryStream()){ } catch() { }的方式,来避免显式close释放资源。
因此判断当时写这段代码的同学看Files.newDirectoryStream()示例时,没看到close()方法误以为不需要释放文件就直接进行使用。导致了too many open files的异常。另外需要注意的是,在try()中打开的文件,不要执行删除操作,否则同样会导致句柄无法释放问题。例如:
try(InputStream inputStream = new FileInputStream(new File(path))) { // doSomething(); } catch(Exception e) { }finally{ Files.deleteIfExists(Paths.get(path)); }会导致如下结果:

文件句柄未释放
总结:
遇到too many open files时,- 执行 lsof -p <pid>,查看该进程打开的句柄
若句柄数不正常,则根据打开的句柄检查不正常的原因,若句柄正常,则进行如下第二步。 执行 unlimit -a,查看open files(最大允许打开文件数)

边栏推荐
- 分配异常的servlet
- Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
- Servlet的生命周期
- 如何应对数仓资源不足导致的核心任务延迟
- php-fpm软件的安装+openresty高速缓存搭建
- Notes and bugs generated during the use of h:i:s and y-m-d
- Osgearth target selection
- Downward compatibility and upward compatibility
- Chocolate installation
- Introduction to Base64 coding
猜你喜欢
![[concurrent programming] concurrent tool class of thread](/img/16/2b4d2b3528b138304a1a3918773ecf.jpg)
[concurrent programming] concurrent tool class of thread

请求参数的发送和接收

Unity editor expansion - controls, layouts

Message queue for interprocess communication

Alibaba canaladmin deployment and canal cluster Ha Construction

Annotations simplify configuration and loading at startup

Installation of PHP FPM software +openresty cache construction

Graphics_ Learnopongl learning notes

Introduction to Base64 coding

Deep parsing (picture and text) JVM garbage collector (II)
随机推荐
Message queue for interprocess communication
Unity editor expansion - the design idea of imgui
Servlet的生命周期
Allocation exception Servlet
Mxone Pro adaptive 2.0 film and television template watermelon video theme apple cmsv10 template
Unity notes 1
How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework
【Rust 笔记】12-闭包
Osgearth starry background
UE4 source code reading_ Bone model and animation system_ Animation compression
Cesium for unreal quick start - simple scenario configuration
【Rust笔记】05-错误处理
Jupyter remote server configuration and server startup
Sending and receiving of request parameters
Life cycle of Servlet
Simple demo of solving BP neural network by gradient descent method
22-06-28 西安 redis(02) 持久化机制、入门使用、事务控制、主从复制机制
Introduction to Base64 coding
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
Eating fruit




