I studied the following. Object header consists of a mark word and a klass pointer. In the case of an array, it additionally has size header.
In other words, there is no size information in the header of object data. So how does the jvm know how far to read from the start address when reading object data?
CodePudding user response:
The difference between an array and a "normal" object is that arrays can have different sizes, while every object of a given class has the same size. In a way, the size of non-array objects is stored, but not with every object, but only once in the respective class data (hence the class pointer).
Regarding accessing the data in an array/object, there is another difference: When accessing the element of an array, the JVM has to do a bounds check, which requires knowing the size of the array. When accessing the field of a normal object, however, this is not the case, as the number/positions of all fields is fixed (and known at compile time) for every object of a given class. So usually, knowing the size of an object is not actually required to access its data. However, I want to relativize Sweeper's comment: The JVM does need to know the size of non-array objects, e.g., when moving them around as part of garbage collection compaction.
CodePudding user response:
The JVM can determine the size of an object based on the object's class and the layout of the object's fields in memory. When an object is created, the JVM allocates memory for the object's header (mark word and klass pointer) and its fields. The JVM uses the object's class to determine the size of the header and the size and types of the fields.
Let's consider an array of "Page" objects, where each "Page" object has two fields, "text" and "images". The JVM would allocate memory for the array header (mark word, klass pointer, and size header) and the "Page" objects. The mark word, klass pointer, and size header make up the array header, and the "Page" objects are stored immediately after the header.
When the JVM needs to read the data for a specific "Page" object in the array, it starts at the start address of the array and first reads the mark word, klass pointer, and size header from the array header. The size header contains information about the size of the array, which the JVM uses to determine the number of "Page" objects in the array.
Next, the JVM uses the klass pointer to determine the layout of the "Page" objects in memory. The JVM knows that the first field of each "Page" object is the "text" field, which is a string, so it reads the necessary number of bytes to retrieve the value of the "text" field for the specific "Page" object it is interested in. Next, the JVM knows that the next field is the "images" field, which is also a string, so it reads the necessary number of bytes to retrieve the value of the "images" field for the same "Page" object.
By using the klass pointer to determine the layout of the fields in memory, the JVM is able to efficiently and accurately read the data for specific "Page" objects in the array, even if the array contains a large number of "Page" objects. This process is performed every time the JVM needs to access the data for an object in an array, making it an important part of the JVM's memory management system.