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/

相关文章:

Java 8 Stream List<Foo> 到 Map<Date, Map<String,Long>> 与条件 groupingBy

java - 如果节点不属于同一个 AST,如何复制它们

java - 如何动态更改警报文本?

java - 如何在将 blob 上传到 Blobstore 的 POST 请求中提交文本字段,并在 blob 的上传处理程序中检索它?

c# - Java 等价于带有 @ 的 C# 逐字字符串

python - 以 10 为基数的 int() 的文字无效 : 'i' : jump program

ruby - 在 ruby​​ 中增加二进制有效载荷

java - 如何在java中使用TCP/IP将相同的数据从服务器发送到所有客户端?

c++ - 在 C++ 的构造函数中将字符串文字分配给成员 const char* 变量时会发生什么?

c++ - '\u0B95' 是多字 rune 字吗?