Skip to content

Resource not closed (ARCH4080)

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.

public void copy(Path in, Path out) throws IOException {
InputStream src = Files.newInputStream(in);
Files.copy(src, out);
}
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.