The story of forgetting to close a file in Java and failing

Introduction

I'm the idiot who has been doing Java for years and forgot to close and caused an error.

Problems that occurred

When performing continuous load tests on a server under development, the phenomenon that the application behaves strangely over time occurred. There was no problem with the CPU or memory usage and the cause remained unknown, but if I left it for a week or two, the application died with an error like "File cannot be opened anymore" in the log. .. After that, when the application checked the number of open file descriptors, it was confirmed that the maximum number of file descriptors that could be opened per process was reached.

The code in question

return Files.list(Paths.get(dirPathStr)).map(path -> path.getFileName().toString())
            .collect(Collectors.toSet());

The code in question is above. A method that returns a list of filenames in a directory using the Stream API, but the directory opened by Files.list () is not closed. Even if I forget to close, I think that the memory itself will be released by GC, but the file descriptors will not be released, so the number of file descriptors will continue to increase each time it is executed. By the way, when using Eclipse for the development environment (Scanner class etc.), if you forget to close in the instance of the class that opens the file, it will warn you, but (as of Eclipse 4.7) the above code does not warn you. why……

Modified code

try (Stream<Path> paths = Files.list(Paths.get(dirPathStr))) {
	return paths.map(path -> path.getFileName().toString()).collect(Collectors.toSet());
}

I tried to close it properly. The increase in file descriptors no longer occurs, and the application works fine.

in conclusion

Lessons below

Recommended Posts

The story of forgetting to close a file in Java and failing
The story of writing Java in Emacs
Get the public URL of a private Flickr file in Java
How to get the length of an audio file in java
[Java improvement case] Experience story of learning Java in 2 months and changing jobs to a programmer
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.
The story of low-level string comparison in Java
The story of making ordinary Othello in Java
A story about the JDK in the Java 11 era
The story of learning Java in the first programming
Measure the size of a folder in Java
A story about trying to operate JAVA File
How to find out the Java version of a compiled class file
Sample program that returns the hash value of a file in Java
How to get the absolute path of a directory running in Java
How to ZIP a JAVA CSV file and manage it in a Byte array
[Java] Get the file in the jar regardless of the environment
Java classes and instances to understand in the figure
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
Gzip-compress byte array in Java and output to file
[Android 9.0 Pie Java] Implement setOnTouchListener in the margin of RecyclerView and close the soft keyboard
[Java] Integer information of characters in a text file acquired by the read () method
A memo about the types of Java O/R mappers and how to select them
How to save a file with the specified extension under the directory specified in Java to the list
Sample code to assign a value in a property file to a field of the expected type
Create a Java Servlet and JSP WAR file to deploy to Apache Tomcat 9 in Gradle
Generate and execute Jar file of Java file belonging to package
A quick explanation of the five types of static in Java
I tried to summarize the basics of kotlin and java
20190803_Java & k8s on Azure The story of going to the festival
Command to check the number and status of Java threads
Activate Excel file A1 cell of each sheet in Java
Create a method to return the tax rate in Java
How to develop and register a Sota app in Java
How to derive the last day of the month in Java
I tried to make a client of RESAS-API in Java
The story of pushing Java to Heroku using the BitBucket pipeline
When I switched to IntelliJ, I got a lot of differences in the encoding of the properties file.
[Java version] The story of serialization
The story of pushing a Docker container to GitHub Package Registry and Docker Hub with GitHub Actions
A program (Java) that outputs the sum of odd and even numbers in an array
Considering the adoption of Java language in the Reiwa era ~ How to choose a safe SDK
Unzip the zip file in Java
How to write comments in the schema definition file in GraphQL Java and reflect them in GraphQL and GraphQL Playground
Log output to file in Java
I want to get a list of the contents of a zip file and its uncompressed size
The story of building a Java version of Minecraft server with GCP (and also set a whitelist)
A story that solved the problem that the Java (jdk) version was too high to use the h2o library in R and R Studio.
[Java] Various summaries attached to the heads of classes and members
It doesn't respond to the description in .js of the packs file
[Java] Get the dates of the past Monday and Sunday in order
Fix the file name of war to the one set in Maven
A story about hitting the League Of Legends API with JAVA
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
Cast an array of Strings to a List of Integers in Java
Read the first 4 bytes of the Java class file and output CAFEBABE
Add a time stamp to the JAR file name in Gradle
From fledgling Java (3 years) to Node.js (4 years). And the impression of returning to Java
I tried to summarize the methods of Java String and StringBuilder
[Java] Returns a Japanese name file in filename of HTTP header