J2SE quick advanced -- Java memory analysis, j2se advanced java
Program Execution Process
To analyze the memory in Java, let's first take a look at the program execution process:
As shown in, there are roughly three steps:
1. At the beginning, our program exists in the hard disk. When it is started and running, the program will be loaded into the memory. The memory here can be seen as our memory;
2. At this time, in addition to the code of the newly loaded program, the code of the operating system itself exists in the memory (well, this sentence can be treated as nonsense → _ → ), the operating system will find the Main method in the program to start executing the program;
3. The third step is the focus of this article. The system manages the memory during program execution. In Java, memory is roughly divided into four parts: heap (stack), stack (stack), data segment (data segment), and code segment (code segment ), they are used to store local variables in the program, new objects or arrays, static variables, and program code.
Here we mainly discuss heap and stack ).
Java Data Type
Let's take a look at the Java data types. There are two types of data in Java: Basic Data Types and reference data types, for example:
There are eight basic data types, including classes, arrays, and interfaces.
(Beginners may confuse String, Integer, and Other types with char, int, and other basic data types. Here we will explain that Integer is equivalent to int's "Packaging class ", string can be seen as an array of the char [] type. In addition, Byte, Float, and so on are similar. So these types should be treated as reference types .)
Memory Analysis
As shown in the first figure, the local variables we define are generally stored in the stack memory, these local variables can be both basic data-type variables (basic data-type variables directly save their values in the stack ), it can also be a reference type variable (the reference type variable saves the address of the object in heap memory that it points to in the stack ).
The heap memory stores the object pointed to by the address of the referenced type variable.
Actually, I hope you can take 10 seconds to carefully check the code and continue:
Public class MemoryAnalysis {public static void main (String [] args) {Person person = new Person ("James", 15); String newName = ""; int newAge = 18; person. setName (newName); person. setAge (newAge); person. sayHello () ;}} public class Person {public String name; public int age; public Person (String name, int age) {this. name = name; this. age = age;} public void SetName (String name) {this. name = name;} public void SetAge (int age) {this. age = age;} public void SayHello () {System. out. println ("My name is" + name + ", I" + age + "years old ");}}
The following code in the Main method is analyzed one by one:
Person person = new Person ("James", 15 );
After the Person class is instantiated:
Heap memory: allocate a piece of memory in the heap memory to store data in Person instances. Because the name attribute of person is of the String type, therefore, an additional piece of memory will be allocated in the heap memory to store the String-type "Xiao Ming". The name in person is only an address (address 3 ), this address points to the memory block that stores "Xiao Ming". In person, the age attribute is of the int type, so the memory unit of the age directly stores the "15" of the int type ".
Stack memory: Because person is a reference type, the memory unit person allocated in the stack memory stores the address (address 1) pointing to the person instance in the heap memory ). If the above Code defines only person, but not new, then only one person memory unit will be allocated in the stack memory, and the value in the class can be blank.
String newName = "Xiaohong ";
Because the newName type is String type, the actual content of newName will also be stored in heap memory. The memory unit newName allocated by stack memory only stores the address pointing to the "small red" in heap memory.
int newAge=18;
The newAge type is int, so the value (18) is directly stored in the unit newAge allocated by stack memory.
person.SetName(newName);
The function is a little complicated, because the SetName (String name) function has a reference type parameter name, and the input real parameter is newName, in this case, the value stored in newName (address 2) is assigned to this name, so the address stored in newName and name is the same (that is, the address points to "Xiaohong" at the same time "). Similarly, when the SetName (String name) function is executed, this. when name = name, the value (address 2) stored in name in stack memory is assigned to the name of person in heap memory, in this case, the name in person also stores the address pointing to "Xiaohong.
Then, the original name of person "James" will be recycled by java's garbage collection mechanism at a certain time.
After the method is executed, the memory occupied by the variable name in the stack memory is recycled.
person.SetAge(newAge);
Like SetName (String name), memory units are allocated for the parameter age in the stack memory during execution, except that the age parameter in the SetAge (int age) function is of the int type, therefore, you can directly store the value of the real parameter (18) in the memory allocation unit of the stack.
After the method is executed, the memory occupied by the variable age in the stack memory is recycled.
The above is the execution process of the Code. You must have guessed the running result if you understand some java basics:
Running result: