简体   繁体   English

对导致服务器因“打开的文件过多”而挂起的 Java 程序进行故障排除

[英]Troubleshooting a Java program that causes a server to hang with “too many files open”

I have a Java program runs (on linux) for a while and then causes the server to lock up with "Too many files open"我有一个 Java 程序运行(在 linux 上)一段时间,然后导致服务器锁定为“打开的文件太多”

After restarting the machine, I run the java program again and then execute the lsof command against it's pid.重新启动机器后,我再次运行 java 程序,然后针对它的 pid 执行 lsof 命令。 A large number of lines with the following output are produced:大量生产以下output的线:

java    971 uknown  980u     sock                0,9       0t0  20461 protocol: TCPv6

Does this means the program is opening multiple tcp connections & not closing them?这是否意味着程序正在打开多个 tcp 连接而不是关闭它们?

What further steps can i take to troubleshoot this?我可以采取哪些进一步的步骤来解决此问题?

It means your program is opening file descriptors but not closing them.这意味着您的程序正在打开文件描述符但没有关闭它们。 It may be sockets or file handlers.它可能是 sockets 或文件处理程序。 So it is causing resource leak.所以它导致资源泄漏。 Make sure all file handlers are closed and make it ready for garbage collected.确保所有文件处理程序都已关闭并准备好进行垃圾收集。

As dan1st pointed out in comment, one preventive measure to avoid this scenario is using try-with-resources.正如 dan1st 在评论中指出的那样,避免这种情况的一种预防措施是使用 try-with-resources。 This will implements the AutoClosable interface and make sure automatic resource closing.这将实现 AutoClosable 接口并确保自动关闭资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM