Object-Oriented Programming (Basics) 3: Memory Analysis of Objects

Table of contents

3.1 JVM memory structure division

 3.2 Object Memory Analysis

Example:

Memory analysis diagram:

Interview question: What is stored in the object name?

3.3 Exercises


3.1 JVM memory structure division

The architecture diagram of the HotSpot Java virtual machine is as follows. Among them, our main concern is the runtime data area (Runtime Data Area).

in:

堆(Heap): The sole purpose of this memory area is to store object instances, and almost all object instances allocate memory here. This is described in the Java Virtual Machine Specification: All object instances and arrays must be allocated on the heap.

栈(Stack): Refers to the virtual machine stack. The virtual machine stack is used to store local variables, etc. The local variable table stores various basic data types (boolean, byte, char, short, int, float, long, double) and object reference (reference type, which is not equivalent to the object itself, but the object in the heap first address of memory). After the method is executed, it is automatically released.

方法区(Method Area): It is used to store data such as class information loaded by the virtual machine, constants, static variables, code compiled by the just-in-time compiler, etc.

 3.2 Object Memory Analysis

Example:

class Person { //类:人
    String name;
    int age;
    boolean isMale;
}

public class PersonTest { //测试类
    public static void main(String[] args) {
        Person p1 = new Person();
        p1.name = "赵同学";
        p1.age = 20;
        p1.isMale = true;

        Person p2 = new Person();
        p2.age = 10;

        Person p3 = p1;
        p3.name = "郭同学";
    }
}

Memory analysis diagram:

说明:

堆:凡是new出来的结构(对象、数组)都放在堆空间中。
对象的属性存放在堆空间中。
创建一个类的多个对象(比如p1、p2),则每个对象都拥有当前类的一套"副本"(即属性)。当通过一个对象修改其属性时,不会影响其它对象此属性的值。
当声明一个新的变量使用现有的对象进行赋值时(比如p3 = p1),此时并没有在堆空间中创建新的对象。而是两个变量共同指向了堆空间中同一个对象。当通过一个对象修改属性时,会影响另外一个对象对此属性的调用。

Interview question: What is stored in the object name?

Answer: object address

public class StudentTest{
    public static void main(String[] args){
        System.out.println(new Student());//Student@7852e922

        Student stu = new Student();
        System.out.println(stu);//Student@4e25154f
        
        int[] arr = new int[5];
		System.out.println(arr);//[I@70dea4e
    }
}

Directly printing the object name and array name both display the "type@object hashCode value", so classes and arrays are all reference data types, and the variables of the reference data type store the address of the object, or point to the beginning of the object in the heap. address.

3.3 Exercises

According to the code, draw a memory map

class Car {
    String color = "red";
    int num = 4;

    void show() {
        System.out.println("color=" + color + ",num=" + num);
    }
}

class CarTest {
    public static void main(String[] args) {
        Car c1 = new Car();   //建立对象c1
        Car c2 = new Car();   //建立对象c2
        c1.color = "blue";   //对对象的属性进行修改
        c1.show();   //使用对象的方法
        c2.show();
    }
}

Guess you like

Origin blog.csdn.net/swx595182208/article/details/129950822