Is closing the resources always important?

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:

  1. I have a small application
  2. It opens a few resources (e.g. files, database connections, remote streams) and processes it
  3. It works a few minutes and then it exits
  4. Let's say it's in Java (if the language is relevant)

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?


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.


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.

Need Your Help

DownloaderClientMarshaller.Proxy override superclass functions

java android eclipse

In the google generated code, that comes with the google play APK expansion i get the following error:

onSomeEventListener crash

android android-fragments

I have set up an application with a main activity and 2 fragment activities. I use a onsomeevent to pass data from the fragment to the main activity. The first fragment works fine and passes data. ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.