Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java nio2 Directory is not closed. Causes "too many open files" error [closed]

I am creating a lot of temp directories for batch processing using this:

Path tmp = Files.createTempDirectory("tmp-images");

The directory may contain 1-50 image files. I am deleting the directory after processing using Apache FileUtils:

FileUtils.deleteDirectory(tmp.toFile());

The directory is deleted but I am running into "too many open files". lsof is giving me this:

java    DIR  253,0   6  136899239 /opt/tomcat/temp/tmp-images1570439806870910607 (deleted)
java    DIR  253,0   6  136899237 /opt/tomcat/temp/tmp-images2456811184361231168 (deleted)
java    DIR  253,0   6  136899237 /opt/tomcat/temp/tmp-images2456811184361231168 (deleted)
java    DIR  253,0   6  136899238 /opt/tomcat/temp/tmp-images8109733179118089091 (deleted)
java    DIR  253,0   6   69527104 /opt/tomcat/temp/tmp-images8763413559313243911 (deleted)
java    DIR  253,0   6  136927313 /opt/tomcat/temp/tmp-images8168355305097994981 (deleted)       

How to delete nio2 temp directory without open file handle?

like image 345
Roman K Avatar asked Jul 29 '16 06:07

Roman K


People also ask

How do I investigate too many open files?

try to identify the source of the problem. - 1 - Check the current limits. - 2 - Check the limits of a running process. - 3 - Tracking a possible file descriptors leak.


1 Answers

I've found the bug. I've used this:

List<Path> result = Files.list(tmp).collect(Collectors.toList());

This leaves the directory OPEN two times!

The right way is actually this:

List<Path> result;
try(Stream<Path> stream = Files.list(tmp)){
    result = stream.collect(Collectors.toList());
}

java doc:

The returned stream encapsulates a DirectoryStream. If timely disposal of file system resources is required, the try-with-resources construct should be used to ensure that the stream's close method is invoked after the stream operations are completed.

like image 131
Roman K Avatar answered Oct 14 '22 09:10

Roman K