<small id='3ya3m'></small><noframes id='3ya3m'>

        <i id='3ya3m'><tr id='3ya3m'><dt id='3ya3m'><q id='3ya3m'><span id='3ya3m'><b id='3ya3m'><form id='3ya3m'><ins id='3ya3m'></ins><ul id='3ya3m'></ul><sub id='3ya3m'></sub></form><legend id='3ya3m'></legend><bdo id='3ya3m'><pre id='3ya3m'><center id='3ya3m'></center></pre></bdo></b><th id='3ya3m'></th></span></q></dt></tr></i><div id='3ya3m'><tfoot id='3ya3m'></tfoot><dl id='3ya3m'><fieldset id='3ya3m'></fieldset></dl></div>

        <legend id='3ya3m'><style id='3ya3m'><dir id='3ya3m'><q id='3ya3m'></q></dir></style></legend>
        • <bdo id='3ya3m'></bdo><ul id='3ya3m'></ul>
        <tfoot id='3ya3m'></tfoot>

      1. 将流类或连接视为 Java 中的资源泄漏

        Will stream classes or connections considered as a resource leak in Java(将流类或连接视为 Java 中的资源泄漏)

        1. <legend id='1gYAZ'><style id='1gYAZ'><dir id='1gYAZ'><q id='1gYAZ'></q></dir></style></legend>
          • <bdo id='1gYAZ'></bdo><ul id='1gYAZ'></ul>

            <small id='1gYAZ'></small><noframes id='1gYAZ'>

            <tfoot id='1gYAZ'></tfoot>
              <tbody id='1gYAZ'></tbody>
            • <i id='1gYAZ'><tr id='1gYAZ'><dt id='1gYAZ'><q id='1gYAZ'><span id='1gYAZ'><b id='1gYAZ'><form id='1gYAZ'><ins id='1gYAZ'></ins><ul id='1gYAZ'></ul><sub id='1gYAZ'></sub></form><legend id='1gYAZ'></legend><bdo id='1gYAZ'><pre id='1gYAZ'><center id='1gYAZ'></center></pre></bdo></b><th id='1gYAZ'></th></span></q></dt></tr></i><div id='1gYAZ'><tfoot id='1gYAZ'></tfoot><dl id='1gYAZ'><fieldset id='1gYAZ'></fieldset></dl></div>

                  本文介绍了将流类或连接视为 Java 中的资源泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Java 没有对象的生命周期,这由 garbage collector 管理.如果我使用一些 IO 类而不关闭它,或者一些 DBConnection,这会被认为是资源泄漏吗?换句话说,IO对象是否会被垃圾收集器AFAIK收集和销毁,垃圾收集器仅用于内存.例如:

                  Java has no lifetime for an object, this is managed by the garbage collector. And if I use some IO classes without closing it, or some DBConnection, will this considered a resource leak? In another words, will IO object be collected and destroyed by garbage collector, AFAIK, the garbage collector is for memory only. For example:

                  BufferedReader br = new BufferedReader( new FileReader( new File("path") ) );
                  

                  推荐答案

                  是的,你是对的.

                  垃圾收集释放 Java 堆(内存),但 close() 释放用于打开文件的操作系统资源(大多数系统上打开文件的数量有限)并确保数据确实被写入.

                  Garbage collection frees Java heap (memory) but close() frees OS resources used for open file (number of open files is limited on most system) and assures that the data is really written.

                  但是许多类,例如 FileInputStreamRandomAccessFile 都是用 finalize() 方法编写的,该方法确保如果垃圾回收中的实例,将首先调用 close().所以在很多情况下,垃圾回收确实会间接释放文件,而且程序员经常可能会懒于关闭资源,因为垃圾回收通常会为你清理它们.很遗憾.

                  But many classes such as FileInputStream and RandomAccessFile are written with a finalize() method which ensures that IF an instance in garbage collected, close() will be called first. So in many cases, garbage collection does indirectly free up files, and it is often possible for programmers to be lazy about closing resources, because garbage collection usually cleans them up for you. Unfortunately.

                  问题是您无法控制何时发生这种情况,而且它可能根本不会发生.因此,如果您打开了太多文件,操作系统可能会在垃圾收集器开始关闭它们之前给您一个错误.或者,如果您想移动或删除文件,请在读取文件后立即 - 移动或删除可能会失败,因为此时您仍然可以打开文件以供阅读.

                  The problem is that you can't control when this happens, and it may not happen at all. So if you have too many files open, the operating system may give you an error about that before the garbage collector gets around to closing them. Or if you want to move a file or delete it, immediately after reading it - the move or delete may fail, because at that moment you've still got the file open for reading.

                  这样的错误通常很难可靠地重现,因为它们取决于垃圾收集器的时间.所以你得到的东西通常可以正常工作,但有时会神秘地失败.调试起来很烦.出于这个原因,强烈建议您在完成后立即关闭()您可能正在使用的任何流/读取器/连接或其他可关闭资源.最好在 finally 块中,以确保即使在处理过程中发生一些其他错误也会发生.

                  Errors like this are often hard to reproduce reliably, because they depend on the timing of the garbage collector. So you get things which usually work fine, but sometimes fail mysteriously. Very annoying to debug. For this reason, it is stronly recommended to be sure to close() any stream/reader/connection or other closable resource you may be using, as soon as you are done with it. Preferably in a finally block, to ensure it happens even if some other error occurs in processing.

                  在 Java 7 中,添加了 AutoClosable 接口,了解更多信息这里.

                  And with Java 7, there is an addition of AutoClosable interface, read more about it here.

                  参考:http://www.coderanch.com/t/278165//java/InputStream-close-garbage-collection

                  这篇关于将流类或连接视为 Java 中的资源泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Maven JAVA_HOME environment variable is not defined correctly, but it is(Maven JAVA_HOME 环境变量未正确定义,但它是)
                  Java System Environment Variable(Java 系统环境变量)
                  Java -classpath option(Java -classpath 选项)
                  Read environment variable in SpringBoot(在 SpringBoot 中读取环境变量)
                  Tomcat 8 - context.xml use Environment Variable in Datasource(Tomcat 8 - context.xml 在数据源中使用环境变量)
                  Issue with JAVA_HOME(JAVA_HOME 的问题)

                        <small id='uYBps'></small><noframes id='uYBps'>

                          <tbody id='uYBps'></tbody>

                      1. <tfoot id='uYBps'></tfoot>

                        1. <i id='uYBps'><tr id='uYBps'><dt id='uYBps'><q id='uYBps'><span id='uYBps'><b id='uYBps'><form id='uYBps'><ins id='uYBps'></ins><ul id='uYBps'></ul><sub id='uYBps'></sub></form><legend id='uYBps'></legend><bdo id='uYBps'><pre id='uYBps'><center id='uYBps'></center></pre></bdo></b><th id='uYBps'></th></span></q></dt></tr></i><div id='uYBps'><tfoot id='uYBps'></tfoot><dl id='uYBps'><fieldset id='uYBps'></fieldset></dl></div>
                            <bdo id='uYBps'></bdo><ul id='uYBps'></ul>
                            <legend id='uYBps'><style id='uYBps'><dir id='uYBps'><q id='uYBps'></q></dir></style></legend>