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

边栏推荐
- Graphics_ Games101/202 learning notes
- Cesium for unreal quick start - simple scenario configuration
- 【Rust笔记】06-包和模块
- 【Rust 笔记】08-枚举与模式
- Unity Editor Extension - drag and drop
- Find the intersection of line segments
- 22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
- [rust notes] 05 error handling
- How does unity fixedupdate call at a fixed frame rate
- Advanced OSG collision detection
猜你喜欢

Jupyter remote server configuration and server startup

VIM learning notes from introduction to silk skating

单调栈-503. 下一个更大元素 II

UE4 source code reading_ Bone model and animation system_ Animation node

Binary to decimal, decimal to binary

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

Introduction to Base64 coding

SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time

Redux - learning notes
![[concurrent programming] Table hopping and blocking queue](/img/b7/023991a00956e469af855e7a81e126.jpg)
[concurrent programming] Table hopping and blocking queue
随机推荐
php-fpm软件的安装+openresty高速缓存搭建
Dealing with duplicate data in Excel with xlwings
UE4 source code reading_ Mobile synchronization
UE4 source code reading_ Bone model and animation system_ Animation compression
【Rust笔记】05-错误处理
createjs easeljs
UE4 source code reading_ Bone model and animation system_ Animation process
【Rust 笔记】12-闭包
UE4 source code reading_ Bone model and animation system_ Animation node
22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
分配异常的servlet
Try to reprint an article about CSDN reprint
Osgearth topographic shading map drawing
[concurrent programming] thread foundation and sharing between threads
Unity editor expansion - controls, layouts
Life cycle of Servlet
第一个Servlet
Find the intersection of line segments
[rust notes] 06 package and module
Log4j2 vulnerability recurrence and analysis




