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

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

  • <tfoot id='lNywi'></tfoot>

        在 Java 中对变量使用 final 会改善垃圾收集吗?

        Does using final for variables in Java improve garbage collection?(在 Java 中对变量使用 final 会改善垃圾收集吗?)

            <legend id='TcSqT'><style id='TcSqT'><dir id='TcSqT'><q id='TcSqT'></q></dir></style></legend>

          1. <small id='TcSqT'></small><noframes id='TcSqT'>

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

                  问题描述

                  今天我和我的同事讨论了在 Java 中使用 final 关键字来改进垃圾收集.

                  Today my colleagues and me have a discussion about the usage of the final keyword in Java to improve the garbage collection.

                  例如,如果您编写一个方法,例如:

                  For example, if you write a method like:

                  public Double doCalc(final Double value)
                  {
                     final Double maxWeight = 1000.0;
                     final Double totalWeight = maxWeight * value;
                     return totalWeight;  
                  }
                  

                  在方法final中声明变量将有助于垃圾回收在方法退出后从方法中未使用的变量中清理内存.

                  Declaring the variables in the method final would help the garbage collection to clean up the memory from the unused variables in the method after the method exits.

                  这是真的吗?

                  推荐答案

                  这里有一个稍微不同的例子,一个具有最终引用类型字段而不是最终值类型局部变量的示例:

                  Here's a slightly different example, one with final reference-type fields rather than final value-type local variables:

                  public class MyClass {
                  
                     public final MyOtherObject obj;
                  
                  }
                  

                  每次创建 MyClass 实例时,都会创建对 MyOtherObject 实例的传出引用,并且 GC 必须通过该链接查找活动对象.

                  Every time you create an instance of MyClass, you'll be creating an outgoing reference to a MyOtherObject instance, and the GC will have to follow that link to look for live objects.

                  JVM 使用标记扫描 GC 算法,该算法必须检查 GC根"位置中的所有活动引用(就像当前调用堆栈中的所有对象一样).每个活动对象都被标记"为活动对象,活动对象引用的任何对象也被标记为活动对象.

                  The JVM uses a mark-sweep GC algorithm, which has to examine all the live refereces in the GC "root" locations (like all the objects in the current call stack). Each live object is "marked" as being alive, and any object referred to by a live object is also marked as being alive.

                  标记阶段完成后,GC 扫过堆,为所有未标记的对象释放内存(并为剩余的活动对象压缩内存).

                  After the completion of the mark phase, the GC sweeps through the heap, freeing memory for all unmarked objects (and compacting the memory for the remaining live objects).

                  此外,重要的是要认识到 Java 堆内存被划分为年轻一代"和老一代".所有对象最初都在年轻代(有时称为托儿所")中分配.由于大多数对象都是短暂的,GC 更积极地从年轻代中释放最近的垃圾.如果一个对象在年轻代的收集周期中存活下来,它就会被移入老年代(有时称为老年代"),而老年代的处理频率较低.

                  Also, it's important to recognize that the Java heap memory is partitioned into a "young generation" and an "old generation". All objects are initially allocated in the young generation (sometimes referred to as "the nursery"). Since most objects are short-lived, the GC is more aggressive about freeing recent garbage from the young generation. If an object survives a collection cycle of the young generation, it gets moved into the old generation (sometimes referred to as the "tenured generation"), which is processed less frequently.

                  所以,在我的脑海中,我要说不,'final' 修饰符不会帮助 GC 减少其工作量".

                  So, off the top of my head, I'm going to say "no, the 'final' modifer doesn't help the GC reduce its workload".

                  在我看来,在 Java 中优化内存管理的最佳策略是尽快消除虚假引用.您可以在使用完对象引用后立即将null"分配给它.

                  In my opinion, the best strategy for optimizing your memory-management in Java is to eliminate spurious references as quickly as possible. You could do that by assigning "null" to an object reference as soon as you're done using it.

                  或者,更好的是,最小化每个声明范围的大小.例如,如果您在 1000 行方法的开头声明一个对象,并且如果该对象在该方法的范围关闭(最后一个闭合花括号)之前保持活动状态,那么该对象可能会比实际保持活动更长时间必要的.

                  Or, better yet, minimize the size of each declaration scope. For example, if you declare an object at the beginning of a 1000-line method, and if the object stays alive until the close of that method's scope (the last closing curly brace), then the object might stay alive for much longer that actually necessary.

                  如果您使用只有十几行代码的小方法,那么在该方法中声明的对象将更快地超出范围,并且 GC 将能够在以下范围内完成大部分工作-更高效的年轻一代.除非绝对必要,否则您不希望将对象移入老一代.

                  If you use small methods, with only a dozen or so lines of code, then the objects declared within that method will fall out of scope more quickly, and the GC will be able to do most of its work within the much-more-efficient young generation. You don't want objects being moved into the older generation unless absolutely necessary.

                  这篇关于在 Java 中对变量使用 final 会改善垃圾收集吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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='2pwHL'><style id='2pwHL'><dir id='2pwHL'><q id='2pwHL'></q></dir></style></legend>
                • <small id='2pwHL'></small><noframes id='2pwHL'>

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

                      <tbody id='2pwHL'></tbody>

                    <tfoot id='2pwHL'></tfoot>

                          <bdo id='2pwHL'></bdo><ul id='2pwHL'></ul>