01-JVM Memory Area Review notes

Source: Internet
Author: User

Java Memory Area
    
1. Procedure counter (program Counter Register)
    A small piece of memory space in a virtual machine that acts as a line number indicator of the bytecode that is being executed by the current thread . In the virtual machine concept model (only the conceptual model, the various virtual machines may be implemented in some more efficient way), the bytecode interpreter works by changing the value of this counter to select the next need to execute the bytecode instruction, branch, loop, jump, exception handling, Basic functions such as thread recovery need to rely on this counter to complete .
    because the multithreading of a Java Virtual machine is implemented in a way that threads rotate and allocate processor execution time, at any given moment, a processor (a kernel for a multicore processor) executes only the instructions in one thread. Therefore, in order for the thread to switch back to the correct execution location, each thread needs to have a separate program counter , the counters between the threads do not affect each other, isolated storage. We call this memory area "thread-private" memory
If the thread is executing a Java method, this counter records the address of the virtual machine bytecode instruction being executed , and if the native method is being executed, the calculator value is empty (Undefined). This memory area is the only area in the Java Virtual Machine specification that does not stipulate any outofmemoryerror conditions.

2.Java VM Stack (Java virtual machine Stacks)     
    like the program counter, the Java Virtual machine stack is also thread-private , with the same life cycle as the thread. The virtual machine stack describes the memory model that is executed by the Java method: Each time a method is executed, a stack frame is created to store the local variable table, the Operation Stack, the dynamic link, the method exit, and so on. Each method is called until the completion of the process, corresponding to a stack frame in the virtual machine stack into the stack into the stack.
    the heap stack (stack) is usually said to be rough. The "stack" refers to the virtual machine stack, or the local variable table portion of the virtual machine stack. The local variable table holds the various basic types that are known during the compilation period (Boolean, Byte, char, short, int, float, long, double), object reference (reference type, which is not equivalent to the object itself, depending on the virtual machine implementation, it may be a reference pointer to the starting address of the object to execute, It is also possible to execute a handle representing the object or other location associated with the object) and the ReturnAddress type (the address of a bytecode directive).
64-bit lengths of long and double data consume 2 local variable spaces (slots), and other data types occupy only 1 . The memory space required for a local variable table is allocated during compilation, where the local variable space of a method is finalized and the local variable table size is not changed during the run of the method.

3. Local methods Stack (Native method Stacks)
    and the virtual machine stack play a very similar role, the difference is that the virtual machine stack for the virtual machine just want you a Java method (that is, bytecode) service, and the local method stack for the virtual machine used by the Nativce method service . The language, usage, and data structure of the methods used in the virtual machine specification for the local method stack are not mandatory, so the specific virtual machine can be implemented freely. Even some virtual machines, such as sun hotspot virtual machines, combine the local method stack and the virtual machine stack directly. As with the virtual machine stack, the local method stack throws Stackoverflowerror and OutOfMemoryError exceptions.

4.Java Heap
    for most applications, the Java heap (Java heap) is the largest piece of memory managed by a Java virtual machine. The Java heap is a piece of memory that is shared by all threads and created when the virtual machine is started. The only purpose of the word memory area is to hold object instances where almost all of the object instances are allocated memory. The description in Java Virtual machine planning is that all object instances and arrays are allocated on the heap, but with the development of the JIT compiler and the gradual maturation of the escape analysis technology, the stack allocation, scalar replacement optimization technology will cause some subtle changes to occur, All objects are assigned on the right and gradually become less "absolute".
The Java heap is also the main area of garbage collector management, so it is often referred to as the "GC Heap" (garbage collected heap). In the case of memory recycling, the Java heap can be subdivided into the new generation and the old age, since the collector is basically a generational collection algorithm .

5. Method Area
    The method area, like the Java heap, is an area of memory shared by each thread that stores data such as class information, constants, static variables, and code after the instant compiler that have been loaded by the virtual machine. Although the Java Virtual Machine specification describes the method area as a logical part of the heap, it has an alias called Non-heap (Not a heap), which should be distinguished from the Java heap.

6. Run a constant pool (runtime Constant)
        The run-time Pool is part of the method area , and in addition to the description of the class's version, field, method, interface, and other descriptive information, there is also a constant pool that holds the various literal and symbolic references generated by the compiler. This portion of memory is stored in the run-time pool of the method area after the class is loaded.
    Another important feature of the constant pool relative to the class file is that it is dynamic, and the Java language does not require constants to be generated only at compile time, that is, the memory of the constant pool in the class file is not pre-built to enter the method area to run the frequent pool . It is also possible to put new constants into the pool during the run , which is the intern method of the String class, which is used by developers more.
    since the run-time-constant pool is part of the method area, you naturally receive a limit on the method area memory, which throws a Outofmemryerror exception when the constant pool is no longer able to request memory.
    
7. Direct Memory
    Direct Memory is not part of the data area when the virtual machine is running, nor is it a memory area defined in the Java VM specification, but this part of memory is also used frequently and can cause outofmemoryerror anomalies to occur.
    The new Input/output class was added to the JDK1.4, introducing a channel-and buffer-based I/O approach that can be used to directly allocate out-of-heap memory using the native library. It is then manipulated by a Directbytebuffer object stored in the Java heap as a reference to this memory. This can significantly improve performance in some scenarios because it avoids copying data back and forth in the Java heap and native heap.
    Obviously, the native memory allocation does not receive the Java heap size limit , but since it is memory, it will certainly be subject to the size of the native total memory (including RAM and swap or paging files) and the processor addressing space limit . When the server administrator configures the virtual machine parameters, the parameters such as-xmx are usually set according to the actual memory, but the direct memory is often ignored, so that the sum of each memory area is greater than the physical memory limit(Packet physical and operating system-level limitations), resulting in a OutOfMemoryError exception when dynamic scaling occurs.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

01-JVM Memory Area Review notes

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.