Why do I need to call a close() or shutdown() method?

a 夏天 提交于 2019-12-05 08:49:45

Most classes will clean up their external resources in their finalize methods, but this isn't analogous to C++'s destructor - a destructor is called as soon as an object is no longer used, but a finalizer isn't called until an object is garbage-collected, and you don't know when this is going to happen (and it might never happen if you don't have a memory-intensive program). So let's say you're allocating a bunch of objects that are each allocating a database connection (yes, you should be using connection pooling, but this is a hypothetical); you use each object and then null out its references so that it can be garbage-collected without first closing their database connections, and now your database is going to crap out on you because it's got too many connections open. You can call System.gc in the hopes that this will clean up the connections, but this is only a suggestion to the garbage-collector that it perform a collection, and it's possible that your garbage database connections' finalizers aren't smart enough to fully clean up the connections.

Long story short, you need to call close and shutdown because you don't know if/when your objects' finalizers are going to run, and you don't know how good they are at cleaning up external resources.

Incidentally, you should use try-catch-finally blocks to make sure you call close and shutdown - put the methods in the finally block. Or if you're using Java7, use try-with-resources.

the java garbage collector would do the work for you, BUT in some cases (with sockets or database connections) you need to call the close method (for example to close a connection with a database, or a file handler etc.).

There's people that calls the close() method of a connection and then turn it into null like:

conn.close();
conn = null;

Then they shoot it in the head to make it clear that it's dead.

Java's garbage collection automatically frees memory that's no longer referenced.

It doesn't free other resources. Consequently, you need an explicit method to free other resources.

Ok, the concrete question would be why do I need to call to .close or .shutdown methods?

To free resources that are not disposed of automatically.

I understand the purpose of existence, to unlock the file, but why do I have to remember to close it?

If you use Java 7's try-with-resources, you don't have to.

Finally, is it a proper workaround to wrap the class that needs to be closed or shutdown with a class that implements the finalize method and there I call the close or shutdown method?

No. You cannot rely on the finalize() method being called soon or at all after an object is no longer referenced.

Many classes exist to encapsulate the services of outside entities. In many cases, the usefulness of a class will depend upon its asking an outside entity to do something on its behalf until further notice, to the potential detriment of other prospective users of that entity. A class which asks an outside entity to act on its behalf until further notice, takes upon itself the responsibility of notifying the outside entity when its services are no longer needed.

Calling close() will tell an object that its services are no longer required; the (no-longer-needed) object can thus pass the messages along to entities that are acting on its behalf that their services won't be needed either. Such notice may then allow those entities to make their services available to other prospective users.

Garbage-collection operates on the presumption that the system knows when it needs memory, and there isn't any real benefit to getting rid of garbage until such time as the system would have use for memory that could be freed up. Such a philosophy doesn't really work with most things that use close. In many cases, code which holds a resource will have no way of knowing whether anyone else might want to use it. Consequently, code should generally try to hold resources only for the duration that they will actually be used. Once code is done with a resource, it should release it so as to make it available to anyone else who might want it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!