Some extended concepts about runtime memory data area

Top-of-Stack Cashing

As mentioned earlier, the zero-address instructions used by the virtual machine based on the stack architecture are more compact, but it is necessary to use more push and pop instructions to complete an operation, which also means that more instructions will be required. The number of instruction dispatch and the number of memory read/write operations are stored in the memory, so frequent execution of memory read/write operations will inevitably affect the execution speed. In order to solve this problem, the designers of Hotspot JVM proposed the top-of-stack cache (ToS, Top-of-Stack Cashing) technology, which caches all the top elements of the stack in the registers of the physical CPU, thereby reducing the read/write to memory times to improve the execution efficiency of the execution engine

method call

Static linking and dynamic linking

In the JVM, the conversion of symbolic references to direct references to invoke methods is related to the method's binding mechanism.

Static link:

When a bytecode file is loaded into the JVM, if the called target method is known at compile time and remains unchanged at runtime . In this case, the process of converting the symbolic reference of the calling method into a direct reference is called static linking.

Dynamic link:

If the called method cannot be determined at compile time, that is to say, the symbolic reference of the calling method can only be converted into a direct reference at the runtime of the program . Since this reference conversion process is dynamic, it is also called for dynamic linking.

Early Binding vs Late Binding

The binding mechanism of the corresponding method is: early binding (Early Binding) and late binding (Late Binding) . Binding is a process where a symbolic reference to a field, method, or class is replaced by a direct reference, which happens only once.

Early binding:

Early binding means that if the called target method is known at compile time and remains unchanged at runtime, the method can be bound to the type to which it belongs. In this way, since the called target method is clear Which one is competing, so you can use static linking to convert symbolic references into direct references.

Late binding:

If the called method cannot be determined at compile time, the related method can only be bound according to the actual type at program runtime. This binding method is also called late binding.

Virtual and non-virtual methods

With the emergence of high-level languages, there are more and more object-oriented programming languages ​​similar to Java. Although these programming languages ​​have certain differences in grammatical style, they always maintain a commonality with each other. , that is, they all support object-oriented features such as encapsulation, inheritance, and polymorphism. Since this type of programming language has polymorphic features, it naturally has two binding methods: early binding and late binding.

Any ordinary method in Java actually has the characteristics of virtual functions, which are equivalent to virtual functions in the C++ language (in C++, the keyword virtual needs to be used to explicitly define). If you do not want a method to have the characteristics of a virtual function in a Java program, you can use the keyword final to mark this method.

Non-virtual method:

If the method determines the specific calling version at compile time, this version is immutable at runtime. Such methods are called non-virtual methods.

Static methods, private methods, final methods, instance constructors, and superclass methods are all non-virtual methods.

Other methods are called virtual methods.

The following method call instructions are provided in the virtual machine:

Ordinary call instruction:

  1. invokestatic: Invoke a static method, and the parsing phase determines the only method version
  2. invokespecial: call the <init> method, private and parent methods, and determine the only method version in the parsing phase
  3. invokevirtual: call all virtual methods
  4. invokeinterface: call interface method

Dynamic call instruction:

  1. invokedynamic: dynamically parse out the method that needs to be called, and then execute

The first four instructions are solidified inside the virtual machine, and the execution of the method call cannot be artificially interrupted, while the invokedynamic instruction supports the user to determine the method version. Among them, the method invoked by the invokestatic instruction and the invokespecial instruction is called a non-virtual method, and the rest (except those modified by final) are called virtual methods.

The JVM bytecode instruction set has always been relatively stable. It was not until Java7 that an invokedynamic instruction was added. This is an improvement made by Java to support "dynamic type language".

However, Java7 does not provide a method to directly generate invokedynamic instructions, and it is necessary to use ASM, a low-level bytecode tool, to generate invokedynamic instructions. Until the emergence of Java8's Lambda expression and the generation of invokedynamic instructions, there was no direct way to generate them in Java.

The essence of the dynamic language type support added in Java7 is the modification of the Java virtual machine specification, not the modification of the Java language rules. This part is relatively complicated, and the method call in the virtual machine is added. The most direct beneficiary It is a dynamic language compiler running on the Java platform.

Dynamically typed language and statically typed language

The difference between a dynamically typed language and a statically typed language lies in whether the type is checked at compile time or at runtime. If the former is satisfied, it is a statically typed language, and vice versa, it is a dynamically typed language.

To put it more bluntly, a static type language is to judge the type information of the variable itself: a dynamic type language is to judge the type information of the variable value, the variable has no type information, and the variable value has type information, which is an important feature of the dynamic language .

The essence of method overriding

The essence of method rewriting in the Java language:

  1. Find the actual type of the object executed by the first element at the top of the operand stack, denoted C.
  2. If a method that matches the description in the constant and the simple name is found in the type C, the access authority check is performed, and if it passes, the direct reference of this method is returned, and the search process ends; if not, it returns java.lang. IllegalAccessError exception.
  3. Otherwise, carry out the search and verification process of step 2 on each parent class of C in sequence from the bottom to the top according to the inheritance relationship.
  4. If no suitable method is found, a java.lang.AbstractMethodError exception is thrown.

IllegalAccessError introduction:

The program attempted to access or modify a property or call a method that you do not have permission to access. generally

Yes, this will cause a compiler exception. If this error occurs at runtime, it means that a class has an incompatible

Change.

virtual method table

In object-oriented programming, dynamic dispatch is frequently used. If you have to search for a suitable target in the method metadata of the class during each dynamic dispatch, it may affect the execution efficiency. Therefore, in order to improve performance, the JVM implements it by creating a virtual method table (virtual method table) in the method area of ​​the class (non-virtual methods will not appear in the table). Use indexed tables instead of lookups.

Each class has a virtual method table, which stores the actual entry of each method.

So when is the virtual method table created?

The virtual method table will be created and initialized during the linking phase of class loading. After the initial value of the variable of the class is prepared, the JVM will also initialize the method table of the class.

Guess you like

Origin blog.csdn.net/m0_73843666/article/details/130095240