Automatic Resource Management or try-with-resources is a new exception handling mechanism that was introduced in Java 7, which automatically closes the resources used within the try-catch block.
In general most of the time memory leaks happens when we forget to close resources. If we follow ARM concept we could avoid memory leaks caused by ‘forgetting resource closing in finally block’.
Resource
A Resource is an object which is required to be closed once we used it. For example, a File, database connection, network connection … etc.
Technically a Resource is an object that implements java.lang.AutoCloseable.
Syntax
1 2 3 4 5 6 7 8 |
// Resources going to use in try-block need to declare in try parenthesis try(FileReader fr = new FileReader("file path")) { // use the resource here } catch () { } // but we no need to close FileReader explicitly, it will be auto-closed } |
Example 01 : close resource in finally block manually – old way
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.vidvaan.corejava.exception06.arm; import java.io.FileReader; import java.io.IOException; //close resource in finally block manually - old way public class Example01CloseResourceInFinally { public static void main(String args[]) { FileReader fr = null; try { fr = new FileReader("C:\\demo.txt"); char[] a = new char[50]; fr.read(a); // reads file content to the array for (char c : a) System.out.print(c); // prints characters one by one } catch (IOException e) { e.printStackTrace(); } finally { // Explicitly closing resources in finally block. try { fr.close(); // Resources closing may throw exception // We need to write exception handling code on resource closing code } catch (IOException ex) { ex.printStackTrace(); } } } } |
In the above example we are closing FileReader explicitly in finally block. Resources closing may throw exception. We need to write exception handling code on resource closing code. We could avoid all these ‘resource closing code’ with Automatic resource management.
Example 02 : Automatic resource management- new way
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.vidvaan.corejava.exception06.arm; import java.io.FileReader; import java.io.IOException; //Automatic resource management- new way public class Example02TryWithResources { public static void main(String args[]) { // To close resources automatically we need to declare them in try parenthesis try (FileReader fr = new FileReader("C:\\demo.txt")) { char[] a = new char[50]; fr.read(a); // reads file content to the array for (char c : a) System.out.print(c); // prints characters one by one } catch (IOException e) { e.printStackTrace(); } // Here we don't need finally block to close FileReader // FileReader automatically closed } } |
In the above example, we reads file data using the try-with-resources statement. But if we observe we are not closing the FileReader because it will be automatically closed. This is called Automatic resource management.
We can define our own Resource objects by implementing the AutoCloseable interface and override close() method which gets invoked automatically at runtime after try-catch block executed.
We can declare multiple resources with semicolon separated in try parenthesis.
By default all resources declared at the try block are implicitly final.