gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:52:51 27 4
gpt4 key购买 nike

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 -> PermGen -> InternedStrings 中,如 JVM 文档所说,或在某些私有(private)集合中的 String 类中,如 String API 所说?

最佳答案

在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com