当前位置:网站首页>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(最大允许打开文件数)

边栏推荐
- Osgconv tool usage
- Alibaba canaladmin deployment and canal cluster Ha Construction
- Unity editor expansion - the framework and context of unity imgui
- Really explain the five data structures of redis
- OpenGL learning notes
- Kunlunbase meetup is waiting for you!
- [rust note] 10 operator overloading
- Mysql容器化(1)Docker安装MySQL
- Concurrent programming (VI) ABA problems and solutions under CAS
- UE4 source code reading_ Bone model and animation system_ Animation process
猜你喜欢

Phpstudy 80 port occupied W10 system

UE4 source code reading_ Mobile synchronization

Markdown learning

Concurrent programming (V) detailed explanation of atomic and unsafe magic classes

ES6 promise learning notes

单调栈-42. 接雨水

First Servlet

Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)
![[concurrent programming] explicit lock and AQS](/img/5f/a80751a68726f53d11133810f454a3.jpg)
[concurrent programming] explicit lock and AQS

DOM 渲染系统(render mount patch)响应式系统
随机推荐
基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
[rust notes] 06 package and module
Life cycle of Servlet
Creation and content of mapnode -- osgearth rendering engine series (2)
Try to reprint an article about CSDN reprint
Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
VIM learning notes from introduction to silk skating
Explain sizeof, strlen, pointer, array and other combination questions in detail
[concurrent programming] thread foundation and sharing between threads
createjs easeljs
Final review of Database Principles
Monotonic stack -503 Next bigger Element II
Creation of osgearth earth files to the earth ------ osgearth rendering engine series (1)
【Rust 笔记】09-特型与泛型
第一个Servlet
Unity Editor Extension - event handling
Cesium for unreal quick start - simple scenario configuration
[rust notes] 02 ownership
[rust notes] 11 practical features
Osganimation library parsing




