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

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

      <legend id='BKvQl'><style id='BKvQl'><dir id='BKvQl'><q id='BKvQl'></q></dir></style></legend>
      • <bdo id='BKvQl'></bdo><ul id='BKvQl'></ul>

        局部变量上的垃圾收集

        Garbage collection on a local variable(局部变量上的垃圾收集)
          <tbody id='yY9sA'></tbody>

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

                <bdo id='yY9sA'></bdo><ul id='yY9sA'></ul>
                • <legend id='yY9sA'><style id='yY9sA'><dir id='yY9sA'><q id='yY9sA'></q></dir></style></legend>

                • <tfoot id='yY9sA'></tfoot>
                • <i id='yY9sA'><tr id='yY9sA'><dt id='yY9sA'><q id='yY9sA'><span id='yY9sA'><b id='yY9sA'><form id='yY9sA'><ins id='yY9sA'></ins><ul id='yY9sA'></ul><sub id='yY9sA'></sub></form><legend id='yY9sA'></legend><bdo id='yY9sA'><pre id='yY9sA'><center id='yY9sA'></center></pre></bdo></b><th id='yY9sA'></th></span></q></dt></tr></i><div id='yY9sA'><tfoot id='yY9sA'></tfoot><dl id='yY9sA'><fieldset id='yY9sA'></fieldset></dl></div>
                  本文介绍了局部变量上的垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是一名进入 Java 世界的 C++ 程序员.而且我无法摆脱不得不让 Java 垃圾收集器进行清理的糟糕感觉.

                  I'm a C++ programmer entering the world of Java. And I cannot get rid of the bad feeling of having to let the Java garbage collector do my cleaning.

                  例如,这段代码在 Java 中的表现如何?

                  How, for example, will this code behave in Java?

                  public void myFunction() {
                      myObject object = new myObject();
                      object.doSomething();
                  }
                  

                  myFunction()退出时会不会删除局部变量对象?

                  Will the local variable object be deleted when myFunction() exits?

                  我必须在退出之前将 object 设置为 null,还是会超出范围并被 GC 删除?或者,在最坏的情况下,它会像在 C++ 中那样泄漏吗?

                  Do I have to set object to null before exiting, or will it be out of scope and be deleted by the GC? Or, at worst, will it leak like it would in C++?

                  推荐答案

                  它会在不再使用后的某个时间被垃圾回收.我相信在 Java 的当前实现中,它实际上会一直持续到方法结束,而 .NET 中的垃圾收集器更具侵略性.(我不知道即使在 Java 中是否也有任何保证.通常,您只希望局部变量在调试时持续超过其最后一次可能的读取.)

                  It will be garbage collected at some point after it's no longer used. I believe in current implementations of Java it will actually persist until the end of the method, whereas the garbage collector in .NET is more aggressive. (I don't know whether there are any guarantees even in Java. Normally you'd only want the local variable to persist beyond its last possible read when you're debugging.)

                  但是不,您不需要将变量设置为 null,这样做会损害可读性.

                  But no, you don't need to set the variable to null, and doing so would harm readability.

                  在方法退出后立即对象不太可能被垃圾回收;这取决于 GC 运行的时间......当然,如果有其他任何东西保留对对象的引用,那么它可能无论如何都没有资格进行垃圾收集.不要忘记变量的值只是一个引用,而不是对象本身.(这可能需要一段时间才能习惯来自 C++.)

                  It's unlikely that the object will garbage collected immediately after the method exits; it's up to when the GC runs... and of course if anything else holds onto a reference to the object, it may not be eligible for garbage collection anyway. Don't forget that the value of the variable is just a reference, not the object itself. (That may take a while to get used to coming from C++.)

                  这篇关于局部变量上的垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 的问题)
                  <legend id='SPVeI'><style id='SPVeI'><dir id='SPVeI'><q id='SPVeI'></q></dir></style></legend>
                    <tfoot id='SPVeI'></tfoot>

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

                          <tbody id='SPVeI'></tbody>

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

                            <bdo id='SPVeI'></bdo><ul id='SPVeI'></ul>