Java 1.7 String.intern() 不是 JVM 中的 InternedStrings 或者我错了

标签 java literals

Returns a canonical representation for the string object. A pool of strings, initially empty, is maintained privately by the class String. When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned. It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

这意味着文字被缓存在其私有(private)池中的 String 类中,但 JVM 架构对 InternedStrings 进行了说明:

Interned Strings (String Table) The Java Language Specification requires that identical string literals, that contain the same sequence of Unicode code points, must refer to the same instance of String. In addition if String.intern() is called on an instance of String a reference must be returned that would be identical to the reference return if the string was a literal. The following therefore holds true: ("j" + "v" + "m").intern() == "jvm" In the Hotspot JVM interned string are held in the string table, which is a Hashtable mapping object pointers to symbols (i.e. Hashtable), and is held in the permanent generation. For both the symbol table (see above) and the string table all entries are held in a canonicalized form to improve efficiency and ensure each entry only appears once. String literals are automatically interned by the compiler and added into the symbol table when the class is loaded. In addition instances of the String class can be explicitly interned by calling String.intern(). When String.intern() is called, if the symbol table already contains the string then a reference to this is returned, if not the string is added to the string table and its reference is returned.

我的问题是文字/字符串到底存储在哪里 - 正如 JVM 文档所述,在 JVM -> PermGen -> InternedStrings 中,还是如 String API 所说,在某些私有(private)集合中的 String 类中?

最佳答案

在 1.6 及以下版本中,它是 permgen。从 7 开始,它进入堆,我知道 7 中的一个点发布版本对其工作方式进行了一些重大更改。

编辑

找到这个http://java-performance.info/string-intern-in-java-6-7-8/ 7u40 发生了重大变化。

编辑 编辑

http://docs.oracle.com/javase/7/docs/technotes/guides/vm/enhancements-7.html

Synopsis: In JDK 7, interned strings are no longer allocated in
the permanent generation of the Java heap, but are instead
allocated in the main part of the Java heap (known as the young
and old generations), along with the other objects created by the
application. This change will result in more data residing in the
main Java heap, and lessw data in the permanent generation, and
thus may require heap sizes to be adjusted. Most applications
will see only relatively small differences in heap usage due to
this change, but larger applications that load many classes or
make heavy use of the String.intern() method will see more
significant differences.  RFE: 6962931

关于Java 1.7 String.intern() 不是 JVM 中的 InternedStrings 或者我错了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30035605/

相关文章:

scala - scala 中的函数和函数文字

c - 声明复杂 C 结构化常量数据的策略?

c - C 整数文字如何工作?

c - 为什么-32768 在下面的平台上有 signed long 类型?

c - strcpy 与 memcpy 用于复制已知大小的 char *

java - 从java代码调用网站的javascript函数?

java - 使用下载管理器下载时显示进度条

java - FacesMessage Jsf 中的新行字符

java - 有没有一种直接的方法来关闭 java 中的整数位?

java - 设置 Cloud9 IDE 来编译和运行 Java?