Memory analysis

Stack space (stack)

  • Continuous storage space for local variables on a last-in, first-out basis.
  • Store the base variable type (containing the value of the base type)
  • The variable that refers to the object (which stores the address of the reference in the heap)

Heap space (heap)

  • Discontinuous space
  • Holds new objects and arrays
  • It can be shared by all threads and does not store other object references

Methods area (method)

  • The method area is in the heap space
  • Can be shared by all threads
  • Class code information
  • Static variables and methods
  • Constant pool

In Java, all but basic data types are reference types, including classes, arrays, and so on.

Default value of data type, basic data type Default value: numeric: 0 Floating-point: 0.0 Boolean: false Character: \u0000 Reference type: NULL

Variable initialization: The system automatically initializes member variables without initializing them. Local variables must be explicitly initialized by the programmer; the system does not initialize them automatically.

In simple terms, a complete Java program execution involves the following memory areas: 1. Program Counter Register, which allows the bytecode interpreter in the VIRTUAL machine to obtain the next code instruction by changing the value of the Counter, such as branch, loop, jump, exception handling, thread recovery, etc.

If Java Virtual Machine Stacks Stacks, the current method can be stored on the top of the stack.

3. Native Method Stacks, on the other hand, serve the Native methods used by virtual machines and function as virtual machine Stacks.

4. The Java Heap is the largest chunk of memory managed by the Java VIRTUAL machine. It is an area of memory shared by all threads. The sole purpose of this memory area is to hold object instances, and almost all object instances are allocated memory here.

The Method Area, like the Java heap, is an Area of memory shared by each thread. It is used to store data such as class information that has been loaded by the virtual machine, constants, static variables, and code compiled by the just-in-time compiler.

Example to analyze, create class:

public class Student {
    int score;
    int age;
    String name;
    Computer computer;

    public void study(a) {
        System.out.println("studying..."); }}Copy the code
public class Computer {
    int price;
    String brand;
}
Copy the code
public class Test {
    public static void main(String[] args) {
        Student stu = new Student();
        stu.name = "xiaoming";
        stu.age = 10;
        stu.study();
        Computer c = new Computer();
        c.brand = "Hasse";
        System.out.println(c.brand);
        stu.computer = c;
        System.out.println(stu.computer.brand);
}
Copy the code
Code analysis:

As we know, the entry point to the program is main(), so the main method is analyzed from top to bottom and left to right.

Student stu = new Student();

  1. First, the Java Virtual machine (JVM) goes to the methods section to see if there is code information for the Test class, and if so, calls it directly. If not, use the ClassLoader to load. Class bytecode into memory, and load static variables and methods, and constant pool (xiaoming,Hasse).

  2. Go to Student and load the Student class with the same logic; Static member; Constant pool (studying).

  3. Go to stu, which is inside main and therefore a local variable, stored in stack space.

  4. Go to New Student, the new object (instance) is stored in the heap space, using the class information of the method area as the template to create the instance.

  5. = assignment, which tells the stu variable the address of new Student, and stu references the instance with a four-byte address (in hexadecimal).

  6. Stu. Name = “xiaoming”; Stu references the name attribute of the new Student instance, which points to the “Xiaoming” constant of the constant pool by address.

  7. stu.age = 10; The age attribute of s instance is the basic data type, and the basic data type is directly assigned.

  8. stu.study(); When an instance’s method is called, it does not generate a new method in the instance object. Instead, it points to the method with the class information in the method area by address.

    Computer c = new Computer(); Same as stU variable generation process.

    Mount rand = “Hasse”; Name = “xiaoming” procedure.

    stu.computer = c;

  9. thecThe object ofComputerThe reference to the instance is assigned toStudentThe instancecomputerProperties. Namely: theStudentThe instancecomputerProperty pointing toComputerClass.