Resource not closed (ARCH4080)
Ce contenu n’est pas encore disponible dans votre langue.
Key java.performance.disposable-not-disposed · Severity error · Category performance
If an AutoCloseable is created and left without calling close(), the resource it owns (file handle, socket, memory) is never released and leaks. Try-with-resources guarantees closure.
Incorrect
Section titled “Incorrect”public void copy(Path in, Path out) throws IOException { InputStream src = Files.newInputStream(in); Files.copy(src, out);}Correct
Section titled “Correct”public void copy(Path in, Path out) throws IOException { try (InputStream src = Files.newInputStream(in)) { Files.copy(src, out); }}Create resource objects in a try-with-resources block to guarantee they are closed.