当前位置:网站首页>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(最大允许打开文件数)
边栏推荐
- [concurrent programming] consistency hash
- Concurrent programming (V) detailed explanation of atomic and unsafe magic classes
- On the difference and connection between find and select in TP5 framework
- 22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism
- 单调栈-84. 柱状图中最大的矩形
- Constraintlayout's constraintset dynamically modifies constraints
- [MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle
- How does unity fixedupdate call at a fixed frame rate
- Osgearth topographic shading map drawing
- UE4 source code reading_ Bone model and animation system_ Animation compression
猜你喜欢
Kunlunbase meetup is waiting for you!
Unity interactive water ripple post-treatment
Graphics_ Games101/202 learning notes
Jupyter remote server configuration and server startup
JS ternary operator - learning notes (with cases)
[concurrent programming] working mechanism and type of thread pool
UE4 source code reading_ Bone model and animation system_ Animation process
22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism
UE4 source code reading_ Mobile synchronization
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
随机推荐
Osgearth target selection
MySQL index types B-tree and hash
[rust notes] 08 enumeration and mode
Explain sizeof, strlen, pointer, array and other combination questions in detail
UE4 source code reading_ Bone model and animation system_ Animation node
How to deal with the core task delay caused by insufficient data warehouse resources
22-05-26 西安 面试题(01)准备
Animation_ IK overview
ES6 promise learning notes
Unity Editor Extension - drag and drop
请求参数的发送和接收
Allocation exception Servlet
Redis cluster series 4
[concurrent programming] explicit lock and AQS
Dom4j遍历和更新XML
基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
Chocolate installation
22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism
Eating fruit
Alibaba canaladmin deployment and canal cluster Ha Construction