简体   繁体   English

对象如何在内存中存储-Java(C ++)

[英]How objects are stored in memory - Java (c++)

I have a memory question regarding creating objects in java-language. 我有一个关于在Java语言中创建对象的内存问题。

When one creates an object in java a certain memory-space is occupied on the heap - correct? 当在Java中创建一个对象时,堆上会占用一定的内存空间-对吗? But what is part of this memory? 但是这个记忆是什么呢? Is it membervariables or both membervariables and methods? 是成员变量还是成员变量和方法? Could it be so that several object that refers to the same class share something? 可能是几个引用同一类的对象共享某些东西吗?

Let say I have the following class 假设我有以下课程

 class MyClass {

     //Membervariables
     protected int age;
     protected long dateOfBirth;


    /**
     * Constructor  
     * @param age
     */
    public MyClass(int age) {
        this.age = age;
    }


    /**
     * 
     * @param dateOfBirth
     */
    protected void setDob(long dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }


    /**
     * 
     * @return
     */
    protected int getAge() {   
        return this.age;
    }


    /**
     * 
     * @return
     */
    protected long getDob() {
        return this.dateOfBirth;
    }
  }

So in this class I have the following: - Two membervariables - one int and one long 因此,在该类中,我具有以下内容:-两个成员变量-一个int和一个long

  • one constructor 一个构造函数

  • one mutator-method 一种变法

  • one accessor-method. 一种访问方法。

In the main-method I create two-instances (objects) of this class: 在主要方法中,我创建此类的两个实例(对象):

 MyClass myClass1 = new MyClass(10);
 MyClass myClass2 = new MyClass(11);

So my question is: 所以我的问题是:

1) How could this be depicted in the computers memory? 1)如何在计算机内存中进行描述?

2) Could the same "architecture" of how things are stored in memory concerning objects be applied to c++ as well? 2)是否可以将与对象有关的事物存储在内存中的相同“架构”也应用于c ++?

Thanks in advance!!! 提前致谢!!!

An object consists of a pointer to the internal version of the class, an area used for synchronized locking, an area of GC flags, and the instance fields defined in the class. 对象由指向类的内部版本的指针,用于synchronized锁定的区域,GC标志的区域以及在类中定义的实例字段组成。 (Note that an object reference field is only a pointer -- the referenced object is allocated separately.) (请注意,对象引用字段只是一个指针-被引用的对象是单独分配的。)

Methods are located by following the class pointer to the class and indexing a method table in the internal class object. 通过跟随指向类的类指针并在内部类对象中索引方法表来定位方法。

Your object above, in a 64-bit JVM, would be roughly: 在64位JVM中,您上面的对象大致为:

  • Class pointer - 8 bytes 类指针-8个字节
  • Lock variable - 8 bytes 锁定变量-8个字节
  • GC flags - 4 bytes GC标志-4个字节
  • Hash value - 4 bytes 哈希值-4个字节
  • dateOfBirth - 8 bytes dateOfBirth-8个字节
  • age - 4 bytes 年龄-4个字节

36 bytes total. 总共36个字节。 (Less, of course, in a 32-bit JVM.) The 36 bytes would likely be rounded up to 48 bytes to put it on a 16-byte boundary, or at least 40 to put it on an 8-byte boundary. (当然,在32位JVM中较少。)这36个字节可能会四舍五入为48个字节,以将其置于16字节边界,或者至少为40个以将其置于8字节边界。

(This is one possible implementation. There are tricks that can be used to reduce/combine the space for the lock and GC fields, eg. But the basic principles apply.) (这是一种可能的实现。可以使用一些技巧来减少/合并用于lock和GC字段的空间,例如,但是基本原理适用。)

对象数据在堆上,类数据和成员函数共享

The Java language spec doesn't specify heap layout. Java语言规范未指定堆布局。 In practice, most Java objects are going to have at least a "vtable pointer" (terminology questionable) which points to a table of method vectors (possibly method pointers, but also quite possibly a series of 'jump' instructions) and other run-time type information, and then data members. 实际上,大多数Java对象将至少具有一个“ vtable指针”(术语可疑),该指针指向方法向量表(可能是方法指针,但也很有可能是一系列的“跳转”指令)和其他运行-时间类型信息,然后是数据成员。 So with your example, the heap layout of the object might look like: 因此,以您的示例为例,对象的堆布局可能类似于:

(4 bytes) vtable pointer (points to MyClass vtable)
(4 bytes) age
(8 bytes) date of birth

For any two objects of the same class, the vtable pointer should have the same value. 对于同一类的任何两个对象,vtable指针应具有相同的值。 So, the table of method vectors, and the methods themselves, are each present only once in the address space. 因此,方法向量表以及方法本身在地址空间中仅存在一次。

Please bear in mind that is just a (simplified) example and implementations of Java need not adhere to this. 请记住,这只是一个(简化的)示例,Java的实现不必遵循此示例。

Regarding the second part of your question, C++ uses the same techniques; 关于问题的第二部分,C ++使用相同的技术。 but again, heap layout isn't specified completely in the language spec. 但是同样,语言规范没有完全指定堆布局。 Of course objects in C++ need not necessarily be stored on the heap (and the same is true in Java when certain optimizations come into play), but the memory layout of an object is generally not dependent on where it is allocated. 当然,C ++中的对象不必一定存储在堆上(当某些优化起作用时,在Java中也是如此),但是对象的内存布局通常并不依赖于其分配位置。

When one creates an object in java a certain memory-space is occupied on the heap - correct? 当在Java中创建一个对象时,堆上会占用一定的内存空间-对吗?

Probably, but the JVM can place it on the stack or optimise it away completely. 可能是,但是JVM可以将其放在堆栈上或完全对其进行优化。

But what is part of this memory? 但是这个记忆是什么呢?

If you have a generational collector, small objects are placed in the Eden space and large objects are placed in tenured space. 如果您有分代收集器,则将小对象放置在伊甸园空间中,将大对象放置在使用权空间中。

Is it membervariables or both membervariables and methods? 是成员变量还是成员变量和方法?

Just variables. 只是变量。 The object has a link to the class which holds all the loaded methods. 该对象具有指向包含所有已加载方法的类的链接。

Could it be so that several object that refers to the same class share something? 可能是几个引用同一类的对象共享某些东西吗?

Yes, of course. 当然是。 But this is not a requirement, just a more optimal solution. 但这不是必需的,只是一个最佳的解决方案。

1) How could this be depicted in the computers memory? 1)如何在计算机内存中进行描述?

A 64-bit JVM has a 16-bytes header which include the hashCode and a reference to the class. 64位JVM具有16字节的标头,其中包含hashCode和对该类的引用。 This is followed by the fields. 接下来是字段。 To improve memory access, the wider fields appear together to reduce the amount of padding needed. 为了改善内存访问,较宽的字段会一起出现,以减少所需的填充量。

2) Could the same "architecture" of how things are stored in memory concerning objects be applied to c++ as well? 2)是否可以将与对象有关的事物存储在内存中的相同“架构”也应用于c ++?

For C++ object with virtual methods, they will have a link to the vtable also. 对于具有虚拟方法的C ++对象,它们还将具有指向vtable的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM