Close the outermost stream ASAP in XX-Shared object | CAST Appmarq

Close the outermost stream ASAP in XX-Shared object


Rule Definition
A frequent issue when dealing with stream is resource leak. This mainly comes from an incorrect code that miss to close the stream in any cases. Incorrect resource management is a common source of failures in production applications, with the usual pitfalls being database connections and file descriptors remaining opened after an exception has occurred somewhere else in the code. This leads to application servers being frequently restarted when resource exhaustion occurs, because operating systems and server applications generally have an upper-bound limit for resources.

Remediation
You can: - use the try with resource to declare the resource that must be closed (available in java 7) - or close the resource in a finally block. - or annotate this resource with @Cleanup annotation (lombok.Cleanup)

Violation Code Sample
private void incorrectWriting() throws IOException {
    DataOutputStream out = new DataOutputStream(new FileOutputStream("data"));
    out.writeInt(666);
    out.writeUTF("Hello");
    out.close();
} // VIOLATION
Fixed Code Sample
with finally
~~~~~~~~~~~~   
private void correctWriting() throws IOException {
    DataOutputStream out = null;
    try {
        out = new DataOutputStream(new FileOutputStream("data"));
        out.writeInt(666);
        out.writeUTF("Hello");
    } finally {
        if (out != null) {
            out.close(); // FIX
        }
    }        
}

with Java 7
~~~~~~~~~~~
private void writingWithARM() throws IOException {
    try (DataOutputStream out 
            = new DataOutputStream(new FileOutputStream("data"))) { // FIX
        out.writeInt(666);
        out.writeUTF("Hello");
    }
}

with @Cleanup annotation
~~~~~~~~~~~~~~~~~~~~~~~~
private void writingWithCleanup() throws IOException {
    @Cleanup DataOutputStream out = new DataOutputStream(new FileOutputStream("data")); // FIX
    out.writeInt(666);
    out.writeUTF("Hello");
}

Reference
http://stackoverflow.com/questions/15405396/is-it-necessary-to-close-input-output-streams-created-from-a-sockets-io-streams http://www.oracle.com/technetwork/articles/java/trywithresources-401775.html

Related Technologies

Health Factor

  Efficiency


Technical Criterion
Efficiency - Memory, Network and Disk Space Management

About CAST Appmarq

CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.

Benchmark Statistics

Global Compliance

nan%

Total Violations
0
Total Opportunities
0
Average Violations / App.
nan
The compliance score represents 1 minus the ratio between the number of times a rule has been violated compared to the number of opportunities in a set of applications that the rule could have been violated.

Industry Insights

Utilities

99.92%