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

    <tfoot id='vUlcO'></tfoot>

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

      1. String.intern() 的垃圾收集行为

        Garbage collection behaviour for String.intern()(String.intern() 的垃圾收集行为)
            <bdo id='LEthr'></bdo><ul id='LEthr'></ul>
              <tbody id='LEthr'></tbody>
              <tfoot id='LEthr'></tfoot>
              <i id='LEthr'><tr id='LEthr'><dt id='LEthr'><q id='LEthr'><span id='LEthr'><b id='LEthr'><form id='LEthr'><ins id='LEthr'></ins><ul id='LEthr'></ul><sub id='LEthr'></sub></form><legend id='LEthr'></legend><bdo id='LEthr'><pre id='LEthr'><center id='LEthr'></center></pre></bdo></b><th id='LEthr'></th></span></q></dt></tr></i><div id='LEthr'><tfoot id='LEthr'></tfoot><dl id='LEthr'><fieldset id='LEthr'></fieldset></dl></div>
              <legend id='LEthr'><style id='LEthr'><dir id='LEthr'><q id='LEthr'></q></dir></style></legend>

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

                  本文介绍了String.intern() 的垃圾收集行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果我使用 String.intern() 来提高性能,因为我可以使用=="来比较实习字符串,我会遇到垃圾收集问题吗?interned 字符串的垃圾回收机制与普通字符串有何不同?

                  If I use String.intern() to improve performance as I can use "==" to compare interned string, will I run into garbage collection issues? How does the garbage collection mechanism of interned strings differ from normal strings ?

                  推荐答案

                  其实这不是垃圾回收优化,而是字符串池优化.当您调用 String.intern(),你用它的基本引用替换你初始字符串的引用(第一次遇到这个字符串的引用,或者这个引用,如果它还不知道的话).

                  In fact, this not a garbage collection optimisation, but rather a string pool optimization. When you call String.intern(), you replace reference to your initial String with its base reference (the reference of the first time this string was encountered, or this reference if it is not yet known).

                  但是,一旦您的字符串在应用程序中不再使用,它将成为垃圾收集器问题,因为实习字符串池是 String 类的静态成员,永远不会被垃圾收集.

                  However, it will become a garbage collector issue once your string is of no more use in application, since the interned string pool is a static member of the String class and will never be garbage collected.

                  根据经验,我认为最好从不使用这种实习方法,让编译器只将它用于常量字符串,声明如下:

                  As a rule of thumb, i consider preferrable to never use this intern method and let the compiler use it only for constants Strings, those declared like this :

                  String myString = "a constant that will be interned";
                  

                  这更好,因为它不会让您做出错误的假设 == 可以在它不起作用时起作用.

                  This is better, in the sense it won't let you do the false assumption == could work when it won't.

                  此外,事实上 String.equals 在底层调用 == 作为优化,确保在后台使用内部字符串优化.这是 == 不应该永远用于字符串的又一证据.

                  Besides, the fact is String.equals underlyingly calls == as an optimisation, making it sure interned strings optimization are used under the hood. This is one more evidence == should never be used on Strings.

                  这篇关于String.intern() 的垃圾收集行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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='K0q35'></small><noframes id='K0q35'>

                      • <bdo id='K0q35'></bdo><ul id='K0q35'></ul>

                          <tfoot id='K0q35'></tfoot>

                            <tbody id='K0q35'></tbody>

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