Deep analysis of object creation and use in Java


1. How does JVM manage memory?

  The following figure shows the memory management diagram of the java virtual machine:

Insert picture description here

1. Program Counter

  • Concept: Can be seen as a line number indicator of the bytecode executed by the current thread
  • Features: thread private memory

2. Java virtual machine stack

  • Concept: Describes the memory model of java method execution. (When each method is executed, a stack frame is created to store the local variable table, operation stack frame, dynamic link, method exit and other information. The process of each method from calling to completion corresponds to a stack frame from entering Stack to pop process)
  • Features: The thread is private, and the life cycle is the same as that of the thread. There are two exceptions in this area: StackOverflowError exception-if the depth of the thread request is greater than the depth allowed by the virtual machine. OutOfMemoryError-If the virtual machine can be dynamically expanded, if the Berserker cannot apply for enough memory.

3. Local method stack

  • Concept: It is similar to the role played by the virtual machine stack. The difference is that the java virtual machine stack serves to execute java methods , while the local method stack serves local methods .
  • Features: The thread is private, and two different StackOverflowError and OutOfMemoryError are also thrown.

4. Stack area

  • Concept: is an area shared by all threads, created when the virtual machine starts
  • Features: Thread sharing, storing object instances (all object instances and arrays), the main area of ​​GC management. It can be in physically discontinuous memory space.

5. Method area

  • Concept: Store data such as class information, constants, static variables that have been loaded by the virtual machine, and code compiled by the just-in-time compiler.
  • Features: The thread shared area, throws an exception OutOfMemory exception-when the method area cannot meet the memory allocation requirements.

  For beginners, the three main memory spaces of the Java virtual machine that we must know are "virtual machine stack" (referred to as stack), "method area", and "heap area".

  • Method area :Storage class information
  • Stack :Store the stack frame and local variables when the method is executed
  • Heap :Mainly store new objects and instance variables inside the objects

The method area has data first, because the class is loaded before the program is executed. The stack memory activity is the most cumbersome, because the method is continuously executed and ended, and the stack is pushed and popped continuously.
Insert picture description here

2. Briefly describe the memory changes of the JVM virtual machine when the object is created

Code:

public class StudentTest {
    
    
    public static void main(String[] args) {
    
    
        int i=10;
        Student s1=new Student();
    }
}

Student code:

class Student{
    
    
    int no;
    String name;
    int age;
    boolean sex;
}

The memory changes during the execution of the above code are as follows:

  1. The first step is to load the class
    Insert picture description here
  2. The second step is to call the main method to allocate a stack frame (push the stack) to the main method
    Insert picture description here
  3. The third step is to execute int i=10
    Insert picture description here
  4. The fourth step is to execute new student() to create objects in the heap and initialize instance variables.
    Insert picture description here
  5. The fifth step is to assign the memory address of the student object in the heap area to the local variable s1.
    Insert picture description hereNote : The
      i variable and the s1 variable in the figure are local variables, both in the stack memory, except that i is the basic data type int and s1 is the reference data type Student.
      For the object s1 created in the heap, the internal attributes no, name, age, and sex of the object are all instance variables. These variables are initialized when the object is new. If they are not manually assigned, the system will assign them by default.
    About the understanding of "reference" :
      After the "object" in the heap area in the above figure is created, the memory address of the object in the heap area is: 0x1111, the = in the program represents, assign the address of the heap memory 0x1111 to the s1 variable, In other words, the s1 variable holds the memory address of the heap memory object.s1 is not an object, but a reference to an object, The object is actually in the heap area, s1 holds the memory address of this object.
      There is no concept of pointers in java, so java programmers have no right to directly manipulate heap memory, and can only access objects in heap memory through "references".

to sum up

  Through the above explanation, we should know that local variables are stored in the stack area , and instance variables are stored in the heap area . The initialization of instance variables is to initialize and assign values ​​while creating objects. Java accesses the instance variables of the object through the object name and attribute .

Guess you like

Origin blog.csdn.net/m0_46988935/article/details/110144235