Simple analysis of the Java stack method area

Source: Internet
Author: User

Heap Stack (stack) and method area (methods) in JavaThe underlying data type is directly allocated in the stack space, and the form parameters of the method are allocated directly in the stack space when the method call is completed and reclaimed from the stack space. The reference data type needs to be created with new, which allocates an address space in the stack space and allocates the class variables of the object in the heap space. The reference parameter of the method, which allocates an address space in the stack space, and points to the object area of the heap space, which is reclaimed from the stack space when the method call is complete. When the local variable new comes out, allocates space in the stack space and heap space, when the local variable life cycle ends, the stack space is immediately reclaimed, and the heap space area waits for GC to reclaim. The literal parameter passed in when the method is called, first allocated in the stack space, and then allocated from the stack space after the method call is complete. String constants are allocated in the DATA area, this is allocated in heap space. Arrays are both allocated in the stack space and the actual size of the array is allocated in the heap space!
Oh, by the way, add static to the data area assignment.

From this allocation mechanism in Java, the stack can be understood as a stack is a storage area that the operating system establishes for a process, or a thread (a thread in a multithreaded operating system) for this thread, which has an advanced post-out feature.
Each Java application uniquely corresponds to a single JVM instance, and each instance uniquely corresponds to one heap. All instances or arrays of classes created by the application in the run are placed in this heap and shared by all threads of the application. Unlike C + +, allocating heap memory is automatically initialized in Java. The storage space for all objects in Java is allocated in the heap, but the reference to this object is allocated on the stack, that is, allocating memory from two places when an object is built, memory allocated in the heap actually establishes the object, and the memory allocated on the stack is just a pointer to the heap object (reference) Only.

< two >

These two days to look at the understanding of the JVM this book, recommended to advanced Java programmers to see, to you understand Java's underlying and operating mechanism has
A big help.
Nonsense don't want to talk about. Into the topic:
First understand the specific concepts:
Java's JVM memory can be divided into 3 zones: heap, stack (stack), and method area

Heap Area:
1. All objects are stored, each containing a class-corresponding information. (The purpose of class is to get operation instructions)
2.JVM only one heap area (heap) is shared by all threads, and the heap does not hold basic types and object references, only the object itself
Stack area:
1. Each thread contains a stack where only objects of the underlying data type are saved and references to custom objects (not objects), objects are stored in the heap area
2. The data in each stack (original type and object reference) is private and other stacks are inaccessible.
3. The stack is divided into 3 parts: the basic type variable area, the execution environment context, the operation instruction area (holds the operation instruction).
Method Area:
1. Also called the static zone, like the heap, is shared by all threads. The method area contains all class and static variables.
2. The method area contains elements that are always unique throughout the program, such as the class,static variable.
To get a clearer picture of what's going on in the runtime data area, let's prepare 2 props (2 very simple small programs).
Appmain.java

Java codePublic class Appmain //runtime, the JVM puts Appmain information into the method area
  1. {
  2. Public  The static void main (string[] args)//main the method itself into the method area.   
  3. {
  4. Sample test1 = new sample ( "Test 1" ); //test1 is a reference, so put it in the stack, sample is the custom object should be put into the heap
  5. Sample test2 = new sample ( "Test 2" );
  6. Test1.printname ();
  7. Test2.printname ();
  8. }
  9. } Sample.java
  10. Public class Sample //runtime, the JVM puts Appmain information into the method area
  11. {
  12. /** Example Name * /
  13. private name; //new The sample instance, the name reference is placed in the stack, and the name object is placed in the heap
  14. /** Construction Method * /
  15. Public Sample (String name)
  16. {
  17. this. Name = name;
  18. }
  19. /** Output * /
  20. Public  The void printname () //print method itself is placed in the method area.   
  21. {
  22. SYSTEM.OUT.PRINTLN (name);
  23. }
  24. }

OK, let's get started, the departure command is: "Java appmain", bag with our action Guide diagram, lets ' s go!



The system received our instructions and started a Java Virtual machine process, which first finds the Appmain.class file from Classpath, reads the binary data from the file, and then stores the class information of the Appmain class in the method area of the runtime data area. This process is called the Appmain class's loading process.
The Java virtual machine then 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.

Simple analysis of the Java stack 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.