java-heap area, stack area, method area.

Source: Internet
Author: User

Reprint: http://blog.csdn.net/wangxin1982314/article/details/50293241

Heap Area:

Village Thread-manipulated data (object form Storage)

1 All of the objects are stored, each containing a class information corresponding to the--class purpose is to get the operation instruction

2 JVM only one heap area (heap) is shared by all threads, and no base type or object reference is stored in the heap, only the object itself.

Stack area:

1 Each thread contains a stack that holds only objects of the underlying data type and references to custom objects (not objects). Objects are stored in the heap area.

2 data in each battle (underlying data types and object references) are private and other stacks are inaccessible.

The 3 stack is divided into 3 parts: The basic type variable goes, the execution environment context, the operation instruction area (holds the operation instruction).

Method Area:

The byte code instruction that the thread executes.

1 is also called the static zone, the same as the heap. shared by all threads. The method area contains: All class and Static variables.

2 The method area contains elements that are unique throughout the program. such as the class static variable.

Appmain.java
public class Appmain//Runtime, the JVM puts Appmain information into the method area
{
public static void Main (string[] args)//main method itself into the method area.
{
Sample test1 = new sample ("Test 1"); Test1 is a reference, so put it in the stack, and the sample object should be put inside the heap.
Sample test2 = new sample ("Test 2");

Test1.printname ();
Test2.printname ();
}
}


Sample.java


public class sample//Runtime, the JVM puts sample information into the method area
{
/** Example Name */
Private name; After the new sample instance, the name reference is placed in the stack, and the name object is placed in the heap

/** Construction Method */
Public Sample (String name)
{
this. Name = name;
}

/** Output */
The public void Printname ()//print method itself is placed in the method area.
{
SYSTEM.OUT.PRINTLN (name);
}
}

The system received the instructions we sent and started a Java Virtual machine process,
This process first finds the Appmain.class file from the classpath, reads the binary data from the file,
The class information of the Appmain class is then stored in the method area of the run-time data area. This process is called the Appmain class's loading process.
Then
The Java Virtual machine navigates to the bytecode of the main () method of the Appmain class in the method area and starts executing its instructions.
The first statement of the main () method is:
Sample Test1=new sample ("Test 1");
The simple statement is to have the Java Virtual machine Create a sample instance, and to make the reference variable Test1 reference the instance. Looks like a small case a pile oh, let us follow the Java virtual machine, see how it is to perform this task:

1, Java Virtual machine A look, is not to establish a sample instance, simple, so go straight to the method area, first find the type of sample class information. As a result, hey, did not find @@, at the moment the method area has no sample class yet. Java Virtual machine is not a rib of the idiot, so, it carry forward the "hands-on, clothed" style, immediately loaded the sample class, the type of sample class information stored in the method area.

2, good, the information found, the following began to work. The first thing a Java Virtual machine does is allocate memory to a new sample instance in the heap, which holds a reference to the type information of the sample class of the method area. The reference mentioned here actually refers to the memory address of the type information of the sample class in the method area, in fact, it is a bit like the C language pointer ~ ~, and this address, it is stored in the sample instance data area.

3, in the Java Virtual machine process, each thread will have a method call stack, used to track the thread run in a series of method call procedures, each element of the stack is called a stack frame, each time the thread calls a method, the method stack is pressed into a new frame. The frames here are used to store the parameters of the method, local variables, and temporary data during the operation. OK, the principle is finished, let us continue our follow-up action! The Test1 in front of "=" is a variable defined in the main () method, which is visible as a local variable, so it is added to the Java method call stack that executes the main thread of the main () method. The "=" will point this test1 variable to the sample instance in the heap area, which means it holds a reference to the sample instance.

OK, so far, the Java Virtual machine has completed the task of executing this simple statement. Referring to our action guide diagram, we finally got a little bit of the Java Virtual machine, cool!

Next, the Java Virtual machine continues with subsequent instructions, continues to create another sample instance in the heap area, and then executes their printname () method in turn. When the Java virtual machine executes the Test1.printname () method, the Java virtual machine navigates to the sample instance in the heap based on the reference held by the local variable test1, and then navigates to the type information of the sample class in the method according to the reference held by the sample instance. The byte code of the Printname () method is obtained, followed by the instructions contained in the Printname () method.

java-heap area, stack area, method area.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.