Java Memory structure (execution-time data region)

Source: Internet
Author: User

The Java Virtual Machine specification Specifies that the Java Virtual machine memory is actually the Java Virtual Machine Execution data area, and its schema is as follows:

The method area and heap are data areas shared by all threads.

Java Virtual machine stack. The local method stack and the program counter are thread-isolated data areas.

(1). Program Counter:

is a small amount of memory space, its function can be regarded as the current thread is running the bytecode of the line number indicator, the bytecode parser works by changing the value of the program counter to select the next need to run the bytecode directive.

The basic functions such as branching, looping, jumping, exception handling, and thread recovery are all dependent on the program counter to complete.

The multithreading of a Java Virtual machine is achieved by switching threads in turn and allocating processor run time slices. At any one time, a processor simply runs a thread instruction. So. To ensure that the thread switches back to the correct run location, each thread needs a separate program counter, so the program counter is a thread-private memory.

The program counter is the only memory area in the Java virtual machine that does not stipulate that no matter what memory overflows outofmemoryerror.

(2). Java Virtual machine stack:

The Java Virtual machine stack is also thread-private. Its life cycle is the same as the thread.

The virtual machine stack description describes the memory model in which the Java method runs: Each method is created at the same time to create a stack frame for storing local variable tables, operand stacks, dynamic connections, and method exits. Each method is called until the completion of the process, corresponding to a stack frame in the virtual machine from the stack to the stack process.

The local variable table of the Java Virtual machine stack holds 8 Java basic type Data, object references (not the object instance itself) known by the compiler, and method return address ReturnAddress.

The local variable table space units of the Java Virtual machine stack are slots (slots), where a double and a long type of 64-bit length occupy two slots. The remaining data types occupy only one slot.

The memory space required for the local variable table is allocated during compilation, when a method is entered. The method needs to allocate how much local variable space in the frame is completely determined, and the size of the local variable table is not changed during the execution of the method.

The Java Virtual machine stack has two exceptions: if the stack depth requested by the thread is greater than the maximum depth agreed by the virtual machine, the Stackoverflowerror exception is thrown, assuming that the virtual machine stack can be dynamically extended, An OutOfMemoryError exception is thrown when the extension cannot request enough memory.

(3). Local Method Stack:

The local method stack works very much like the Java Virtual machine stack. The difference is that the Java Virtual machine stack is the Java method service that runs for the virtual machine. The local method stack is the operating system local method service that is called for the virtual machine.

The Java Virtual Machine specification does not impose a requirement on the implementation and data structures of the local method stack. The Sun hotspot virtual machine directly combined the Java Virtual machine stack with the local method stack.

Similar to the Java Virtual machine stack, the local method stack throws Stackoverflowerror and OutOfMemoryError exceptions.

(4). Heap:

A heap is the largest chunk of memory that a Java Virtual machine manages, and the Java heap is a chunk of memory that is shared by all threads. Created when a Java virtual machine is started. The only purpose of heap memory is to hold object instances. Almost all of the object instances are allocated memory on the heap.

The Java heap is the primary area of garbage collector management. From the point of view of garbage collection, because today's garbage collectors are basically using generational collection algorithms. Therefore, the Java heap can be subdivided into cenozoic and older generations.

The Java Virtual Machine specification stipulates that the heap can be in a physically discontinuous memory space, only logically contiguous. can be fixed-size in implementation. can also be dynamically extensible. The OutOfMemoryError exception is thrown if there is no memory-complete instance allocation in the heap and the heap size cannot be extended.

(5). Method Area:

The method area, like the heap, is an area of memory that is shared by each thread. It is used to store data such as class information, constants, static variables, and immediately compiled code, which have been loaded into the virtual aircraft. Although the Java Virtual Machine specification describes a method area description as a logical part of a heap, the method area has a name non-heap (not a heap).

The Sun hotspot virtual machine calls the method area a permanent generation (Permanent Generation), and the most important part of the method area is the execution of a constant pool.

class file In addition to the type of version number, field, method, interface and other descriptive information, the other information is a constant pool. For storing the various literal variables, symbolic references, direct references, etc. generated during the compilation period, the contents will be stored in the execution-time pool of the method area after the class is loaded, and the new constants can be stored in the constant pool during execution, such as The Intern () method of the string.

The OutOfMemoryError exception is also thrown when the method area and the execution constant pool are unable to satisfy memory allocations.

(6). Direct Memory:

Direct memory is not part of the data area when the Java Virtual machine executes, nor is it a memory area defined in the Java Virtual Machine specification. However, it will still be used in Java development.

The newly introduced NIO (new I/O) in JDK1.4 introduces a channel-and buffer-based I/O approach that can be used to directly allocate out-of-heap memory using the operating system local method library. A Directbytebuffer object stored in the Java heap is then manipulated as a reference to the out-of-heap direct memory, avoiding duplication of data between Java heap memory and local direct memory, which can significantly improve performance.

Although direct memory does not directly receive Java Virtual machine memory impact. However, assuming that the sum of each memory region of a Java virtual machine is greater than the physical memory limit, resulting in direct memory shortages, the OutOfMemoryError exception is thrown when dynamically expanding.

program counters, virtual machine stacks, and local method stacks in the Java Virtual machine memory structure these three regions are created with thread creation and are destroyed with thread destruction, so memory allocation and reclamation for these three zones are deterministic. The Java garbage collector focuses on the heap memory and the method area memory of the Java Virtual machine.

Java Memory structure (execution-time data region)

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.