Objects on the heap need a reference to their type descriptor (Tag) and information for the garbage collection.
The field size allows for a faster sweep phase during garbage collection. size includes the size of the object plus heap field and Tag.
If the object is an array the field size contains the length of the array (see Arrays).
When a new object is created there is a call to the method newObject of the class Heap. After this follows the call of the constructor of the super class. For the class java/lang/Object this call is missing in the Bytecode.
If an array is to be created things run differently. Each array is an extension of java/lang/Object. In the Bytecode the call to this constructor is missing. If java/lang/Object has fields which need to be initialized this would have to be done when allocating the array in the heap.
There are 4 different Bytecode instructions to create objects on the heap. The following table shows this instructions together with register usage. This usage is platform specific and must be fixed between the code generator and the Heap class of the runtime system.
Bytecode | Use | Heap Method | Parameter | Register | Return Value | Register |
---|---|---|---|---|---|---|
new | Object | newObject | Reference to type descriptor | 1st parameter register | Reference to Object | return register |
newarray | Array of base type | newPrimTypeArray | Nof | 1st parameter register | Reference to array | return register |
Java array type | 2nd parameter register | |||||
Reference to type descriptor | 3rd parameter register | |||||
anewarray | Array of objects | newRefArray | Nof | 1st parameter register | Reference to array | return register |
Reference to type descriptor | 2nd parameter register | |||||
multianewarray | Multidimensional array | newMultiDimArray | Type or reference to Type | 1st parameter register | Reference to array | return register |
Nof dimensions | 2nd parameter register | |||||
Dimension 1 | 3rd parameter register | |||||
Dimension 2 | 4th parameter register | |||||
etc. | ||||||
String | newstring | Reference to type | 1st parameter register | Reference to object | return register | |
Length | 2nd parameter register |
The class Heap is a system class. Its methods will be linked to the appropriate Bytecode instructions as indicated above.
The instruction newstring is used to create strings, see Strings.