Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 reading file list, but files remain open using up resources till server freezes

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.

like image 935
Mike Murphy Avatar asked Dec 22 '18 12:12

Mike Murphy


1 Answers

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...
}
like image 187
Slaw Avatar answered Nov 19 '22 18:11

Slaw