4 Object Memory Diagram

Create an object

Through drawing, we can see how objects are allocated in memory, which is more helpful for beginners to understand objects.

Student class

class Student{

    //Student ID
    int id; 

    //Full name
    String name;

    //Gender
    boolean sex;

    //Age
    int age;

    public void study() {                //Define learning methods
        System.out.println("Student learning");
    }

    public void love(String name) {                //Define the way to fall in love
        System.out.println("I am following." + name + "Fall in love");
    }

    public void takeExercises(String sport){
        System.out.println(sport);
    }
}

Student Test Category:

public class StudentTest01{

    public static void main(String[] args){

    Student s = new Student();
    s.name = "Zhang San";
    s.age = 20;
    s.sex = true;
    s.id = 1001;

    s.study();
    s.love("Zhao Liu");
    s.takeExercises("Play basketball");    


    }
}

There are three main areas in JVM memory:

Stack: Stores references to underlying data and custom objects
Heap: the main storage of the created object, that is, the new out of the object.
Method area: Load and store class files (bytecode files)

From the figure above, the program is executed as follows:

1 Load the StudentTest01.class file into the method area
2 Load the Student.class file into the method area
3 main method stack
4 create a Student object in the heap
s in stack 5 points to memory address 0x0101 of Student object in stack
6 assigning member variables to objects
7-way stack
8 Program Execution End

The main point explained here is the solid line in the graph, which represents the address of s pointing to the memory of Student object heap. S itself is not an object, but a pointing. For example, the room number 1024, 1024 in the hotel itself is not a room, but points to the room number, so that the accommodation staff can quickly find the room.

Create two objects

public class StudentTest01{

    public static void main(String[] args){

    Student s = new Student();
    s.name = "Zhang San";
    s.age = 20;
    s.sex = true;
    s.id = 1001;

    Student s1 = new Student();
    s1.id=1002;
    s1.name="Li Si";
    s1.age=21;
    s1.sex=false;


    }
}


Three points to two objects

public class StudentTest01{

    public static void main(String[] args){

    Student s = new Student();
    s.name = "Zhang San";
    s.age = 20;
    s.sex = true;
    s.id = 1001;

    Student s1 = new Student();
    s1.id=1002;
    s1.name="Li Si";
    s1.age=21;
    s1.sex=false;

    Student s2 = s1;

    }
}

The above code assigns s1 to s2, which is equivalent to s1 and S2 pointing to the same object.


Cancel direction

public class StudentTest01{

    public static void main(String[] args){

    Student s = new Student();
    s.name = "Zhang San";
    s.age = 20;
    s.sex = true;
    s.id = 1001;

    s = null;//Set s to null
    //s.study(); report NullPointerException (null pointer exception)


    }
}


If the object in the heap is not pointed at, there is a garbage collector in Java that recycles the object. There is no need to manually set it to null during development, and the garbage collector will automatically recycle it.

Tags: jvm Java

Posted on Sun, 06 Oct 2019 17:24:38 -0700 by cocpg