Deep understanding of Java Virtual Machine Reading Notes (2) Creation of HotSpot Java objects, memory layout and access methods, javahotspot

Source: Internet
Author: User

Deep understanding of Java Virtual Machine Reading Notes (2) Creation of HotSpot Java objects, memory layout and access methods, javahotspot

In-Memory Object creation, object structure, and access method.

I. Object Creation

At the language level, object creation is just a new Keyword. What is the process in a virtual machine?

(1) determine whether a class is loaded. When the VM encounters a new command, it first checks whether the parameter of this command can locate a Class symbol reference in the constant pool, check whether the class represented by this symbol is loaded, parsed, and initialized. If the process is not completed, the corresponding class must be loaded.

(2) Allocate space for objects on the heap. The size of the space required by the object can be determined after the class is loaded. A fixed size of space is allocated to the object on the heap. There are two allocation methods:

I. first, if the collector with Compact processes such as Serial and ParNew is used, the Java memory heap is regular, you only need to move the pointer as a distance between the used and unused space.

Ii. second, if the Java memory is not regular when using the CMS-based Mark-Sweep algorithm-based collector, the virtual machine must maintain a list to record the memory usage, this method is called "Idle list.

Virtual machines frequently allocate space to objects. If multiple threads allocate objects at the same time, concurrency security control is involved. There are generally two solutions:

(1) The first method is to synchronize the actions in the allocated memory space-use CAS with retry failures to ensure the atomicity of the update operation.

(2) The second approach is to allocate the memory allocation actions in different spaces. Each thread allocates a small block of memory in advance in the Java heap, it is called the Local Thread Allocation Buffer (TLAB ). The TLAB of the thread where the memory is to be allocated is allocated. Synchronization locks are required only when TLAB is used and a new TLAB needs to be assigned.

(3) initializing memory space. After the memory allocation is complete, the virtual opportunity will initialize the zero value (excluding the object header) in the allocated space. If you use TLAB for allocation, this process can also be done in advance when TLAB is allocated.

(4) set object Headers. Next, the virtual machine needs to set the object header. Including the object's hash code, class Element Information, GC generational age, and so on. The information is stored in the object header.

(5) execute <init>MethodInitialize the members in the object.

After these five steps are completed, an object is actually generated.

Ii. Object Memory Layout

In memory, the OSS layout can be divided into three parts:Object Header(Header ),Sample Data(Instance Data) andAlignment Filling(Padding ).

I.Object Header: Contains two parts. The first part is used to store the data in the running hours of the object, such as the hash code, GC generational age, lock status, thread holding lock, and so on. The length of this part of data is 32 or 64-bit, officially referred to as "Mark Word ". Another part of the object header is the type pointer, that is, the pointer to the class element of the object. This pointer is used to determine the instance of the class when the object is located. (If a Java object is an array, the object header must also have a piece of data used to record the length of the array. Because the Java array metadata does not have an array size record)

Ii.Instance data: This part is actually used to store valid object information.

Iii.Alignment Filling: This part is not mandatory, but serves as a placeholder. Because the HotSpot VM requires that the starting address of an object must be a multiple of 8 bytes.

Iii. Object Access methods

We can use reference data on the stack to operate specific objects on the stack. There are two ways to access a specific object: a handle and a direct pointer.

Handle: A handle pool is defined in the Java heap to store the instance address and type address of the object. The reference in the stack is only the address of a handle in the handle pool. The advantage is that after garbage collection and moving, the object address changes and the reference data does not need to be changed.

Direct pointer: The reference directly points to the address of an object. The advantage is that the speed is fast, saving a time overhead for positioning.


Understanding of objects in java:

Three variables a, B, and c are three objects.
The most common method to create objects is to use the new keyword. Example: String str = new String ();
For a String, you can create an object that is unique to you. It contains text in quotation marks. Example: String str = "abc ";
Here I want to add some knowledge to help the landlord:
A String pool exists in the Java Virtual Machine (JVM), which stores many String objects and can be shared. Therefore, it improves the efficiency. Because the String class is final, its value cannot be changed once it is created, so we don't have to worry about the confusion caused by String object sharing. The String pool is maintained by the String class. We can call the intern () method to access the String pool.
String str = "abc"; when this line of code is executed, the Java VM first checks whether such an object with a value of "abc" already exists in the String pool, it is determined based on the return value of the String equals (Object obj) method. If yes, no new object is created and a reference to an existing object is directly returned. If no, this object is created first and then added to the string pool, then return its reference.
So String str1 = "abc"; String str2 = "abc ";
The two lines of code actually only create a String object. This is one of my questions during the interview.
Follow this knowledge point to learn the String class. If you have any questions, contact us again.
The above is my answer. I hope you can learn from each other ~

Java memory problems

In fact, you just want to know something about the Java Virtual Machine mechanism. For details, refer to wenku.baidu.com/..c.html#go to-degree Java virtual machine.

The question you asked is only a few points in the jvm introduction, involving the concepts of constant pools, heap memory, and stack memory. Refer to this manual for details. In particular, we need to distinguish what is a variable declaration, what is a variable creation, and what is a variable reference. Then we will understand the special situations. Such as int I = 127. this 127 is actually in the constant pool, when you use integer a = new Integer ("127 "). integer B = newinteger ("127 "). they point to the same address, but when you create new Integer ("128"), two objects are created, but they are all in the constant pool, these concepts are included in the details of jvm virtual machines.

The second and third problems are actually very clear comparisons. The second problem is to create an array object in the heap memory with an array object address, which contains two int constants, that is, 0, but actually all point to the same constant pool address, that is, 0.
The third problem is different. The address of the person array object, but the length is not specified for it, should not be compiled. To create an array, you must mark the subscript of the leftmost index. Even if your problem is Person [] p = new Person [2. In fact, an array object is created, pointing to an array address. Then, the array object stores two null values without allocating memory space and addresses.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.