Java Memory Area Partition Memo

Source: Internet
Author: User

Recently read the "in-depth understanding of virtual machine" memory allocation and management of this part of the content, here to do a summary to deepen my understanding of knowledge points, such as the wrong place, but also hope that the great God pointed out that I promptly corrected;

  

Memory Area Division

The first is the following picture:

    

                    Figure 1.0

This picture is download down on the internet, but it can be very intuitive to tell us how many memory areas the Java Virtual machine manages (including the method area, heap, virtual Machine stack, Local Method stack, program counter five regions), and how the thread works in these memory regions (virtual machine stack, Local method stack , the program counter three regions are thread-specific, the method area, the heap two regions is the thread sharing);

1. We first look at the program counter module in the gray module;    

It belongs to the Gray module, so its first feature is that it is thread-private, the counters between the threads do not affect each other, isolated storage (in a multithreaded environment to ensure that each thread executes bytecode instructions, the instruction of the next instruction will not be chaotic);

The second feature is the use of it. Learn the principle of computer components are aware of the PC register, the Java Virtual machine in the PC register function and Hardware-level pc register similar, as the current thread is executed by the line number indicator of the byte code (save the next need to execute the bytecode directive or the address of the instruction), The bytecode parser selects the next byte-code instruction to be executed by changing the value of the counter, such as conditional branching (if), loops (such as for), jumps (such as switch), exception handling (exception table jump address), thread recovery, and other basic functions that rely on this counter to complete;

2. Then look at the virtual machine stack module in the gray module;    

It is also a feature of the thread-private, its life cycle and the same thread, that is, a thread to execute the start, create a virtual machine stack, the end of the thread will also destroy the virtual machine stack;

Second feature the Stack element in the virtual machine stack is called a stack (as shown in 1.1), that is, there is an element in the virtual machine stack that indicates that there is also a stack in the virtual machine stack; the stack and stack of the pins correspond to a call to a method in the thread to the end of the execution of the method, Then the line stacks the stack needle is all out of the stack, thread execution ends, the line stacks is destroyed, recycling;

thirdly, Let's look at what the contents of the stack are stored in the structure, (as shown in 1.1), which are mainly contained in the Needle.

1. local variable table: in fact, This table is used to store the parameters of the function we are currently calling, as well as the local variables we define in the function, when the Java program is compiled into a class file, the max_ of the Code property of the Method table collection The locals data item determines the capacity of the maximum local variable table that the method needs to allocate (method table collection This part of the knowledge refers to the class file structure in the method table collection of Knowledge points, please consult yourself), plainly, after the compilation is completed, the largest size of the local variable table is fixed The capacity of the local variable table is the smallest unit in the variable slot (slot), and a slot in a 32-bit virtual machine can hold a data type that is less than 32 bits (boolean, byte, char, short, int, float, Reference and ReturnAddress eight species).

2. Operand stacks: the interpretation execution engine of a Java virtual machine is called a "stack-based execution engine", in which the stack refers to The-operand stack. The operand stack is also often referred to as the stack of Operations. Like a local variable table, the operand stack is organized into an array of word lengths. But unlike the former, it is not accessed through the index, but through the standard stack operation-the stack and the Stack. For example, If an instruction presses a value into the operand stack, another command can then eject the value for Use. The virtual machine takes the operand stack as its working area-most instructions will pop up the data from here, perform the operation, and then press the result back to the operand stack. For example, The iadd instruction is going to pop two integers from the operand stack, perform the addition operation, and press the result back to the operand stack to see the following example, which shows how the virtual machine adds the local variables of the two int types, and then saves the result to the third local variable:

Java bytecode directive (you can see the explanatory Text)

1 begin   2 iload_0    //  push the int in local variable 0 onto the stack  3 iload_1    
   
    // 
     Push the int in local variable 1 onto the stack  
    4 iadd       
    // 
     pop t  Wo ints, Add them, push result  
    5 istore_2   
    // 
     pop int, store into local variable 2  
    6
   

   

  Figure 1.2

In the preceding Byte-code sequence, the first two directives iload_0 and iload_1 are stored in the local variables indexed to 0 and 1 of the integer into the operand stack, after which the iadd instruction pops the two integers from the operand stack, and then presses the result into the operand stack. The fourth instruction istore_2 pops the result from the operand stack and stores it in the local variable area index of 2. 1.2 The state changes of local variables and operand stacks in this process are described in detail, and the local variable area and operand stack area which are not used in the graph are represented by BLANK.

      3. Dynamic linking

This first explains the static parsing and dynamic linkage of the method invocation, which means that the specific address of the method to be called is determined at compile time, and the symbolic reference parameter of the method invocation instruction is the direct reference In the Java language, the embodiment is 1. A static method invocation of a class, a private method invocation of a class, a method call to a parent class, a dynamic link means that the specific address of a method invocation can be determined only at run time, and in the Java language the embodiment is 1. A typical feature of a class is a method called the override Annotation. , if a symbolic reference is converted to a direct reference at the time of class loading or when it is first used , then this conversion becomes static parsing , and if it is converted to direct references during runtime , then the conversion becomes Dynamic linking; A dynamic link for storing a direct reference to a run-time transformation;

4. Return Address

The return of the method is divided into two cases, one is normal exit, after exiting will be based on the definition of the method to determine whether to pass the return value to the upper level of the caller, one is the exception caused by the end of the method, In this case, the calling method that returns the value to the upper layer is not passed. for exception exits if the exception is not caught and processed in the method body by catch, then this method will not produce any return value to its upper-level caller; the program jumps directly to the location of the matching exception handler, the first one to catch the exception;

    

            Figure 1.1

Virtual Machine Stack Learning experience: the use of a long time to introduce the virtual machine stack, mainly because this part of the content and our programming in the most commonly used method calls closely related, understand these, We write code will have more comprehensive and in-depth consideration, write code will be more robust;

3. next, Take a look at Figure 1.0, the third gray module local method stack;

It is very similar to the virtual machine stack, The difference is that the virtual machine stack for the virtual machine to execute Java methods (that is, Bytecode) services, and the local method stack for the virtual machine used by the native method service, in the hotspot virtual machine directly into the;

4. Thread Exclusive memory area is finished, next look at the shared memory area between threads the method area in the green area;

    It is used to store the class information loaded by the virtual machine, constants, static variables, The immediate compiler compiled code, and other data;

Here is a detailed description of the method area of the blog, written in very detailed;

http://blog.csdn.net/bingduanlbd/article/details/8548916/

5. Java heap for green areas    

    The Java heap is created as the virtual machine starts, and its sole purpose is to hold object instances where almost all of the object instances are allocated memory, which is also the area where the GC is primarily managed; when we new an object, the data portion of the object is stored in the heap, and the Object's reference is usually stored in the stack;

Note that it is a shared access between threads, so in the use of objects in the heap, in a multi-threaded environment to consider, the thread of the object of concurrent access to the problem;
  

differences in memory leaks and memory overflows

1. Memory leak refers to the objects we no longer use to clean up, due to the existence of reference chains, these objects still occupy memory, the virtual machine is not cleaned up, this part of the memory is not properly cleaned is a memory leak;

2. Memory overflow refers to the memory of the object we really need to use, beyond the memory that the virtual machine can allocate to us, and it will result in an OVERFLOW.

Java Memory Area Partition Memo

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.