This article will briefly explain the memory allocation of the JVM (Java Virtual machine) when we run the Java program.
First of all, let's start with a sense of understanding the next few nouns:
1, Stack, in general, the basic data type directly in the stack allocation of space, local variables (variables defined in the method code snippet) also directly allocated space in the stack, when the local variables are in the execution of the method after the completion of the space is immediately the JVM is recycled, there is a reference data type, That is, the reference to the object that we usually call the keyword new to create is also in the stack space, at this time, the JVM in the stack space to the object reference assigned an address space (the equivalent of a house number, through which you can find your home), in the heap space for the referenced object to allocate a space, The address reference in the stack space points to the object area in the heap space (through the house number to find the address);
2, heap, generally used to store the keyword new out of the data.
Next we have a program to intuitively feel the memory of the JVM when the program is running,
1 Packagedemo;2 3 Public classStudent {4 PrivateString name;5 Private intAge ;6 7 Public voidStudy () {8System.out.println ("I Love study!");9 }Ten One PublicString GetName () { A returnname; - } - the Public voidsetName (String name) { - This. Name =name; - } - + Public intGetage () { - returnAge ; + } A at Public voidSetage (intAge ) { - This. Age =Age ; - } -}
1 Packagedemo;2 3 Public classStudentdemo {4 Public Static voidMain (string[] args) {5Student Student =NewStudent ();6 7System.out.println (Student.getname () + ":" +student.getage ());8Student.setname ("John");9Student.setage (23);TenSystem.out.println (Student.getname () + ":" +student.getage ()); One A student.study (); - -Student Student2 =student; theStudent2.setname ("Jack"); -Student2.setage (25); -System.out.println (Student2.getname () + ":" +student2.getage ()); -System.out.println (Student.getname () + ":" +student.getage ()); + } -}
When we run the program, the JVM compiles the student class and the Studentdemo class and loads it into a place called the method area in the JVM, and the member and member methods of the class are also loaded into the method area, at which point the memory model is as follows
You can see that the study method has a 16-binary tag on the right, and the name and age variables do not, because each object has its own member variable, and the member method in the class can be shared by each object, in order to save memory space, The JVM assigns the token (also called the memory address) to the method to make it easier for each new object lookup call, and then the JVM automatically looks for the main method, applies a space to the main method in the stack, calls into the stack, and executes the 5th line of code in our student class, when The JVM allocates a piece of memory to the student object in the heap space and assigns it a memory address (if the object's member variable is not assigned, the JVM assigns an initial value to the variable), and the stack allocates a chunk of memory space to the memory address of the Student object area in the heap space, at which point the memory model is as follows
Following the code line 8th and line 9th, the program assigns a value to the member variable of the student object, and the JVM looks for the student class's variable in heap memory based on the address pointed to by student and assigns a new value to the variable
Line 12th, when the student object calls the study method, the JVM requests a memory space for the study method in the stack space
Immediately after the study method executes, the stack space is freed, line 14th of the code, and the reference to the Student2 object points to the address that student points to
The code 15 and 16 lines, for Student2 variable assignment value, because Student2 and student point to the same place, so the value of the variable in the student object is also changed
Here, all the code in the main method is executed, the stack space occupied by the main method is recycled, and the heap space waits for the GC to reclaim
The code execution results are as follows:
null: 0John:I Love study! Jack:25
Write more simple, only as a knowledge record, welcome correction!
Simple analysis of Java program memory