JAVA-stack, stack, and method ., Java-heap Method
Reprinted: http://blog.csdn.net/wangxin1982314/article/details/50293241
Heap zone:
Data manipulated by village threads (stored as objects)
1. All objects are stored. Each object contains a corresponding class information. The purpose of the class is to obtain operation instructions.
2. Only one heap in the jvm is shared by all threads. The basic type and object reference are not stored in the heap, but only the object itself is stored.
Stack zone:
1. Each thread contains a stack zone. The stack only stores reference (not an object) of the basic data type and custom object ). Objects are stored in the heap.
2. Data (basic data types and object references) in each battle are private and cannot be accessed by other stacks.
Stack 3 is divided into three parts: basic type variables, execution environment context, and operation instruction area (storing operation instructions ).
Method Area:
Stores the bytecode commands executed by the thread.
1 is also called the static zone. It is the same as the heap. It is shared by all threads. The method zone contains all class and static variables.
2. The method area contains unique elements in the entire program, such as the class static variable.
AppMain. java
Public class AppMain // during runtime, jvm puts all appmain information in the Method Area
{
Public static void main (String [] args) // place the main method itself in the method area.
{
Sample test1 = new Sample ("Test 1"); // test1 is a reference, so put it in the stack area, the Sample object should be placed in the heap
Sample test2 = new Sample ("Test 2 ");
Test1.printName ();
Test2.printName ();
}
}
Sample. java
Public class Sample // during runtime, jvm puts all the Sample information into the Method Area
{
/** Example name */
Private name; // after the new Sample instance is created, the name reference is put into the stack, and the name object is put into the heap.
/** Constructor */
Public Sample (String name)
{
This. name = name;
}
/** Output */
Public void printName () // print method itself is placed in the method area.
{
System. out. println (name );
}
}
The system received the command and started a Java Virtual Machine Process,
This process first finds the AppMain. class file from the classpath and reads the binary data in the file,
Then, store the class information of the Appmain class in the Method Area of the runtime data zone. This process is called the loading process of the AppMain class.
Next,
The Java Virtual Machine locates the bytecode of the Main () method of the AppMain class in the Method Area and starts to execute its commands.
The first statement of this main () method is:
Sample test1 = new Sample ("Test 1 ");
The statement is simple, that is, let the Java Virtual Machine create a Sample instance, and make the reference variable test1 reference this instance. It looks like a small case. Let's track the Java Virtual Machine and see how it executes this task:
1. Check the Java Virtual Machine. Isn't it just creating a Sample instance? It's simple, so I went straight to the method area and found the type information of the Sample class first. The result is: Hey, no @ is found. There is no Sample class in the method area. The Java virtual machine is not an idiot, so it carries forward the style of "self-built, full-fledged", immediately loads the Sample class, and stores the type information of the Sample class in the method area.
2. Well, the information is found. Let's get started. The first thing Java virtual machine does is allocate memory for a new Sample instance in the heap area. This Sample instance holds a reference to the type information of the Sample class pointing to the method area. The reference here actually refers to the memory address of the Sample class type information in the method area. In fact, it is a bit similar to the pointer in C language ~~, This address is stored in the data area of the Sample instance.
3. In the Java Virtual Machine Process, each thread has a method call stack to track a series of method call processes in the thread running. Each element in the stack is called a stack frame, every time a thread calls a method, a new frame is pushed to the method stack. The frame here is used to store method parameters, local variables, and temporary data during calculation. OK. After the principles are completed, let's continue our tracking operations! Test1 located before "=" is a variable defined in the main () method. It is visible that it is a local variable. Therefore, it is added to the execution of main () the method is in the JAVA method call stack of the main thread of the method. "=" Points the test1 variable to the Sample instance in the heap area, that is, it holds a reference pointing to the Sample instance.
Okay. So far, the Java Virtual Machine has completed the execution of this simple statement. Based on our action wizard diagram, we finally figured out a little bit of details about the Java Virtual Machine, COOL!
Next, the Java Virtual Machine will continue to execute subsequent commands, create another Sample instance in the heap area, and then execute their printName () method in sequence. When the Java virtual machine executes the test1.printName () method, the Java Virtual Machine locates the Sample instance in the heap area based on the reference held by the local variable test1, and then based on the reference held by the Sample instance, locate the Sample class type information in the method to obtain the bytecode of the printName () method, and then execute the commands contained in the printName () method.