Java Runtime data Region partitioning

Source: Internet
Author: User

Java Runtime data region partitioning JAVAJVM memory stack 1, overview

For Java programmers, memory leaks and memory overflow are not easily seen under the automatic memory management mechanism of virtual machines. However, if you do not understand how the virtual machine uses memory, once there is a memory leak and overflow problems, then the wrong way to go.

2. Run-time data area

The Java Virtual machine performs a Java program by dividing the memory it manages into several different data regions, as shown in.

2.1. Program counter

  Program Counter (Program Counter Register): is a small memory space that can be seen as the line number indicator of the byte code executed by the current thread. (in the conceptual model of a virtual machine, the bytecode interpreter relies on the value of the program counter to select the next byte-code directive to execute)
In order for the thread to switch back to the correct execution location, each thread needs to have a separate program counter, and the counters between the threads do not affect independent storage. It is also known that the program counter is a thread-isolated data area.

If a thread executes a Java method: The program counter records the address of the executing virtual machine bytecode instruction.
If you execute the native method: The counter value is empty

2.2. Java Virtual Machine stack

Same as program counter Span class= "Mark" style= "border:0; Background-color: #ddf3e7; Color: #29754d; font-size:90%; padding-top:2px; padding-right:4px; padding-bottom:2px; The padding-left:4px ">java VM stack (Java virtual machine Stacks) is also thread-private, with the same life cycle as the thread.

Java Virtual machine stack Describes the Java method execution Memory model: Each method will create a stack frame (stack frames). Each method, from the invocation until the completion of execution, corresponds to a stack frame from java the virtual machine stack into the stack to the stack.

stack frame (stack frame): For storing local variable table , operand stack, dynamic link, method exit and other information.

  local Variable table : The local variable table contains the various basic data types known to the compiler (Boolean,byte,int ... etc), object references, and ReturnAddress types. The amount of memory space required for a local variable table is allocated during compilation, and when entering a method, this method requires that the local variable space allocated in the stack is fully deterministic and does not change the size of the local variable table while the method is running.

There are two exceptions to the Java Virtual Machine stack area in the Java Virtual Machine specification:
A Stackoverflowerror exception is thrown if the thread requests a stack depth that is greater than the virtual machine's allowed depth.
If the current Java Virtual machine stack can be dynamically extended, an OutOfMemoryError exception will be thrown if the extension cannot request enough memory.

2.3. Local method Stack

  The native method stack is similar to the Java Virtual Machine stack (Native). The Java Virtual machine stack serves the Java method (that is, bytecode). The local method stack is the native method service used by the virtual machine.

The language, usage, and data structure of the methods used in the local method stack are not mandatory in the virtual machine specification, so the specific virtual machine can be implemented freely. For example, the Sun hotspot virtual machine directly merged the virtual machine stack with the local method stack.
The exception that is thrown is the same as the Java Virtual machine stack.

2.4. Java Heap

In general, the Java heap is the largest piece of memory managed by a Java virtual machine. The Java heap (Java heap) is a piece of memory that is shared by all threads and created when the virtual machine is started.

The Java Virtual machine specification describes the Java heap as:
All objects and arrays are allocated memory on the heap (which is less "absolute" as the technology progresses).
The Java heap can be in a physically discontinuous memory space, as long as it is logically contiguous.

The current mainstream virtual machine to Java heap is implemented according to extensible, if there is no memory in the heap for the instance to complete the allocation, and the heap can not be extended, the OutOfMemoryError exception will be thrown.

2.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 compiled by the immediate compiler that have been loaded by the virtual machine.

The Java Virtual machine specification describes the heap as:
The method area is a logical part of the heap, with an alias Non-heap (not a heap) that should be distinguished from the Java heap.
The method area, like the heap, does not require contiguous memory and scalability, and it can not implement garbage collection.

The OutOfMemoryError exception is thrown when the method area does not meet the memory allocation requirements.

2.6. Running a constant-rate pool

  Running a constant pool (runtime Constant Poll) is part of the method area.

The class file contains descriptions of the classes ' versions, fields, methods, interfaces, and so on, including Chang (Costant Pool Table)--used to hold various literal and symbolic references generated during compilation. This section will be stored in the run-time pool of the method area after the class is loaded.

Java virtual machines have strict specifications for each part of the class file, including Chang, which must conform to the specification to be recognized, loaded, and executed by the virtual machine. However, for running a constant pool, the Java Virtual Machine specification does not require any details, and different virtual machines can have different implementations, but in general, in addition to saving the symbolic references in the class file, the translated direct references are also saved in the run-time pool.

The run-time pool has an important feature relative to the class file constant pool-dynamic, not only the content of the constant pool that is pre-placed in the class file can enter the runtime pool, but also the new constant in the run-times pool during runtime. (This feature uses the Intern () method, such as the String Class)

The run-time constant pool is part of the method area, so it is limited by the memory of the method area and throws a OutOfMemoryError exception when the constant pool is no longer able to apply to memory.

Java Runtime data Region partitioning

Related Article

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.