老师想问一下
来源:6-11 Java内存模型之 常考题解析-1
慕粉13951610073
2019-09-28
public class Test {
public static final String a1 = "123";
public static SingleObject a2 = new SingleObject();
public static SingleObject getSingleObject() {
return a2;
}
}
老师想确认一下.
1."123"对象是存放在堆上面的。。a1引用地址是存放在元空间上面的吗?
2.SingleObject 对象也是存放在堆上面的,a2引用地址是存放在元空间上面的吗?
1回答
-
同学好,以下是官方定义,The proposed implementation will allocate class meta-data in native memory and move interned Strings and class statics to the Java heap. Hotspot will explicitly allocate and free the native memory for the class meta-data.
也就是说,静态成员变量的符号会被存储在java heap中。jdk8以后,字符串常量池和静态变量符号都存储在java堆中(field values are in Java Heap, the field metadata (names, types, offsets) is in Metaspace),所以123在堆(字符串常量池),静态变量的符号在元空间(a1 a2)
具体详见 http://openjdk.java.net/jeps/122
Description
部分
012019-09-29
相似问题