Related articles
Java Virtual Machine Series
Preface
In the previous article we learned about the structure of the Java Virtual machine and the runtime data area, so we probably know the memory of the Java Virtual machine, so how is the data in memory created and accessed? This article will give you an answer.
1. Creation of Objects
Objects are usually created by a new object, and when the virtual machine receives a new command, it does the following.
(1) determine whether the class of the object is loaded, linked, initialized
When a virtual machine receives a new command, it first checks to see if the specified parameter is able to locate the symbolic reference of a class in the constant pool, and checks whether the class that the symbol reference represents has been loaded, linked, and initialized by the ClassLoader. If not, the corresponding class loading process is performed first. About the ClassLoader we have already mentioned in the previous article that we will not repeat them here.
(2) allocating memory to Objects
After the class is loaded, a chunk of memory is then allocated to the object in the Java heap. Memory allocations are structured in terms of the Java heap, in two ways:
- Pointer collisions: If the memory of the Java heap is structured, that is, all the used memory is on one side and the idle is on the other side. When allocating memory, the pointer indicator in the middle moves to the free memory a distance that is equal to the object size, which completes the allocation of memory work.
- Idle list: If the Java heap's memory is not structured, a list of virtual machines needs to be maintained to record that memory is available, so that when allocated, you can query from the list to large enough memory to allocate to the object, and update the list records after the allocation.
The memory of the Java heap is structured in terms of whether the collector is compressed or not, about the garbage collector, which is described in the articles later in this series.
(3) Handling concurrency security issues
Creating objects is a very frequent operation, so there are two ways to solve the problem of concurrency:
- Synchronous processing of the actions of allocating memory space, such as the use of CAS algorithms in virtual machines and the provision of failed retries to ensure the atomicity of the update operation.
- Each thread allocates a small chunk of memory in the Java heap, known as the local thread allocation buffer (thread local Allocation buffer) abbreviated to Tlab, which allocates memory on the tlab of the corresponding thread when the thread needs to allocate memory. Synchronous locking is required when the Tlab in a thread is exhausted and assigned to a new Tlab. Use the-xx:+/-usertlab parameter to set whether the virtual machine uses Tlab.
(4) Initialize the allocated memory space
The memory to be allocated, in addition to the object header, is initialized to a value of 0.
(5) Object header for set object
Data such as the owning class of the object, the hashcode of the object, and the GC generational age of the object are stored in the object's header.
(6) Perform Init method initialization
Executes the Init method, initializes the object's member variables, invokes the constructor of the class, and the object is created.
2. Heap Memory layout for objects
object is created, and memory is already allocated in the Java heap, how is the object laid out in heap memory?
In the case of a hotspot virtual machine, the layout of the object in the heap memory is divided into three regions, namely the object header (header), the instance (Instance data), and the aligned padding (Padding).
- Object header: The object header consists of two pieces of information, mark World and metadata pointers, which mark World uses to store data when the object is running, such as hashcode, lock status Flag, GC generational age, and so on. The metadata pointer is used to point to the type information of the target class in the method area, and the metadata pointer determines the object's specific type.
- Instance data: Used to store various types of field information in an object, including inherited from a parent class.
- Align fills: the alignment fill does not have to exist, it acts as a placeholder and has no special meaning.
The memory layout of the object as shown in.
object model for 3.HotSpot
The Oop-klass model is used in hotspots as a model for describing instances of Java objects, and OOP (ordinary object Pointer) refers to ordinary object pointers, and Klass is used to describe the specific types of object instances.
In hotspot, Instanceoopdesc and Arrayoopdesc are used to describe the object header, where the Arrayoopdesc object is used to describe the array type.
The code for INSTANCEOOPDESC is shown below.
Openjdk/hotspot/src/share/vm/oops/instanceoop.hpp
Class Instanceoopdesc: PublicOopdesc { Public://aligned header size.Static int header_size() {return sizeof(INSTANCEOOPDESC)/heapwordsize; }//If compressed, the offset of the fields of the instance is aligned. Static intBase_offset_in_bytes () {//Offset computation code breaks if usecompressedclasspointers //Is True return(Usecompressedoops && usecompressedclasspointers)? Klass_gap_offset_in_bytes ():sizeof(INSTANCEOOPDESC); }Static BOOLContains_field_offset (intOffsetintNonstatic_field_size) {intBase_in_bytes = Base_offset_in_bytes ();return(Offset >= base_in_bytes && (offset-base_in_bytes) < nonstatic_field_size * Heapoopsize); }};
You can see that Instanceoopdesc inherits from Oopdesc:
Openjdk/hotspot/src/share/vm/oops/oop.hpp
class oopDesc { friend class VMStructs; private: volatile markOop _mark; union _metadata { Klass* _klass; narrowKlass _compressed_klass; } _metadata; // Fast access to barrier set. Must be initialized. static BarrierSet* _bs;...}
The
Oopdesc contains two data members: _mark and _metadata. The _mark object of the Markoop type refers to mark World, which was mentioned earlier. _metadata is a common body, where _klass is a normal pointer, and _compressed_klass is a compression class pointer, which is the metadata pointer mentioned earlier, both pointing to the Instanceklass object, which is used to describe the concrete type of the object. The code for the
Instanceklass is shown below.
openjdk/hotspot/src/share/vm/oops/instanceklass.hpp
class InstanceKlass: public Klass { ... enum ClassState { allocated, // allocated (but not yet linked) loaded, in class hierarchy (but not linked yet) linked, // successfully linked/verified (but not initialized yet) being_initialized, // currently running class initializer fully_initialized, // initialized (successfull final state) initialization_error // error happened during initialization }; ...
Instanceklass inherits from Klass, and enumeration classstate is used to identify the loading progress of an object.
Knowing the Oop-klass model, we can analyze how the Java virtual machine finds the corresponding object instance through the object reference in the stack frame, as shown in.
As you can see, the Instanceoopdesc object in the Java heap is found through the object reference in the stack frame, and the metadata pointer in Instanceoopdesc finds the Instanceklass in the method area to determine the specific type of the object.
Resources
"In-depth understanding of Java virtual machines"
"Java Virtual machine explaining"
Explore the JVM in depth | Research on Klass-oop object model
Java object creation process for JVM source code Analysis
Java class loading process for JVM source code Analysis
Welcome to my public number, the first time to get blog update reminders, and more into the system of Android-related original technology dry.
Sweep the two-dimensional code below or long press identification QR code, you can pay attention to.
Java Virtual Machine (ii) creation and Oop-klass model of objects