Muthyala Naresh
Muthyala Naresh

Reputation: 83

What is a resource in java?why we have to close it after it is used?

What is the meaning of the word "resource" in java? Why we have to close it after usage,even though garbage collector runs in jvm ? Why we have to write resource cleanup code in finally block?

Upvotes: 5

Views: 3502

Answers (4)

Braj
Braj

Reputation: 46841

What is the meaning of the word "resource" in java?

The typical Java application manipulates several types of resources such as files, streams, sockets, and database connections.

Why we have to write resource cleanup code in finally block?

The Oracle article presents the Java 7 answer to the automatic resource management problem.

  1. 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.

  2. 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.

  3. 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.

Use Java 7 The try-with-resources Statement

Upvotes: 2

duffymo
duffymo

Reputation: 308753

A database connection, a thread, a file handle, a socket - all are finite resources.

The operating system you run on only allows so many threads - 1 MB overhead per thread. You're limited by the available RAM. Same for file handles and sockets.

Database connections are interesting because they involve client and server. If the client gc's the connection, what tells the server to close the connection? If you fail to close in finally blocks, you'll soon find out that your database server will run out of connections under heavy load.

Finalize is not the right way to go. Don't depend on the VM to call it. Write a close() method and call it in a finally block when your method is done with the resource. Close in the narrowest scope possible.

Upvotes: 8

Peter Lawrey
Peter Lawrey

Reputation: 533492

Say you have a file, you can write to it and not close the resource and eventually it will be closed by the GC. The problem is that while the file is open you can't delete it in windows, and in Linux you can delete it but it doesn't free any space. If you want to delete a file you don't want to be waiting until the GC feels like running perhaps hours later.

Upvotes: 5

Eran
Eran

Reputation: 393781

A resource is something that has a limited ammount, such as DB connections and file descriptors. The GC frees memory, but you still have to release resources such as DB connections, open files, etc..., to allow other threads to use them.

By the way, it's better to free the resources immediately after you finish using them, and not just in the finalize method, which might take a long time until it is called by the GC.

Upvotes: 11

Related Questions