Java Hotspot VM如何生成字符串OOP和镜像OOP?

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); //

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