Redian新闻
>
string pool vs class constant pool问题,在线等
avatar
OE
2
想知道JVM内部到底是如何实现string pool的。
string pool里存的到底是string objects还是string references?
string pool和class constant pool的关系是什么样的?
public class hello1{
public static String s="Hello!"
}
public class hello2{
public static void main (String[] args){
String s = "Hello!";
System.out.println(s==hello1.s);//this is true
}
}
if you check the constant pool in .class files, you can see both classes
have "#n = Utf8 Hello!" in their constant pool. 但我猜,当JVM load这两个
classes,它们各自的runtime constant pool把"Hello1"存成了string reference, 这
两个string references都指向了同一个string object. 这个string object可能就在
string pool里(permgen上);也有可能还是在heap里,但是它的一个reference 存在
了string pool里(weak hashmap)。
avatar
t*a
3
如果你看编译好的bytecode, 两处应该都是LDC "Hello!", LDC都是生成一个string
refere
nce, 然后再把这个reference存储给static field或者local variable. 所以如果这么
看结果应该是false
但是jvm对于相同string constant在不同class files这种情况,有个优化,它只
create一个instance, 所以其实两个reference指向了同一个Instance,结果就变成了
true

【在 OE 的大作中提到】
: 想知道JVM内部到底是如何实现string pool的。
: string pool里存的到底是string objects还是string references?
: string pool和class constant pool的关系是什么样的?
: public class hello1{
: public static String s="Hello!"
: }
: public class hello2{
: public static void main (String[] args){
: String s = "Hello!";
: System.out.println(s==hello1.s);//this is true

avatar
OE
4
ldc是这样解释的:
Pushing a String causes a reference to a java.lang.String object to be
constructed and pushed onto the operand stack.
astore是这样解释的:
Pops objectref (a reference to an object or array) off the stack and stores
it in local variable
根据这篇文章http://mindprod.com/jgloss/interned.html#UNDERTHEHOOD
我是这样理解的:
那两个string ref.在ldc时就是相同的。与“Hello!”相对应的string object在class
loading的时候就生成了,而且会有一个weak string ref.(指向这个string object
)被放到一个hashmap里。这个hashmap有时就被称为string pool。当第二个class被
load
,或则说它的runtime constant pool 被生成时,JVM会去hashmap里check “Hello!”
,这时开始的那个string object's ref会被found and return.所以如果用
javap -c -v xxx.class
you will see something like
ldc #2.
this #2 is the 2nd item in the runtime constant pool, which is just the ref.
pointing to the only string object.
ldc, astore等等都是要等到真正运行时才work。这时,string ref. 已经确定了。
看完这篇文章,我觉得string pool和class constant pool是根本不一样的。但是
class constant pool里的constant_string_table是和生成string pool相关的。 而且
,在create the 与“Hello!”相对应的string object后,runtime constant pool有
一个指向它的ref., 在hashmap里还有一个weak ref.指向它.

【在 t***a 的大作中提到】
: 如果你看编译好的bytecode, 两处应该都是LDC "Hello!", LDC都是生成一个string
: refere
: nce, 然后再把这个reference存储给static field或者local variable. 所以如果这么
: 看结果应该是false
: 但是jvm对于相同string constant在不同class files这种情况,有个优化,它只
: create一个instance, 所以其实两个reference指向了同一个Instance,结果就变成了
: true

avatar
t*a
5
我就是看了你的帖子,和你程序的结果,自己瞎联想的
constant pool(是叫这个名字么)是per class的,而且是runtime的,但是这个string
的instance肯定是整个jvm来share的,只有一个,那么constant pool里应该只存ref,
而且还是strong ref(它不能让这个instance被gc)
string pool是存soft ref的,它是所有classes共享的,如果所有class里的strong
ref都被回收了,那么string pool里面引用的这个string instance也会被回收的
这么看来。。。的确是不同的。。。。楼主为啥要比较他们两个呢?
avatar
t*a
6
这东西做面试题不错。。。。。

stores
class
object

【在 OE 的大作中提到】
: ldc是这样解释的:
: Pushing a String causes a reference to a java.lang.String object to be
: constructed and pushed onto the operand stack.
: astore是这样解释的:
: Pops objectref (a reference to an object or array) off the stack and stores
: it in local variable
: 根据这篇文章http://mindprod.com/jgloss/interned.html#UNDERTHEHOOD
: 我是这样理解的:
: 那两个string ref.在ldc时就是相同的。与“Hello!”相对应的string object在class
: loading的时候就生成了,而且会有一个weak string ref.(指向这个string object

avatar
OE
7
原因之一就是很多人管string pool叫string constant pool.

string

【在 t***a 的大作中提到】
: 我就是看了你的帖子,和你程序的结果,自己瞎联想的
: constant pool(是叫这个名字么)是per class的,而且是runtime的,但是这个string
: 的instance肯定是整个jvm来share的,只有一个,那么constant pool里应该只存ref,
: 而且还是strong ref(它不能让这个instance被gc)
: string pool是存soft ref的,它是所有classes共享的,如果所有class里的strong
: ref都被回收了,那么string pool里面引用的这个string instance也会被回收的
: 这么看来。。。的确是不同的。。。。楼主为啥要比较他们两个呢?

avatar
OE
8
想知道JVM内部到底是如何实现string pool的。
string pool里存的到底是string objects还是string references?
string pool和class constant pool的关系是什么样的?
public class hello1{
public static String s="Hello!"
}
public class hello2{
public static void main (String[] args){
String s = "Hello!";
System.out.println(s==hello1.s);//this is true
}
}
if you check the constant pool in .class files, you can see both classes
have "#n = Utf8 Hello!" in their constant pool. 但我猜,当JVM load这两个
classes,它们各自的runtime constant pool把"Hello1"存成了string reference, 这
两个string references都指向了同一个string object. 这个string object可能就在
string pool里(permgen上);也有可能还是在heap里,但是它的一个reference 存在
了string pool里(weak hashmap)。
avatar
t*a
9
如果你看编译好的bytecode, 两处应该都是LDC "Hello!", LDC都是生成一个string
refere
nce, 然后再把这个reference存储给static field或者local variable. 所以如果这么
看结果应该是false
但是jvm对于相同string constant在不同class files这种情况,有个优化,它只
create一个instance, 所以其实两个reference指向了同一个Instance,结果就变成了
true

【在 OE 的大作中提到】
: 想知道JVM内部到底是如何实现string pool的。
: string pool里存的到底是string objects还是string references?
: string pool和class constant pool的关系是什么样的?
: public class hello1{
: public static String s="Hello!"
: }
: public class hello2{
: public static void main (String[] args){
: String s = "Hello!";
: System.out.println(s==hello1.s);//this is true

avatar
OE
10
ldc是这样解释的:
Pushing a String causes a reference to a java.lang.String object to be
constructed and pushed onto the operand stack.
astore是这样解释的:
Pops objectref (a reference to an object or array) off the stack and stores
it in local variable
根据这篇文章http://mindprod.com/jgloss/interned.html#UNDERTHEHOOD
我是这样理解的:
那两个string ref.在ldc时就是相同的。与“Hello!”相对应的string object在class
loading的时候就生成了,而且会有一个weak string ref.(指向这个string object
)被放到一个hashmap里。这个hashmap有时就被称为string pool。当第二个class被
load
,或则说它的runtime constant pool 被生成时,JVM会去hashmap里check “Hello!”
,这时开始的那个string object's ref会被found and return.所以如果用
javap -c -v xxx.class
you will see something like
ldc #2.
this #2 is the 2nd item in the runtime constant pool, which is just the ref.
pointing to the only string object.
ldc, astore等等都是要等到真正运行时才work。这时,string ref. 已经确定了。
看完这篇文章,我觉得string pool和class constant pool是根本不一样的。但是
class constant pool里的constant_string_table是和生成string pool相关的。 而且
,在create the 与“Hello!”相对应的string object后,runtime constant pool有
一个指向它的ref., 在hashmap里还有一个weak ref.指向它.

【在 t***a 的大作中提到】
: 如果你看编译好的bytecode, 两处应该都是LDC "Hello!", LDC都是生成一个string
: refere
: nce, 然后再把这个reference存储给static field或者local variable. 所以如果这么
: 看结果应该是false
: 但是jvm对于相同string constant在不同class files这种情况,有个优化,它只
: create一个instance, 所以其实两个reference指向了同一个Instance,结果就变成了
: true

avatar
t*a
11
我就是看了你的帖子,和你程序的结果,自己瞎联想的
constant pool(是叫这个名字么)是per class的,而且是runtime的,但是这个string
的instance肯定是整个jvm来share的,只有一个,那么constant pool里应该只存ref,
而且还是strong ref(它不能让这个instance被gc)
string pool是存soft ref的,它是所有classes共享的,如果所有class里的strong
ref都被回收了,那么string pool里面引用的这个string instance也会被回收的
这么看来。。。的确是不同的。。。。楼主为啥要比较他们两个呢?
avatar
t*a
12
这东西做面试题不错。。。。。

stores
class
object

【在 OE 的大作中提到】
: ldc是这样解释的:
: Pushing a String causes a reference to a java.lang.String object to be
: constructed and pushed onto the operand stack.
: astore是这样解释的:
: Pops objectref (a reference to an object or array) off the stack and stores
: it in local variable
: 根据这篇文章http://mindprod.com/jgloss/interned.html#UNDERTHEHOOD
: 我是这样理解的:
: 那两个string ref.在ldc时就是相同的。与“Hello!”相对应的string object在class
: loading的时候就生成了,而且会有一个weak string ref.(指向这个string object

avatar
OE
13
原因之一就是很多人管string pool叫string constant pool.

string

【在 t***a 的大作中提到】
: 我就是看了你的帖子,和你程序的结果,自己瞎联想的
: constant pool(是叫这个名字么)是per class的,而且是runtime的,但是这个string
: 的instance肯定是整个jvm来share的,只有一个,那么constant pool里应该只存ref,
: 而且还是strong ref(它不能让这个instance被gc)
: string pool是存soft ref的,它是所有classes共享的,如果所有class里的strong
: ref都被回收了,那么string pool里面引用的这个string instance也会被回收的
: 这么看来。。。的确是不同的。。。。楼主为啥要比较他们两个呢?

avatar
n*1
14
http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#d5e
Literal strings within the same class (§8) in the same package (§7)
represent references to the same String object (§4.3.1).
Literal strings within different classes in the same package represent
references to the same String object.
Literal strings within different classes in different packages likewise
represent references to the same String object.
Strings computed by constant expressions (§15.28) are computed at compile
time and then treated as if they were literals.
Strings computed by concatenation at run time are newly created and
therefore distinct.
The result of explicitly interning a computed string is the same string as
any pre-existing literal string with the same contents.
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。