This is a copy of code that I run on a tomcat server on a scheduler. When I check the status of the server I can see the no of open files increasing
This is the command used to check open files
sudo lsof -p $(pidof java) | grep "DIR" | wc -l
This is an example of the code wrapped in a unit test.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public class OpenFilesTest {
@Test
public void FileRemainOpen() throws IOException {
String path = "/data/cache/hotels/from_ivector";
List <String> files = new ArrayList<String>();
Files.list(Paths.get(path))
.filter(Files::isRegularFile)
.forEach(file -> {
String name = file.getFileName().toString().toLowerCase();
if (name.endsWith(".csv") || name.endsWith(".txt")) {
name = file.getFileName().toFile().getName();
files.add(name);
}
});
}
}
Eventually the resources run out and the server freezes.
You should close the Stream
when done. From the Javadoc of Files.list
:
The returned stream contains a reference to an open directory. The directory is closed by closing the stream.
Example:
try (Stream<Path> stream = Files.list(directory)) {
// use the stream...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With