Java run-time data region

Source: Internet
Author: User

The memory managed by the Java Virtual machine will include the following runtime data regions

Program counter (Counter Register)

at any moment, a JVM thread executes only one method of code. The method is called the current method of the thread

If the method is a Java method, That PC Register holds the address of the byte code instruction that the JVM is executing

Java VM Stack (Java Virtual machine stack)

As with the PC registers, the Java Virtual machine stack is also thread-private. Each JVM thread has its own Java Virtual machine stack, which is created at the same time as the thread and whose life cycle is the same 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 operand stack, the dynamic link, the method exit, and so on. Each method is called until the completion process corresponds to a stack frame in the virtual machine stack from the stack to the stack.

The JVM stack can be implemented as a fixed size, or it can be dynamically extended based on calculations.

If a fixed-size JVM stack design is used, then the JVM stack capacity of each thread should be selected independently when threads are created. The JVM implementation should provide the means to adjust the initial capacity of the JVM stack, and if you adopt a dynamically expanding and shrinking JVM stack, you should provide the means to adjust the maximum and minimum capacity.

Stackoverflowerror will be thrown if the thread requests a stack depth greater than the depth allowed by the virtual machine;

If the JVM stack can be dynamically scaled, but fails to request enough memory when trying to scale out, throw outofmemoryerror.

Local methods Stack (Native method Stacks)

The local method stack acts similarly to the virtual machine stack, which executes the Java method service for the virtual machine, which is the native method used by the virtual machine.

The virtual machine specification is not mandatory for the language used by methods in the local method stack, usage and data structures, and even some virtual machines (such as hotspot) directly merge the two.

This thing throws the same exception as the virtual machine stack above.

java Heap (Java heap)

The largest chunk of memory managed by a virtual machine, which is also shared by all threads , is created at the time the virtual machine is started, the meaning of which is to hold object instances where almost all object instances and arrays are allocated memory. The objects are managed automatically, which is commonly known as GC (Garbage Collector). That's it, there's a GC carrying it, don't worry about destroying the recycling thing.

java heap can be a fixed size, You can also dynamically scale (-XMS and-XMX) as your needs are, and shrink automatically when you don't need too much space.

java heap does not need to be guaranteed to be physically contiguous, As long as it's logically contiguous.

JVM implementations should be provided to programmers to tune Java The initial capacity of the heap, for the dynamic expansion and contraction of the heap, you should provide the means to adjust its maximum and minimum capacity.

If there is no memory in the heap to complete the instance assignment and the heap cannot be expanded, Will throw OutOfMemoryError.

Method Area ( Method Area )

Threads , which stores data such as class information, constants, static variables, and code compiled by the instant compiler, which are loaded by the virtual machine. Although this area is described by the virtual machine specification as a logical part of the heap, its name is non-heap and is used to make a difference with the heap.

java virtual machine implementations should be provided to programmers or end users to adjust the initial capacity of the method area Means for the dynamic expansion and contraction of the method area, it should provide the means to adjust its maximum and minimum capacity.

Here is a small example to illustrate the relationship between heaps, stacks, and method areas.

public class Test2 {public static void main (string[] args) {public Test2 t2 = new Test2 (); The JVM loads the Test2 class information into the method area, and the new Test2 () instance is saved in the heap area, Test2 references are saved in the stack}}

run a constant-rate pool ( Runtime Constant Pool )

It's part of the method area. In addition to information such as the version of the class, fields, methods, interfaces, and so on, the class file contains a constant pool (Constant pool Table) that holds the various literal and symbolic references generated during the compilation period, which are stored in the run-time pool of the method area after the class is loaded.

java the virtual machine to each part of the class file (Nature also packs The format of the constant pool) is strictly defined, and each byte used to store which data must conform to the requirements of the specification in order 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 the virtual machines implemented by different providers can implement this memory area as they want. However, in general, in addition to saving the symbolic references described in the class file, the translated direct references are also stored in the run-time-constant pool.

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 portion of memory is also used frequently and can cause outofmemoryerror anomalies to occur.

In JDK1.4 NiO, Bytebuffer has a method of allocatedirect (int capacity), which is an I/O method based on channel (channels) and buffers (buffer). It can use the native library to directly allocate out-of-heap memory and then manipulate it through a Directbytebuffer object stored in the Java heap as a reference to that 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 allocation of native direct memory is not limited by the size of the Java heap, 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. 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 (including physical and operating system-level limitations). This causes a OutOfMemoryError exception to occur when dynamic scaling occurs.



Java Runtime data region

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.