[JVM] LXF Deep Understanding of JVM Theme (5-)

Links to the original text: https://blog.csdn.net/a724888/article/details/78404643

Articles Catalogue

Deep Understanding of JVM Virtual Machine 5: Virtual Machine Bytecode Execution Engine

Original address: https://blog.csdn.net/a724888/article/details/78404643

I. The Essence of Method Call

Let's look at a piece of C code:

void sayHello(int age)
    int x = 32;
    int y = 2323;
    age = x + y;
void main()
    int age = 22;

A very simple piece of code, we assemble and generate the corresponding assembly code, omitting part of the link code, leaving behind the core part:

	pushl	%ebp
	movl	%esp, %ebp
	subl	$20, %esp
	movl	$22, -4(%ebp)
	movl	-4(%ebp), %eax
	movl	%eax, (%esp)
	call	sayHello
	pushl	%ebp
	movl	%esp, %ebp
	subl	$16, %esp
	movl	$32, -4(%ebp)
	movl	$2323, -8(%ebp)
	movl	-8(%ebp), %eax
	movl	-4(%ebp), %edx
	addl	%edx, %eax
	movl	%eax, -12(%ebp)

The leave instruction is equivalent to the sum of the following two instructions:

movl %ebp %esp
popl %ebp

2. Runtime stack frame structure

1. Table of Local Variables

Local variable table is different from operand stack, it uses index mechanism to access elements, but different from the way of operand stack's entry and exit stack. For example:

public void sayHello(String name){
        int x = 23;
        int y = 43;
        x = y - 2;
        long z = 234;
        x = (int)z;
        String str = new String("hello wrold ");
2. Operator stack
3. Return address
4. Method calls

So which methods are static parsed and which methods need dynamic parsing?

For example, the following code:

Object obj = new String("hello");
5. Static assignment

First, let's look at a piece of code:

public class Father {
public class Son extends Father {
public class Daughter extends Father {
public class Hello {
    public void sayHello(Father father){
        System.out.println("hello , i am the father");
    public void sayHello(Daughter daughter){
        System.out.println("hello i am the daughter");
    public void sayHello(Son son){
        System.out.println("hello i am the son");
public static void main(String[] args){
    Father son = new Son();
    Father daughter = new Daughter();
    Hello hello = new Hello();
6. Dynamic allocation
public class Father {
    public void sayHello(){
        System.out.println("hello world ---- father");
public class Son extends Father {
    public void sayHello(){
        System.out.println("hello world ---- son");
public static void main(String[] args){
    Father son = new Son();
7. Support for Dynamic Type Properties
Object obj = new String("hello-world");

JDK 1.7 provides two ways to support Java's dynamic characteristics, invokedynamic instructions and the java.lang.invoke package. The implementation of the two methods is similar, we only introduce the basic content of the latter.

//This method is customized by me, not in the invoke package
public static MethodHandle getSubStringMethod(Object obj) throws NoSuchMethodException, IllegalAccessException {
    //A method template is defined, which specifies the return value and parameter type of the method to be searched.
    MethodType methodType = MethodType.methodType(String[].class,String.class);
    //Method to find simple names and template information that match the specified method
    return lookup().findVirtual(obj.getClass(),"split",methodType).bindTo(obj);
public static void main(String[] args){
    Object obj = new String("hello-world");
    //Location method and parameter execution method
    String[] strs = (String[]) getSubStringMethod(obj).invokeExact("-");

Deep Understanding of JVM Virtual Machine 6: Deep Understanding of JVM Class Loading Mechanism

Original address: https://blog.csdn.net/a724888/article/details/78396462

I. Timing of Class Loading

II. Class Loading Process

1. Loading
2. Verification
  1. File Format Verification
  2. Metadata validation
  3. Bytecode verification
  4. Symbol Reference Verification
3. Prepare

Assume that:

public static int value = 123;

A special case is that if the field property table contains the ConstantValue attribute, the value of the preparation phase variable is initialized to the value specified by the ConstantValue attribute, such as the value above if defined as:

public static final int value = 123;

Tags: jvm Java Attribute JDK

Posted on Tue, 13 Aug 2019 02:32:49 -0700 by MrAdam