Resource Leak: Stream Not Closed

warning: [resource] resource of type InputStream is never closed

Quick Answer

You opened an I/O resource without closing it. Use try-with-resources to ensure automatic cleanup.

Why This Happens

Streams, connections, and other resources that implement AutoCloseable must be closed after use to release system resources. Not closing them causes resource leaks that can lead to file descriptor exhaustion, connection pool depletion, and memory leaks.

The Problem

InputStream in = new FileInputStream("data.txt");
byte[] data = in.readAllBytes();
// Stream never closed - resource leak!

The Fix

try (InputStream in = new FileInputStream("data.txt")) {
    byte[] data = in.readAllBytes();
} // Automatically closed here, even if an exception occurs

Step-by-Step Fix

  1. 1

    Identify the leaked resource

    Find the resource (stream, connection, reader) that is opened but not closed in all code paths.

  2. 2

    Check for try-with-resources

    Verify that the resource is declared in a try-with-resources statement.

  3. 3

    Add try-with-resources

    Wrap the resource in try (Resource r = ...) { } to ensure it is always closed, even when exceptions occur.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free