Java Hotspot VM如何生成字符串OOP和镜像OOP?
在openjdk8源代码中,我发现一些java.lang.String oop并没有经过字节码引擎并由jvm本身进行分配。正如hotspot/src/share/vm/classfile/javaClasses.cpp:185所示:Java Hotspot VM如何生成字符串OOP和镜像OOP?,java,jvm,hotspot,Java,Jvm,Hotspot,在openjdk8源代码中,我发现一些java.lang.String oop并没有经过字节码引擎并由jvm本身进行分配。正如hotspot/src/share/vm/classfile/javaClasses.cpp:185所示: Handle java_lang_String::create_from_unicode(jchar* unicode, int length, TRAPS) { Handle h_obj = basic_create(length, CHECK_NH); //
Handle java_lang_String::create_from_unicode(jchar* unicode, int length, TRAPS) {
Handle h_obj = basic_create(length, CHECK_NH); // alloc size of java.lang.String oop
typeArrayOop buffer = value(h_obj());
for (int index = 0; index < length; index++) {
buffer->char_at_put(index, unicode[index]); // put a char[] into this oop...
}
return h_obj;
}
oop java_lang_Class::create_mirror(KlassHandle k, Handle protection_domain, TRAPS) {
...
Handle mirror = InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->allocate_instance(k, CHECK_0);
...
// if `k` holds a InstanceKlass, it will initialize the static fields by constant value attribute. else do nothing...
}
我对此感到非常困惑。仅alloc
内存,如果它是java.lang.String
的对象,则在其中放入char[]
;但是java.lang.String
和java.lang.Class
中的其他字段何时填充到oop中?谢谢。分配字符串
对象并初始化其值
字段:
obj = InstanceKlass::cast(SystemDictionary::String_klass())->allocate_instance(CHECK_NH);
// Create the char array. The String object must be handlized here
// because GC can happen as a result of the allocation attempt.
Handle h_obj(THREAD, obj);
typeArrayOop buffer;
buffer = oopFactory::new_charArray(length, CHECK_NH);
// Point the String at the char array
obj = h_obj();
set_value(obj, buffer); <<<--- char[] value is set here
// No need to zero the offset, allocation zero'ed the entire String object
assert(offset(obj) == 0, "initial String offset should be zero");
//set_offset(obj, 0);
set_count(obj, length);
obj=InstanceKlass::cast(SystemDictionary::String_klass())->allocate_instance(CHECK_NH);
//创建char数组。必须在此处处理字符串对象
//因为GC可能是分配尝试的结果。
手柄h_obj(螺纹,obj);
类型数组操作缓冲区;
buffer=oopFactory::新字符(长度,检查字符);
//将字符串指向char数组
obj=h_obj();
设置_值(obj,缓冲器) 分配字符串
对象并初始化其值
字段:
obj = InstanceKlass::cast(SystemDictionary::String_klass())->allocate_instance(CHECK_NH);
// Create the char array. The String object must be handlized here
// because GC can happen as a result of the allocation attempt.
Handle h_obj(THREAD, obj);
typeArrayOop buffer;
buffer = oopFactory::new_charArray(length, CHECK_NH);
// Point the String at the char array
obj = h_obj();
set_value(obj, buffer); <<<--- char[] value is set here
// No need to zero the offset, allocation zero'ed the entire String object
assert(offset(obj) == 0, "initial String offset should be zero");
//set_offset(obj, 0);
set_count(obj, length);
obj=InstanceKlass::cast(SystemDictionary::String_klass())->allocate_instance(CHECK_NH);
//创建char数组。必须在此处处理字符串对象
//因为GC可能是分配尝试的结果。
手柄h_obj(螺纹,obj);
类型数组操作缓冲区;
buffer=oopFactory::新字符(长度,检查字符);
//将字符串指向char数组
obj=h_obj();
设置_值(obj,缓冲器);从Java8开始,只有两个实例字段,其他字段是static
;char[]value
是唯一需要初始化的字段,因为hashCode
在hashCode()
方法中是惰性初始化的。谢谢!我完全理解它。在Java8中,只有两个实例字段,其他字段是static
;char[]value
是唯一需要初始化的字段,因为hashCode
在hashCode()
方法中是惰性初始化的。谢谢!我完全理解。谢谢!我发现java.lang.Class
只有一个简单的构造函数java.lang.String
有一个char[]
构造函数和StringTable::intern
在其中注入了TypeArrayOop
。谢谢!我发现java.lang.Class
只有一个简单的构造函数java.lang.String
有一个char[]
构造函数和StringTable::intern
在其中注入了TypeArrayOop
。