简体   繁体   English

java中的资源是什么?为什么我们必须在使用后关闭它?

[英]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? 在java中“resource”这个词是什么意思? Why we have to close it after usage,even though garbage collector runs in jvm ? 为什么我们必须在使用后关闭它,即使垃圾收集器在jvm中运行? Why we have to write resource cleanup code in finally block? 为什么我们必须在finally块中编写资源清理代码?

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. GC释放内存,但您仍然必须释放数据库连接,打开文件等资源,以允许其他线程使用它们。

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. 顺便说一句,最好在完成资源使用后立即释放资源,而不仅仅是在finalize方法中,这可能需要很长时间才能被GC调用。

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. 您运行的操作系统只允许这么多线程 - 每个线程1 MB开销。 You're limited by the available RAM. 你受限于可用的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? 如果客户端gc是连接,是什么告诉服务器关闭连接? 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. 如果您未能在finally块中关闭,您很快就会发现数据库服务器在高负载下将耗尽连接。

Finalize is not the right way to go. 敲定不是正确的方法。 Don't depend on the VM to call it. 不要依赖VM来调用它。 Write a close() method and call it in a finally block when your method is done with the resource. 编写close()方法,并在使用资源完成方法时在finally块中调用它。 Close in the narrowest scope possible. 尽可能在最窄的范围内关闭。

Say you have a file, you can write to it and not close the resource and eventually it will be closed by the GC. 假设您有一个文件,您可以写入它而不关闭资源,最终它将被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. 问题是,当文件打开时,你不能在Windows中删除它,在Linux中你可以删除它,但它不会释放任何空间。 If you want to delete a file you don't want to be waiting until the GC feels like running perhaps hours later. 如果你想删除一个文件,你不想等到GC感觉就好像几小时后才能运行。

What is the meaning of the word "resource" in java? 在java中“resource”这个词是什么意思?

The typical Java application manipulates several types of resources such as files, streams, sockets, and database connections. 典型的Java应用程序操纵几种类型的资源,例如文件,流,套接字和数据库连接。

Why we have to write resource cleanup code in finally block? 为什么我们必须在finally块中编写资源清理代码?

The Oracle article presents the Java 7 answer to the automatic resource management problem. Oracle文章介绍了Java 7对自动资源管理问题的回答。

  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 使用Java 7 try-with-resources语句

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM