RLK.NIORLK (Resource Leak) issues are reported when resources are allocated but not properly disposed after use. Failing to properly dispose a resource can lead to such problems as:
An RLK.NIO warning indicates that a NIO object is not closed on exit. Vulnerability and riskResources such as streams, connections and graphic objects must be explicitly closed. The close operation can unblock transactions or flush file changes in the file system. While a resource will eventually be closed by the garbage collector, resource exhaustion can occur before garbage collection starts. Depending on the nature of the resource, various exceptions will be thrown on a failed attempt to allocate another resource, for example: java.io.FileNotFoundException: Too many open files or too many database connections. Mitigation and preventionExplicitly close all resources that have the close method, even those that you think are not doing anything significant. Future code changes will then be safe from such errors. Example 121 public void channelData(final FileInputStream stream, final byte[] data) { 22 try { 23 DatagramChannel ch = DatagramChannel.open(); // Resource allocated 24 ch.configureBlocking(true); 25 ByteBuffer packet = ByteBuffer.wrap(data); 26 ch.send(packet, dest); 27 ch.close(); 28 } catch (IOException e) { 29 System.err.println(e.getMessage()); 30 } 31 } RLK.NIO is reported for the snippet on line 23: DatagramChannel 'ch' is never disposed after it has been created. Example 221 public void channelData(final FileInputStream stream, final byte[] data) { 22 DatagramChannel ch = null; 23 try { 24 ch = DatagramChannel.open(); // Resource allocated 25 try { 26 ch.configureBlocking(true); 27 ByteBuffer packet = ByteBuffer.wrap(data); 28 ch.send(packet, dest); 29 } finally { 30 ch.close(); // Resource released 31 } 32 } catch (IOException e) { 33 System.err.println(e.getMessage()); 34 } 35 } The snippet from the previous section is fixed; RLK.NIO is not reported here. ExtensionThis checker can be extended through the Klocwork knowledge base. See Tuning Java analysis for more information. |