Files.list does not use try-with-resources processing, which triggers the Linux system Open Files limit, which in turn causes the Java program to report that there are too many open files.

Blog directory

Article directory


in conclusion

When using the Files toolkit, you need to pay extra attention to the return value type. If it is the two interfaces Stream and DirectoryStream, you need to Call its close method promptly to release resources. It is recommended to use the more elegant try-with-resources structure

If you do not execute close to release resources, open files and other resources will be occupied until the Java process terminates. When there are enough open resources, it may trigger the upper limit of process open files configured by the Linux system, causing the Java process to function abnormally. , report an error打开的文件过多

process

First, I found a system exception when uploading files. After checking the log, I found that there were too many open files. Through lsof -p 47090 | wc -l, I found that the Java process had opened 4000+ files. Through ulimit -a or ulimit -n Check the system configuration. The upper limit of Open Files is 1024. Check the open file list through lsof -p 47090 and find that there are many folder directories for uploading files. , similar to the following

...
java    47090 root  472r      DIR              253,2        47 15032447518 /home/bid/upload/53b9bbe6e29e7eadeffa7c03962108c5
java    47090 root  473r      DIR              253,2        47 15032447518 /home/bid/upload/53b9bbe6e29e7eadeffa7c03962108c5
java    47090 root  474r      DIR              253,2        50 16643000312 /home/bid/upload/799e6138b45ac922d9c34a218a47c3a8
java    47090 root  475r      DIR              253,2        47  1077111019 /home/bid/upload/c942c119927d83a3d9bb4e13a46adc75
java    47090 root  477r      DIR              253,2        53        7160 /home/bid/upload/66a37cd0d1f4c76c9aaafcfbb82203d9
java    47090 root  478r      DIR              253,2        47  1077111019 /home/bid/upload/c942c119927d83a3d9bb4e13a46adc75
java    47090 root  481r      DIR              253,2        53  1611530376 /home/bid/upload/41a9b0ca26a194bf204ccf83ee199c67
java    47090 root  482r      DIR              253,2        50  5368709550 /home/bid/upload/8ec135c1594ef233337b2a041a558c20
java    47090 root  485r      DIR              253,2        50  5368709550 /home/bid/upload/8ec135c1594ef233337b2a041a558c20
...

After restarting the Java process, the number of open files is about 350, and files can be uploaded normally. However, as the upload file interface is called, the number of open files keeps increasing, and the corresponding file directory is always open and cannot be released in time. , I suspect that the stream or the like was not closed in time, resulting in resources not being released.

Check the code and find that IO streams such as InputStream / OutputStream are not used, but the Files.list method is used, and its return value type is Stream<Path> , also need to close. After adding try-with-resources processing, restart the Java process and find that the number of open files no longer increases and remains at around 350, and the directory types in the open file list will be released soon. Problem Solving

Look at the Files tool class and find that several methods return Stream<Path> or DirectoryStream<Path>, such as find / < /span>, all need to be processed by try-with-resources / / lines / listnewDirectoryStreamwalk

Guess you like

Origin blog.csdn.net/mrathena/article/details/134681153