java memory analysis

Table of contents

Three concepts you need to know before doing memory analysis

stack memory

heap memory

method area

A picture summary

Diagram 1: Instantiate a class with a parameterless constructor and do not perform any subsequent operations on the object

Diagram 2: Instantiate a class with a parameterized constructor and do not perform any subsequent operations on the object

Diagram 3: Create an object using a parameterized constructor and perform some subsequent operations on the object

Summarize

Three concepts you need to know before doing memory analysis

stack memory

  • The stack is a first-in-last-out (or last-in-first-out) structure, that is, the elements that enter the stack first can be accessed last
  • When a thread enters a java method function, it will push a stack frame into the stack of the current thread. This stack frame is used to save the state of the current thread, such as saving some parameters, local variables, intermediate processes of calculation, and other data
  • When exiting the function method, the content in the stack can be destroyed by modifying the pointer of the stack

heap memory

  • The first thing to explain is that what is commonly referred to as "stack" is not one content, but two content, namely "heap" and "stack".
  • The sole purpose of the heap is to store object instances
  • Each java application corresponds uniquely to a jvm instance, and each jvm instance corresponds to a heap, and the heap memory is shared by all threads of the java application
  • Regarding the relationship between processes and threads, here we can simply think that "a process can contain multiple threads, and all threads of the process share the resources of the process." To learn more about processes and threads, please learn the operating system
  • When initializing objects, non-static member variables, string constant pools, all object instances, and arrays, memory units need to be allocated on the heap

method area

  • The method area stores some elements that are always unique in the entire program
  • For example, a class or a static property defined by the static keyword

A picture summary

Diagram 1: Instantiate a class with a parameterless constructor and do not perform any subsequent operations on the object

Now have the following code snippet:

//class1.java
public class class1 {
    int age;
    String name;

    public static void main(String[] args) {
        class1 c1 = new class1();
    }
}

This code snippet is simply

A class named class1 is defined in the class1.java file. This class has two member variables and one static method

Next, perform memory analysis on this code segment step by step.

Diagram 2: Instantiate a class with a parameterized constructor and do not perform any subsequent operations on the object

There is the following code segment:

//class1.java
public class class1 {
    int age;
    String name;

    public class1(int age, String name) {
        this.age = age;
        this.name = name;
    }
    
    public static void main(String[] args) {
        class1 c1 = new class1(23, "Zhang San");
    }
}

This code segment implements the following operations:

  1. A class is defined called class1
  2. There are two member variables in class1, a constructor with parameters, and a static program entry (main method)
  3. In the program entry method, a class1 class named c1 is instantiated, and the parameterized constructor is called for initialization

The memory analysis is performed as follows:

Diagram 3: Create an object using a parameterized constructor and perform some subsequent operations on the object

There is the following code segment:

//class1.java
public class class1 {
    int age;
    String name;

    public class1(int age, String name) {
        this.age = age;
        this.name = name;
    }
}

//class2.java
public class class2 {
    public static void main(String[] args) {
        int age = 24;
        String name = "Li Si";
        class1 c1 = new class1(age, name);
        age = 25;
        name = "Wang Wu";
        createObject(c1)
    }

    public static void createObject(class1 c2) {
        c2 = new class1(26, "Wang Wu");
    }
}

This code segment performs the following operations

  1. There is a class in the class1.java file, the class name is class1, there are two member variables in this class, and there is a constructor with parameters
  2. There is a class in the class2.java file, the type is class2, in this class there is a static method, a program entry
  3. In the main function, there are two variables, a statement that uses a parameterized constructor to create an object, and two statements that reassign variables
  4. In the createObject function, an object is created by passing parameters

The following memory analysis: (the process is a bit long, watch patiently, it is not difficult)

To say a few more digressions, if it is difficult to understand why the effect of calling the createObject function in the above illustration is to create a new object instead of modifying the original object, you can run the following code to verify:

//class1.java
public class class1 {
    int age;
    String name;

    public class1(int age, String name) {
        this.age = age;
        this.name = name;
    }
}

//class2.java
public class class2 {
    public static void main(String[] args) {
        int age = 24;
        String name = "Li Si";
        class1 c1 = new class1(24, "Li Si");
        age = 25;
        name = "Wang Wu";
        System.out.println(c1);
        System.out.println(createObject(c1));
    }

    public static class1 createObject(class1 c1) {
        c1 = new class1(26, "Wang Wu");
        return c1;
    }
}

Here, in order to prove that the effect is the same in all cases, both the formal parameter and the actual parameter name are c1, but the reader should be very clear, the c1 in the first print statement is the address of the c1 object created in the main function; the second The c1 in the two print statements is also the address of the c1 object created in the main function; in the formal parameters of the createObject function, the formal parameter c1 obtains the address of an object, and this address is a copy of the actual parameter c1 passed in.

Of course, through the verification code above, we can also find that java actually passes parameters by value. No matter whether the parameter type is a value type or a reference type, the actual parameter is passed to the formal parameter. It is a value (or copy of the address of the actual parameter).

Summarize

java memory analysis, of course, IDE also has built-in memory analysis tools, but for beginners, it is recommended to learn how to do memory analysis without relying on analysis tools.

Although how to do memory analysis without relying on analysis tools is a very obscure content, it is very necessary for beginners. Spend more time to sort out the process of memory analysis, which is very helpful for better understanding java and in-depth analysis. Learning java has great benefits (of course, it is also true for other languages. How to use the language is very simple, but it is difficult to understand why it can be used in this way). It is recommended to start with the simplest code and conduct several memory analysis in person In order to better understand this piece of content.

Tags: Java jvm

Posted by enoyhs on Sat, 07 Jan 2023 23:52:25 +1030