JVM learning-runtime data zone and jvm learning data Zone

Source: Internet
Author: User

JVM learning-runtime data zone and jvm learning data Zone

Unlike C and C ++ programs, the memory management of Java programs is taken over by the java Virtual Machine (JVM), which reduces the burden on Java programmers, however, if an OutOfMemory or StackOverFlow Exception error occurs, it is difficult to locate the error quickly if you do not know the memory management details of the JVM virtual machine.

During runtime, the JVM divides the memory it manages into several different data regions: Program counters, virtual machine stacks, local method stacks, stacks, and method zones. These regions store different data and have different functions.

The memory managed by JVM includes the following runtime data partitions:

1. Program counters

A program counter is a small memory space and can be regarded as the row number indicator of the bytecode executed by the current thread. In order for the thread to be switched back to the correct execution position, each thread must have a separate program counter so that each thread does not interfere with each other and is stored independently. Therefore, this region is a private thread zone.

It is worth mentioning that this region is the only region in which the Java Virtual Machine specification does not specify any OutOfMemoryError exception.

2. Java Virtual Machine Stack

The Java Virtual Machine stack is also thread-proprietary. The VM stack describes the Memory Model of Java method execution. Each method creates a stack frame during execution to store information such as the local variable table, operand stack, dynamic link, and method exit. The process from calling to execution of each method is the process of stack frames going into and out of the stack in the Virtual Machine stack.

The local variable table stores various basic data types (boolean, byte, char, int, short, float, double, long) that are known during the compilation period, and references the object) and returnAddress (the address that points to a bytecode instruction. The memory space required for the local variable table is allocated during compilation.

In the Java Virtual Machine specification, this region specifies two exception conditions: If the stack depth requested by the thread is greater than the depth allowed by the virtual machine, an StackOverFlow exception will be thrown; if the Virtual Machine stack can be dynamically expanded, if you cannot apply for sufficient memory during expansion, an OutOfemory exception will be thrown.

3. Local method Stack

The local method stack plays a similar role with the Virtual Machine stack. The local method stack serves the native method used by the virtual machine. Some virtual machines directly merge the local method stack with the Virtual Machine stack. The local method Stack also throws StackOverFlow and OutOfMemoryError exceptions.

4. Java heap

Java heap is the most familiar area. It is the largest memory managed by Java virtual machines. A Java heap is a memory area shared by all threads. It is created when a virtual machine is started. The only purpose of this memory area is to store object instances. Almost all object instances allocate memory in the Java heap. The Java heap can be in physically discontinuous memory space, as long as it is logically continuous. In practice, Java heap is extensible and generally has the following parameters:-Xmx (maximum heap value)-Xms (minimum heap value) control. If the heap does not have memory for instance allocation and the heap cannot be extended, an OutOfMemoryError is thrown.

5. Method Area

The method area is also the memory area shared by various threads. It is used to store the class information, constants, static variables, and Code Compiled by the real-time compiler. When the method area cannot meet the memory allocation requirements, an OutOfMemoryError error is thrown.

6. runtime constant pool

The runtime constant pool is part of the method area. In addition to the version, field, method, and interface information of the Class, the Class file also contains the constant pool ), it is used to store various literal and symbolic references generated during the compilation period. This part of content is stored in the method zone runtime pool after the class is loaded. A major feature of the constant pool of the runtime constant pool relative to the Class file is that it is dynamic, that is, it does not require constants to be generated only during the compilation period, and constants can also be placed in the pool during the runtime. The itern () method in the String class is an example. When the intern method is called, if the pool already containsStringObject string (Useequals(Object)Method), returns the string in the pool. OtherwiseStringThe object is added to the pool, and thisStringObject reference. It follows the following rules: For any two stringssAndt, When and only whens.equals(t)Istrue,s.intern() == t.intern()Onlytrue. An OutOfMemoryError error is thrown when the constant Pool cannot be allocated enough memory.

7. Direct Memory

Direct Memory is not part of the VM runtime data zone, nor is it part of the memory zone defined by the VM specification. However, this part of memory is frequently used. Direct Memory is not limited by the VM memory size, but is limited by the local memory size. Therefore, an OutOfMemoryError error may also be thrown.

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.