Many times I met the statement that the application should always explicitly close all the resources that it opened.
My approach to programming is rather pragmatic and I don't like to blindly follow any convention that I don't clearly see benefits of. Hence my question.
Let's assume that:
Do I really have to care about closing all the resources that I opened? I guess all the resources I opened will be closed/released when the application/virtual machine exits. Am I right?
If that's true, are there any convincing reasons to care about closing resources in such small, short working application?
UPDATE:
The question is purely hypothetical, but the argument for not caring about that is that I may be just hacking together some quick script and don't want to write any unnecessary code not directly related to the problem at hand: closing resources, doing all this verbose try-catch-finally stuff, handling exceptions that I don't care about etc.
The point of the question is whether there are any practical consequences of not doing it.
lang. AutoCloseable . Because the FileReader and BufferedReader instances are declared in a try -with-resource statement, they will be closed regardless of whether the try statement completes normally or abruptly (as a result of the method BufferedReader. readLine throwing an IOException ).
The try-with-resources statement ensures that each resource is closed at the end of the statement execution. If we don't close the resources, it may constitute a resource leak and also the program could exhaust the resources available to it. You can pass any object as a resource that implements java.
I guess all the resources I opened will be closed/released when the application/Virtual machine exits.
What happens with a resource which was not regurarly released is out of your control. It may do no harm, or it may do some. It is also highly platform-dependent, so testing on just one won't help.
why should I care about closing these resources in such small, short working application?
The size of the application shouldn't matter. First, applications usually grow; second, if you don't practice doing it the right way, you won't know how to do it when it matters.
If you not close the resources ,that may 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
According to docs
The typical Java application manipulates several types of resources such as files,streams, sockets,and database connections.Such resources must be handled with great care, because they acquire system resources for their operations.Thus, you need to ensure that they get freed even in case of errors.
Indeed, 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.
try-with-resources Statement introduced in java 7 for the programmers who hates close statements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With